PlotView.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlotView.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Represents a control that displays a <see cref="PlotModel" />.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. /// <summary>
  13. /// Represents a control that displays a <see cref="PlotModel" />.
  14. /// </summary>
  15. public class PlotView : PlotBase
  16. {
  17. /// <summary>
  18. /// Identifies the <see cref="Controller"/> dependency property.
  19. /// </summary>
  20. public static readonly StyledProperty<IPlotController> ControllerProperty = AvaloniaProperty.Register<PlotView, IPlotController>(nameof(Controller));
  21. /// <summary>
  22. /// Identifies the <see cref="Model"/> dependency property.
  23. /// </summary>
  24. public static readonly StyledProperty<PlotModel> ModelProperty = AvaloniaProperty.Register<PlotView, PlotModel>(nameof(Model), null);
  25. /// <summary>
  26. /// The model lock.
  27. /// </summary>
  28. private readonly object modelLock = new object();
  29. /// <summary>
  30. /// The current model (synchronized with the <see cref="Model" /> property, but can be accessed from all threads.
  31. /// </summary>
  32. private PlotModel currentModel;
  33. /// <summary>
  34. /// The default plot controller.
  35. /// </summary>
  36. private IPlotController defaultController;
  37. /// <summary>
  38. /// Initializes static members of the <see cref="PlotView" /> class.
  39. /// </summary>
  40. static PlotView()
  41. {
  42. PaddingProperty.OverrideMetadata(typeof(PlotView), new StyledPropertyMetadata<Thickness>(new Thickness(8)));
  43. ModelProperty.Changed.AddClassHandler<PlotView>(ModelChanged);
  44. PaddingProperty.Changed.AddClassHandler<PlotView>(AppearanceChanged);
  45. }
  46. /// <summary>
  47. /// Gets or sets the model.
  48. /// </summary>
  49. /// <value>The model.</value>
  50. public PlotModel Model
  51. {
  52. get
  53. {
  54. return GetValue(ModelProperty);
  55. }
  56. set
  57. {
  58. SetValue(ModelProperty, value);
  59. }
  60. }
  61. /// <summary>
  62. /// Gets or sets the Plot controller.
  63. /// </summary>
  64. /// <value>The Plot controller.</value>
  65. public IPlotController Controller
  66. {
  67. get
  68. {
  69. return GetValue(ControllerProperty);
  70. }
  71. set
  72. {
  73. SetValue(ControllerProperty, value);
  74. }
  75. }
  76. /// <summary>
  77. /// Gets the actual model.
  78. /// </summary>
  79. /// <value>The actual model.</value>
  80. public override PlotModel ActualModel
  81. {
  82. get
  83. {
  84. return currentModel;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets the actual PlotView controller.
  89. /// </summary>
  90. /// <value>The actual PlotView controller.</value>
  91. public override IPlotController ActualController
  92. {
  93. get
  94. {
  95. return Controller ?? (defaultController ?? (defaultController = new PlotController()));
  96. }
  97. }
  98. /// <summary>
  99. /// Called when the visual appearance is changed.
  100. /// </summary>
  101. protected void OnAppearanceChanged()
  102. {
  103. InvalidatePlot(false);
  104. }
  105. /// <summary>
  106. /// Called when the visual appearance is changed.
  107. /// </summary>
  108. /// <param name="d">The d.</param>
  109. /// <param name="e">The <see cref="AvaloniaPropertyChangedEventArgs" /> instance containing the event data.</param>
  110. private static void AppearanceChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  111. {
  112. ((PlotView)d).OnAppearanceChanged();
  113. }
  114. /// <summary>
  115. /// Called when the model is changed.
  116. /// </summary>
  117. /// <param name="d">The sender.</param>
  118. /// <param name="e">The <see cref="AvaloniaPropertyChangedEventArgs" /> instance containing the event data.</param>
  119. private static void ModelChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  120. {
  121. ((PlotView)d).OnModelChanged();
  122. }
  123. /// <summary>
  124. /// Called when the model is changed.
  125. /// </summary>
  126. private void OnModelChanged()
  127. {
  128. lock (modelLock)
  129. {
  130. if (currentModel != null)
  131. {
  132. ((IPlotModel)currentModel).AttachPlotView(null);
  133. currentModel = null;
  134. }
  135. if (Model != null)
  136. {
  137. ((IPlotModel)Model).AttachPlotView(null); // detach so we can re-attach
  138. ((IPlotModel)Model).AttachPlotView(this);
  139. currentModel = Model;
  140. }
  141. }
  142. InvalidatePlot();
  143. }
  144. }
  145. }