PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/PointGraphBase.cs

#
C# | 210 lines | 172 code | 33 blank | 5 comment | 20 complexity | 841c82503eccbe683426c6f638500524 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Windows;
  3. using Microsoft.Research.DynamicDataDisplay.DataSources;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using Microsoft.Research.DynamicDataDisplay.Common;
  7. using Microsoft.Research.DynamicDataDisplay.Charts;
  8. namespace Microsoft.Research.DynamicDataDisplay
  9. {
  10. public abstract class PointsGraphBase : ViewportElement2D, IOneDimensionalChart
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="PointsGraphBase"/> class.
  14. /// </summary>
  15. protected PointsGraphBase()
  16. {
  17. Viewport2D.SetIsContentBoundsHost(this, true);
  18. }
  19. #region DataSource
  20. public IPointDataSource DataSource
  21. {
  22. get { return (IPointDataSource)GetValue(DataSourceProperty); }
  23. set { SetValue(DataSourceProperty, value); }
  24. }
  25. public static readonly DependencyProperty DataSourceProperty =
  26. DependencyProperty.Register(
  27. "DataSource",
  28. typeof(IPointDataSource),
  29. typeof(PointsGraphBase),
  30. new FrameworkPropertyMetadata
  31. {
  32. AffectsRender = true,
  33. DefaultValue = null,
  34. PropertyChangedCallback = OnDataSourceChangedCallback
  35. }
  36. );
  37. private static void OnDataSourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  38. {
  39. PointsGraphBase graph = (PointsGraphBase)d;
  40. if (e.NewValue != e.OldValue)
  41. {
  42. graph.DetachDataSource(e.OldValue as IPointDataSource);
  43. graph.AttachDataSource(e.NewValue as IPointDataSource);
  44. }
  45. graph.OnDataSourceChanged(e);
  46. }
  47. private void AttachDataSource(IPointDataSource source)
  48. {
  49. if (source != null)
  50. {
  51. source.DataChanged += OnDataChanged;
  52. }
  53. }
  54. private void DetachDataSource(IPointDataSource source)
  55. {
  56. if (source != null)
  57. {
  58. source.DataChanged -= OnDataChanged;
  59. }
  60. }
  61. private void OnDataChanged(object sender, EventArgs e)
  62. {
  63. OnDataChanged();
  64. }
  65. protected virtual void OnDataChanged()
  66. {
  67. UpdateBounds(DataSource);
  68. RaiseDataChanged();
  69. Update();
  70. }
  71. private void RaiseDataChanged()
  72. {
  73. if (DataChanged != null)
  74. {
  75. DataChanged(this, EventArgs.Empty);
  76. }
  77. }
  78. public event EventHandler DataChanged;
  79. protected virtual void OnDataSourceChanged(DependencyPropertyChangedEventArgs args)
  80. {
  81. IPointDataSource newDataSource = (IPointDataSource)args.NewValue;
  82. if (newDataSource != null)
  83. {
  84. UpdateBounds(newDataSource);
  85. }
  86. Update();
  87. }
  88. private void UpdateBounds(IPointDataSource dataSource)
  89. {
  90. if (Plotter2D != null)
  91. {
  92. var transform = GetTransform();
  93. DataRect bounds = BoundsHelper.GetViewportBounds(dataSource.GetPoints(), transform.DataTransform);
  94. Viewport2D.SetContentBounds(this, bounds);
  95. }
  96. }
  97. #endregion
  98. #region DataTransform
  99. private DataTransform dataTransform = null;
  100. public DataTransform DataTransform
  101. {
  102. get { return dataTransform; }
  103. set
  104. {
  105. if (dataTransform != value)
  106. {
  107. dataTransform = value;
  108. Update();
  109. }
  110. }
  111. }
  112. protected CoordinateTransform GetTransform()
  113. {
  114. if (Plotter == null)
  115. return null;
  116. var transform = Plotter2D.Viewport.Transform;
  117. if (dataTransform != null)
  118. transform = transform.WithDataTransform(dataTransform);
  119. return transform;
  120. }
  121. #endregion
  122. #region VisiblePoints
  123. public ReadOnlyCollection<Point> VisiblePoints
  124. {
  125. get { return GetVisiblePoints(this); }
  126. protected set { SetVisiblePoints(this, value); }
  127. }
  128. public static ReadOnlyCollection<Point> GetVisiblePoints(DependencyObject obj)
  129. {
  130. return (ReadOnlyCollection<Point>)obj.GetValue(VisiblePointsProperty);
  131. }
  132. public static void SetVisiblePoints(DependencyObject obj, ReadOnlyCollection<Point> value)
  133. {
  134. obj.SetValue(VisiblePointsProperty, value);
  135. }
  136. public static readonly DependencyProperty VisiblePointsProperty = DependencyProperty.RegisterAttached(
  137. "VisiblePoints",
  138. typeof(ReadOnlyCollection<Point>),
  139. typeof(PointsGraphBase),
  140. new FrameworkPropertyMetadata(null, OnVisiblePointsChanged));
  141. private static void OnVisiblePointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  142. {
  143. PointsGraphBase graph = d as PointsGraphBase;
  144. if (graph != null)
  145. {
  146. graph.RaiseVisiblePointsChanged();
  147. }
  148. }
  149. public event EventHandler VisiblePointsChanged;
  150. protected void RaiseVisiblePointsChanged()
  151. {
  152. VisiblePointsChanged.Raise(this);
  153. }
  154. private bool provideVisiblePoints = false;
  155. public bool ProvideVisiblePoints
  156. {
  157. get { return provideVisiblePoints; }
  158. set
  159. {
  160. provideVisiblePoints = value;
  161. UpdateCore();
  162. }
  163. }
  164. #endregion
  165. protected IEnumerable<Point> GetPoints()
  166. {
  167. return DataSource.GetPoints(GetContext());
  168. }
  169. private readonly DataSource2dContext context = new DataSource2dContext();
  170. protected DependencyObject GetContext()
  171. {
  172. //context.VisibleRect = Plotter2D.Viewport.Visible;
  173. //context.ScreenRect = Plotter2D.Viewport.Output;
  174. return context;
  175. }
  176. }
  177. }