Annotation.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Annotation.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // The annotation base class.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Controls;
  13. using OxyPlot.Annotations;
  14. /// <summary>
  15. /// The annotation base class.
  16. /// </summary>
  17. public abstract class Annotation : Control
  18. {
  19. /// <summary>
  20. /// Identifies the <see cref="Layer"/> dependency property.
  21. /// </summary>
  22. public static readonly StyledProperty<AnnotationLayer> LayerProperty = AvaloniaProperty.Register<Annotation, AnnotationLayer>(nameof(Layer), AnnotationLayer.AboveSeries);
  23. /// <summary>
  24. /// Identifies the <see cref="XAxisKey"/> dependency property.
  25. /// </summary>
  26. public static readonly StyledProperty<string> XAxisKeyProperty = AvaloniaProperty.Register<Annotation, string>(nameof(XAxisKey), null);
  27. /// <summary>
  28. /// Identifies the <see cref="YAxisKey"/> dependency property.
  29. /// </summary>
  30. public static readonly StyledProperty<string> YAxisKeyProperty = AvaloniaProperty.Register<Annotation, string>(nameof(YAxisKey), null);
  31. /// <summary>
  32. /// Identifies the <see cref="EdgeRenderingMode"/> dependency property.
  33. /// </summary>
  34. public static readonly StyledProperty<EdgeRenderingMode> EdgeRenderingModeProperty = AvaloniaProperty.Register<Annotation, EdgeRenderingMode>(nameof(EdgeRenderingMode), EdgeRenderingMode.Automatic);
  35. /// <summary>
  36. /// Gets or sets the rendering layer of the annotation. The default value is <see cref="AnnotationLayer.AboveSeries" />.
  37. /// </summary>
  38. public AnnotationLayer Layer
  39. {
  40. get
  41. {
  42. return GetValue(LayerProperty);
  43. }
  44. set
  45. {
  46. SetValue(LayerProperty, value);
  47. }
  48. }
  49. /// <summary>
  50. /// Gets or sets the X axis key.
  51. /// </summary>
  52. public string XAxisKey
  53. {
  54. get
  55. {
  56. return GetValue(XAxisKeyProperty);
  57. }
  58. set
  59. {
  60. SetValue(XAxisKeyProperty, value);
  61. }
  62. }
  63. /// <summary>
  64. /// Gets or sets the Y axis key.
  65. /// </summary>
  66. public string YAxisKey
  67. {
  68. get
  69. {
  70. return GetValue(YAxisKeyProperty);
  71. }
  72. set
  73. {
  74. SetValue(YAxisKeyProperty, value);
  75. }
  76. }
  77. /// <summary>
  78. /// Gets or sets the <see cref="OxyPlot.EdgeRenderingMode"/> for the annotation.
  79. /// </summary>
  80. public EdgeRenderingMode EdgeRenderingMode
  81. {
  82. get
  83. {
  84. return GetValue(EdgeRenderingModeProperty);
  85. }
  86. set
  87. {
  88. SetValue(EdgeRenderingModeProperty, value);
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the internal annotation object.
  93. /// </summary>
  94. public Annotations.Annotation InternalAnnotation { get; protected set; }
  95. /// <summary>
  96. /// Creates the internal annotation object.
  97. /// </summary>
  98. /// <returns>The annotation.</returns>
  99. public abstract Annotations.Annotation CreateModel();
  100. /// <summary>
  101. /// Synchronizes the properties.
  102. /// </summary>
  103. public virtual void SynchronizeProperties()
  104. {
  105. var a = InternalAnnotation;
  106. a.Layer = Layer;
  107. a.XAxisKey = XAxisKey;
  108. a.YAxisKey = YAxisKey;
  109. a.EdgeRenderingMode = EdgeRenderingMode;
  110. }
  111. /// <summary>
  112. /// Handles changes in appearance.
  113. /// </summary>
  114. /// <param name="d">The sender.</param>
  115. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
  116. protected static void AppearanceChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  117. {
  118. ((Annotation)d).OnVisualChanged();
  119. }
  120. /// <summary>
  121. /// The on visual changed handler.
  122. /// </summary>
  123. protected void OnVisualChanged()
  124. {
  125. (this.Parent as IPlot)?.ElementAppearanceChanged(this);
  126. }
  127. /// <summary>
  128. /// Handles changes in data.
  129. /// </summary>
  130. /// <param name="d">The sender.</param>
  131. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
  132. protected static void DataChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  133. {
  134. ((Annotation)d).OnDataChanged();
  135. }
  136. /// <summary>
  137. /// The on data changed handler.
  138. /// </summary>
  139. protected void OnDataChanged()
  140. {
  141. (this.Parent as IPlot)?.ElementDataChanged(this);
  142. }
  143. static Annotation()
  144. {
  145. LayerProperty.Changed.AddClassHandler<Annotation>(AppearanceChanged);
  146. XAxisKeyProperty.Changed.AddClassHandler<Annotation>(AppearanceChanged);
  147. YAxisKeyProperty.Changed.AddClassHandler<Annotation>(AppearanceChanged);
  148. EdgeRenderingModeProperty.Changed.AddClassHandler<Annotation>(AppearanceChanged);
  149. }
  150. }
  151. }