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

/Main/src/DynamicDataDisplay/Charts/MarkerElementPointGraph.cs

#
C# | 140 lines | 108 code | 21 blank | 11 comment | 11 complexity | a408f777327480851b3d6ce832d8f318 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System.Collections.Generic;
  2. using System.Windows;
  3. using System.Windows.Media;
  4. using Microsoft.Research.DynamicDataDisplay.DataSources;
  5. using Microsoft.Research.DynamicDataDisplay.PointMarkers;
  6. using Microsoft.Research.DynamicDataDisplay.Common;
  7. using System.Windows.Controls;
  8. namespace Microsoft.Research.DynamicDataDisplay
  9. {
  10. public class ElementMarkerPointsGraph : PointsGraphBase
  11. {
  12. /// <summary>List with created but unused markers</summary>
  13. private readonly List<UIElement> unused = new List<UIElement>();
  14. /// <summary>Initializes a new instance of the <see cref="MarkerPointsGraph"/> class.</summary>
  15. public ElementMarkerPointsGraph()
  16. {
  17. ManualTranslate = true; // We'll handle translation by ourselves
  18. }
  19. /// <summary>Initializes a new instance of the <see cref="MarkerPointsGraph"/> class.</summary>
  20. /// <param name="dataSource">The data source.</param>
  21. public ElementMarkerPointsGraph(IPointDataSource dataSource)
  22. : this()
  23. {
  24. DataSource = dataSource;
  25. }
  26. Grid grid;
  27. Canvas canvas;
  28. protected override void OnPlotterAttached(Plotter plotter)
  29. {
  30. base.OnPlotterAttached(plotter);
  31. grid = new Grid();
  32. canvas = new Canvas { ClipToBounds = true };
  33. grid.Children.Add(canvas);
  34. Plotter2D.CentralGrid.Children.Add(grid);
  35. }
  36. protected override void OnPlotterDetaching(Plotter plotter)
  37. {
  38. Plotter2D.CentralGrid.Children.Remove(grid);
  39. grid = null;
  40. canvas = null;
  41. base.OnPlotterDetaching(plotter);
  42. }
  43. protected override void OnDataChanged()
  44. {
  45. // if (canvas != null)
  46. // {
  47. // foreach(UIElement child in canvas.Children)
  48. // unused.Add(child);
  49. // canvas.Children.Clear();
  50. // }
  51. // todo почему так?
  52. base.OnDataChanged();
  53. }
  54. public ElementPointMarker Marker
  55. {
  56. get { return (ElementPointMarker)GetValue(MarkerProperty); }
  57. set { SetValue(MarkerProperty, value); }
  58. }
  59. public static readonly DependencyProperty MarkerProperty =
  60. DependencyProperty.Register(
  61. "Marker",
  62. typeof(ElementPointMarker),
  63. typeof(ElementMarkerPointsGraph),
  64. new FrameworkPropertyMetadata { DefaultValue = null, AffectsRender = true }
  65. );
  66. protected override void OnRenderCore(DrawingContext dc, RenderState state)
  67. {
  68. if (Marker == null)
  69. return;
  70. if (DataSource == null) // No data is specified
  71. {
  72. if (canvas != null)
  73. {
  74. foreach (UIElement child in canvas.Children)
  75. unused.Add(child);
  76. canvas.Children.Clear();
  77. }
  78. }
  79. else // There is some data
  80. {
  81. int index = 0;
  82. var transform = GetTransform();
  83. using (IPointEnumerator enumerator = DataSource.GetEnumerator(GetContext()))
  84. {
  85. Point point = new Point();
  86. DataRect bounds = DataRect.Empty;
  87. while (enumerator.MoveNext())
  88. {
  89. enumerator.GetCurrent(ref point);
  90. enumerator.ApplyMappings(Marker);
  91. if (index >= canvas.Children.Count)
  92. {
  93. UIElement newMarker;
  94. if (unused.Count > 0)
  95. {
  96. newMarker = unused[unused.Count - 1];
  97. unused.RemoveAt(unused.Count - 1);
  98. }
  99. else
  100. newMarker = Marker.CreateMarker();
  101. canvas.Children.Add(newMarker);
  102. }
  103. Marker.SetMarkerProperties(canvas.Children[index]);
  104. bounds.Union(point);
  105. Point screenPoint = point.DataToScreen(transform);
  106. Marker.SetPosition(canvas.Children[index], screenPoint);
  107. index++;
  108. }
  109. Viewport2D.SetContentBounds(this, bounds);
  110. while (index < canvas.Children.Count)
  111. {
  112. unused.Add(canvas.Children[index]);
  113. canvas.Children.RemoveAt(index);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }