TextualAnnotation.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TextualAnnotation.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Provides an abstract base class for annotations that contains text.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Layout;
  13. using global::Avalonia.Media;
  14. /// <summary>
  15. /// Provides an abstract base class for annotations that contains text.
  16. /// </summary>
  17. public abstract class TextualAnnotation : Annotation
  18. {
  19. /// <summary>
  20. /// Identifies the <see cref="Text"/> dependency property.
  21. /// </summary>
  22. public static readonly StyledProperty<string> TextProperty = AvaloniaProperty.Register<TextualAnnotation, string>(nameof(Text), null);
  23. /// <summary>
  24. /// Identifies the <see cref="TextPosition"/> dependency property.
  25. /// </summary>
  26. public static readonly StyledProperty<DataPoint> TextPositionProperty = AvaloniaProperty.Register<TextualAnnotation, DataPoint>(nameof(TextPosition), DataPoint.Undefined);
  27. /// <summary>
  28. /// Identifies the <see cref="TextRotation"/> dependency property.
  29. /// </summary>
  30. public static readonly StyledProperty<double> TextRotationProperty = AvaloniaProperty.Register<TextualAnnotation, double>(nameof(TextRotation), 0.0);
  31. /// <summary>
  32. /// Identifies the <see cref="TextColor"/> dependency property.
  33. /// </summary>
  34. public static readonly StyledProperty<Color> TextColorProperty = AvaloniaProperty.Register<TextualAnnotation, Color>(nameof(TextColor), Colors.Blue);
  35. /// <summary>
  36. /// Identifies the <see cref="TextHorizontalAlignment"/> dependency property.
  37. /// </summary>
  38. public static readonly StyledProperty<HorizontalAlignment> TextHorizontalAlignmentProperty = AvaloniaProperty.Register<TextualAnnotation, HorizontalAlignment>(nameof(TextHorizontalAlignment), HorizontalAlignment.Center);
  39. /// <summary>
  40. /// Identifies the <see cref="TextVerticalAlignment"/> dependency property.
  41. /// </summary>
  42. public static readonly StyledProperty<VerticalAlignment> TextVerticalAlignmentProperty = AvaloniaProperty.Register<TextualAnnotation, VerticalAlignment>(nameof(TextVerticalAlignment), VerticalAlignment.Center);
  43. /// <summary>
  44. /// Gets or sets the text.
  45. /// </summary>
  46. public string Text
  47. {
  48. get
  49. {
  50. return GetValue(TextProperty);
  51. }
  52. set
  53. {
  54. SetValue(TextProperty, value);
  55. }
  56. }
  57. /// <summary>
  58. /// Gets or sets the color of the text.
  59. /// </summary>
  60. public Color TextColor
  61. {
  62. get
  63. {
  64. return GetValue(TextColorProperty);
  65. }
  66. set
  67. {
  68. SetValue(TextColorProperty, value);
  69. }
  70. }
  71. /// <summary>
  72. /// Gets or sets the text position.
  73. /// </summary>
  74. /// <remarks>If the value is <c>DataPoint.Undefined</c>, the centroid of the polygon will be used.</remarks>
  75. public DataPoint TextPosition
  76. {
  77. get { return GetValue(TextPositionProperty); }
  78. set { SetValue(TextPositionProperty, value); }
  79. }
  80. /// <summary>
  81. /// Gets or sets the text horizontal alignment.
  82. /// </summary>
  83. /// <value>The text horizontal alignment.</value>
  84. public HorizontalAlignment TextHorizontalAlignment
  85. {
  86. get
  87. {
  88. return GetValue(TextHorizontalAlignmentProperty);
  89. }
  90. set
  91. {
  92. SetValue(TextHorizontalAlignmentProperty, value);
  93. }
  94. }
  95. /// <summary>
  96. /// Gets or sets the vertical alignment of text (above or below the line).
  97. /// </summary>
  98. public VerticalAlignment TextVerticalAlignment
  99. {
  100. get
  101. {
  102. return GetValue(TextVerticalAlignmentProperty);
  103. }
  104. set
  105. {
  106. SetValue(TextVerticalAlignmentProperty, value);
  107. }
  108. }
  109. /// <summary>
  110. /// Gets or sets the rotation angle (degrees).
  111. /// </summary>
  112. public double TextRotation
  113. {
  114. get
  115. {
  116. return GetValue(TextRotationProperty);
  117. }
  118. set
  119. {
  120. SetValue(TextRotationProperty, value);
  121. }
  122. }
  123. /// <summary>
  124. /// Synchronizes the properties.
  125. /// </summary>
  126. public override void SynchronizeProperties()
  127. {
  128. base.SynchronizeProperties();
  129. var a = (Annotations.TextualAnnotation)InternalAnnotation;
  130. a.TextColor = TextColor.ToOxyColor();
  131. a.Text = Text;
  132. a.TextPosition = TextPosition;
  133. a.TextRotation = TextRotation;
  134. a.TextHorizontalAlignment = TextHorizontalAlignment.ToHorizontalAlignment();
  135. a.TextVerticalAlignment = TextVerticalAlignment.ToVerticalAlignment();
  136. }
  137. static TextualAnnotation()
  138. {
  139. TextProperty.Changed.AddClassHandler<TextualAnnotation>(AppearanceChanged);
  140. TextPositionProperty.Changed.AddClassHandler<TextualAnnotation>(AppearanceChanged);
  141. TextRotationProperty.Changed.AddClassHandler<TextualAnnotation>(AppearanceChanged);
  142. TextColorProperty.Changed.AddClassHandler<TextualAnnotation>(AppearanceChanged);
  143. TextHorizontalAlignmentProperty.Changed.AddClassHandler<TextualAnnotation>(AppearanceChanged);
  144. TextVerticalAlignmentProperty.Changed.AddClassHandler<TextualAnnotation>(AppearanceChanged);
  145. }
  146. }
  147. }