Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/AxisGrid.cs @ 13749

Last change on this file since 13749 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 7.1 KB
Line 
1using System.Windows;
2using System.Windows.Media;
3using Microsoft.Research.DynamicDataDisplay.Charts;
4using System.Windows.Controls;
5using System;
6using System.Windows.Shapes;
7using System.ComponentModel;
8using System.Diagnostics;
9using System.Diagnostics.CodeAnalysis;
10using Microsoft.Research.DynamicDataDisplay.Common;
11using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
12
13
14namespace Microsoft.Research.DynamicDataDisplay
15{
16  /// <summary>
17  /// Draws grid over viewport. Number of
18  /// grid lines depends on Plotter's MainHorizontalAxis and MainVerticalAxis ticks.
19  /// </summary>
20  public class AxisGrid : ContentControl, IPlotterElement
21  {
22    static AxisGrid()
23    {
24      Type thisType = typeof(AxisGrid);
25      Panel.ZIndexProperty.OverrideMetadata(thisType, new FrameworkPropertyMetadata(-1));
26    }
27
28    [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
29    internal void BeginTicksUpdate()
30    {
31    }
32    internal void EndTicksUpdate()
33    {
34      UpdateUIRepresentation();
35    }
36
37    protected internal MinorTickInfo<double>[] MinorHorizontalTicks { get; set; }
38
39    protected internal MinorTickInfo<double>[] MinorVerticalTicks { get; set; }
40
41    protected internal double[] HorizontalTicks { get; set; }
42
43    protected internal double[] VerticalTicks { get; set; }
44
45
46    private bool drawVerticalTicks = true;
47    /// <summary>
48    /// Gets or sets a value indicating whether to draw vertical tick lines.
49    /// </summary>
50    /// <value><c>true</c> if draw vertical ticks; otherwise, <c>false</c>.</value>
51    public bool DrawVerticalTicks
52    {
53      get { return drawVerticalTicks; }
54      set
55      {
56        if (drawVerticalTicks != value)
57        {
58          drawVerticalTicks = value;
59          UpdateUIRepresentation();
60        }
61      }
62    }
63
64    private bool drawHorizontalTicks = true;
65    /// <summary>
66    /// Gets or sets a value indicating whether to draw horizontal tick lines.
67    /// </summary>
68    /// <value><c>true</c> if draw horizontal ticks; otherwise, <c>false</c>.</value>
69    public bool DrawHorizontalTicks
70    {
71      get { return drawHorizontalTicks; }
72      set
73      {
74        if (drawHorizontalTicks != value)
75        {
76          drawHorizontalTicks = value;
77          UpdateUIRepresentation();
78        }
79      }
80    }
81
82    private bool drawHorizontalMinorTicks = false;
83    /// <summary>
84    /// Gets or sets a value indicating whether to draw horizontal minor ticks.
85    /// </summary>
86    /// <value>
87    ///   <c>true</c> if draw horizontal minor ticks; otherwise, <c>false</c>.
88    /// </value>
89    public bool DrawHorizontalMinorTicks
90    {
91      get { return drawHorizontalMinorTicks; }
92      set
93      {
94        if (drawHorizontalMinorTicks != value)
95        {
96          drawHorizontalMinorTicks = value;
97          UpdateUIRepresentation();
98        }
99      }
100    }
101
102    private bool drawVerticalMinorTicks = false;
103    /// <summary>
104    /// Gets or sets a value indicating whether to draw vertical minor ticks.
105    /// </summary>
106    /// <value>
107    ///   <c>true</c> if draw vertical minor ticks; otherwise, <c>false</c>.
108    /// </value>
109    public bool DrawVerticalMinorTicks
110    {
111      get { return drawVerticalMinorTicks; }
112      set
113      {
114        if (drawVerticalMinorTicks != value)
115        {
116          drawVerticalMinorTicks = value;
117          UpdateUIRepresentation();
118        }
119      }
120    }
121
122    private double gridBrushThickness = 1;
123
124    private Path path = new Path();
125    private Canvas canvas = new Canvas();
126    /// <summary>
127    /// Initializes a new instance of the <see cref="AxisGrid"/> class.
128    /// </summary>
129    public AxisGrid()
130    {
131      IsHitTestVisible = false;
132
133      canvas.ClipToBounds = true;
134
135      path.Stroke = Brushes.LightGray;
136      path.StrokeThickness = gridBrushThickness;
137
138      Content = canvas;
139    }
140
141    private readonly ResourcePool<LineGeometry> lineGeometryPool = new ResourcePool<LineGeometry>();
142    private readonly ResourcePool<Line> linePool = new ResourcePool<Line>();
143
144    private void UpdateUIRepresentation()
145    {
146      foreach (UIElement item in canvas.Children)
147      {
148        Line line = item as Line;
149        if (line != null)
150        {
151          linePool.Put(line);
152        }
153      }
154
155      canvas.Children.Clear();
156      Size size = RenderSize;
157
158      DrawMinorHorizontalTicks();
159      DrawMinorVerticalTicks();
160
161      GeometryGroup prevGroup = path.Data as GeometryGroup;
162      if (prevGroup != null)
163      {
164        foreach (LineGeometry geometry in prevGroup.Children)
165        {
166          lineGeometryPool.Put(geometry);
167        }
168      }
169
170      GeometryGroup group = new GeometryGroup();
171      if (HorizontalTicks != null && drawHorizontalTicks)
172      {
173        double minY = 0;
174        double maxY = size.Height;
175
176        for (int i = 0; i < HorizontalTicks.Length; i++)
177        {
178          double screenX = HorizontalTicks[i];
179          LineGeometry line = lineGeometryPool.GetOrCreate();
180          line.StartPoint = new Point(screenX, minY);
181          line.EndPoint = new Point(screenX, maxY);
182          group.Children.Add(line);
183        }
184      }
185
186      if (VerticalTicks != null && drawVerticalTicks)
187      {
188        double minX = 0;
189        double maxX = size.Width;
190
191        for (int i = 0; i < VerticalTicks.Length; i++)
192        {
193          double screenY = VerticalTicks[i];
194          LineGeometry line = lineGeometryPool.GetOrCreate();
195          line.StartPoint = new Point(minX, screenY);
196          line.EndPoint = new Point(maxX, screenY);
197          group.Children.Add(line);
198        }
199      }
200
201      canvas.Children.Add(path);
202      path.Data = group;
203    }
204
205    private void DrawMinorVerticalTicks()
206    {
207      Size size = RenderSize;
208      if (MinorVerticalTicks != null && drawVerticalMinorTicks)
209      {
210        double minX = 0;
211        double maxX = size.Width;
212
213        for (int i = 0; i < MinorVerticalTicks.Length; i++)
214        {
215          double screenY = MinorVerticalTicks[i].Tick;
216          if (screenY < 0)
217            continue;
218          if (screenY > size.Height)
219            continue;
220
221          Line line = linePool.GetOrCreate();
222
223          line.Y1 = screenY;
224          line.Y2 = screenY;
225          line.X1 = minX;
226          line.X2 = maxX;
227          line.Stroke = Brushes.LightGray;
228          line.StrokeThickness = MinorVerticalTicks[i].Value * gridBrushThickness;
229
230          canvas.Children.Add(line);
231        }
232      }
233    }
234
235    private void DrawMinorHorizontalTicks()
236    {
237      Size size = RenderSize;
238      if (MinorHorizontalTicks != null && drawHorizontalMinorTicks)
239      {
240        double minY = 0;
241        double maxY = size.Height;
242
243        for (int i = 0; i < MinorHorizontalTicks.Length; i++)
244        {
245          double screenX = MinorHorizontalTicks[i].Tick;
246          if (screenX < 0)
247            continue;
248          if (screenX > size.Width)
249            continue;
250
251          Line line = linePool.GetOrCreate();
252          line.X1 = screenX;
253          line.X2 = screenX;
254          line.Y1 = minY;
255          line.Y2 = maxY;
256          line.Stroke = Brushes.LightGray;
257          line.StrokeThickness = MinorHorizontalTicks[i].Value * gridBrushThickness;
258
259          canvas.Children.Add(line);
260        }
261      }
262    }
263
264    #region IPlotterElement Members
265
266    void IPlotterElement.OnPlotterAttached(Plotter plotter)
267    {
268      this.plotter = plotter;
269      plotter.CentralGrid.Children.Add(this);
270    }
271
272    void IPlotterElement.OnPlotterDetaching(Plotter plotter)
273    {
274      plotter.CentralGrid.Children.Remove(this);
275      this.plotter = null;
276    }
277
278    private Plotter plotter;
279    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
280    public Plotter Plotter
281    {
282      get { return plotter; }
283    }
284
285    #endregion
286  }
287}
Note: See TracBrowser for help on using the repository browser.