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