TextAnnotation.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TextAnnotation.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // This is a Avalonia wrapper of OxyPlot.TextAnnotation
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Layout;
  13. using global::Avalonia.Media;
  14. /// <summary>
  15. /// This is a Avalonia wrapper of OxyPlot.TextAnnotation
  16. /// </summary>
  17. public class TextAnnotation : TextualAnnotation
  18. {
  19. /// <summary>
  20. /// Identifies the <see cref="Background"/> dependency property.
  21. /// </summary>
  22. public static readonly StyledProperty<Color> BackgroundProperty = AvaloniaProperty.Register<TextAnnotation, Color>(nameof(Background), MoreColors.Undefined);
  23. /// <summary>
  24. /// Identifies the <see cref="Offset"/> dependency property.
  25. /// </summary>
  26. public static readonly StyledProperty<Vector> OffsetProperty = AvaloniaProperty.Register<TextAnnotation, Vector>(nameof(Offset), default);
  27. /// <summary>
  28. /// Identifies the <see cref="Padding"/> dependency property.
  29. /// </summary>
  30. public static readonly StyledProperty<Thickness> PaddingProperty = AvaloniaProperty.Register<TextAnnotation, Thickness>(nameof(Padding), new Thickness(4));
  31. /// <summary>
  32. /// Identifies the <see cref="Stroke"/> dependency property.
  33. /// </summary>
  34. public static readonly StyledProperty<Color> StrokeProperty = AvaloniaProperty.Register<TextAnnotation, Color>(nameof(Stroke), Colors.Black);
  35. /// <summary>
  36. /// Identifies the <see cref="StrokeThickness"/> dependency property.
  37. /// </summary>
  38. public static readonly StyledProperty<double> StrokeThicknessProperty = AvaloniaProperty.Register<TextAnnotation, double>(nameof(StrokeThickness), 1.0);
  39. /// <summary>
  40. /// Initializes static members of the <see cref = "TextAnnotation" /> class.
  41. /// </summary>
  42. static TextAnnotation()
  43. {
  44. TextColorProperty.OverrideDefaultValue<TextAnnotation>(MoreColors.Automatic);
  45. TextColorProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  46. TextHorizontalAlignmentProperty.OverrideDefaultValue<TextAnnotation>(HorizontalAlignment.Right);
  47. TextHorizontalAlignmentProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  48. TextVerticalAlignmentProperty.OverrideDefaultValue<TextAnnotation>(VerticalAlignment.Top);
  49. TextVerticalAlignmentProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  50. BackgroundProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  51. OffsetProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  52. PaddingProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  53. StrokeProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  54. StrokeThicknessProperty.Changed.AddClassHandler<TextAnnotation>(AppearanceChanged);
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref = "TextAnnotation" /> class.
  58. /// </summary>
  59. public TextAnnotation()
  60. {
  61. InternalAnnotation = new Annotations.TextAnnotation();
  62. }
  63. /// <summary>
  64. /// Gets or sets the fill color of the background rectangle.
  65. /// </summary>
  66. public Color Background
  67. {
  68. get
  69. {
  70. return GetValue(BackgroundProperty);
  71. }
  72. set
  73. {
  74. SetValue(BackgroundProperty, value);
  75. }
  76. }
  77. /// <summary>
  78. /// Gets or sets the position offset (screen coordinates).
  79. /// </summary>
  80. public Vector Offset
  81. {
  82. get
  83. {
  84. return GetValue(OffsetProperty);
  85. }
  86. set
  87. {
  88. SetValue(OffsetProperty, value);
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the padding of the background rectangle.
  93. /// </summary>
  94. public Thickness Padding
  95. {
  96. get
  97. {
  98. return GetValue(PaddingProperty);
  99. }
  100. set
  101. {
  102. SetValue(PaddingProperty, value);
  103. }
  104. }
  105. /// <summary>
  106. /// Gets or sets the stroke color of the background rectangle.
  107. /// </summary>
  108. public Color Stroke
  109. {
  110. get
  111. {
  112. return GetValue(StrokeProperty);
  113. }
  114. set
  115. {
  116. SetValue(StrokeProperty, value);
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets the stroke thickness of the background rectangle.
  121. /// </summary>
  122. public double StrokeThickness
  123. {
  124. get
  125. {
  126. return GetValue(StrokeThicknessProperty);
  127. }
  128. set
  129. {
  130. SetValue(StrokeThicknessProperty, value);
  131. }
  132. }
  133. /// <summary>
  134. /// Creates the internal annotation object.
  135. /// </summary>
  136. /// <returns>The annotation.</returns>
  137. public override Annotations.Annotation CreateModel()
  138. {
  139. SynchronizeProperties();
  140. return InternalAnnotation;
  141. }
  142. /// <summary>
  143. /// Synchronizes the properties.
  144. /// </summary>
  145. public override void SynchronizeProperties()
  146. {
  147. base.SynchronizeProperties();
  148. var a = (Annotations.TextAnnotation)InternalAnnotation;
  149. a.TextHorizontalAlignment = HorizontalAlignment.ToHorizontalAlignment();
  150. a.Background = Background.ToOxyColor();
  151. a.Offset = Offset.ToScreenVector();
  152. a.TextVerticalAlignment = VerticalAlignment.ToVerticalAlignment();
  153. a.Padding = Padding.ToOxyThickness();
  154. a.Stroke = Stroke.ToOxyColor();
  155. a.StrokeThickness = StrokeThickness;
  156. }
  157. }
  158. }