Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/LineChart.cs @ 1179

Last change on this file since 1179 was 1059, checked in by bspisic, 16 years ago

#424
Implemented history stack for clipping areas - by pressing the "back" button the antecedent clipping area will be set

File size: 11.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Windows.Forms;
5using HeuristicLab.Core;
6
7namespace HeuristicLab.Visualization {
8  public class LinesShape : WorldShape {
9    private readonly RectangleShape background = new RectangleShape(0, 0, 1, 1, Color.FromArgb(240, 240, 240));
10
11    public LinesShape(RectangleD clippingArea, RectangleD boundingBox)
12      : base(clippingArea, boundingBox) {
13      AddShape(background);
14    }
15
16    public override void Draw(Graphics graphics, Rectangle viewport, RectangleD clippingArea) {
17      UpdateLayout();
18      base.Draw(graphics, viewport, clippingArea);
19    }
20
21    private void UpdateLayout() {
22      background.Rectangle = ClippingArea;
23    }
24  }
25
26  public partial class LineChart : ViewBase {
27    private readonly IChartDataRowsModel model;
28    private int maxDataRowCount;
29    private Boolean zoomFullView;
30    private double minDataValue;
31    private double maxDataValue;
32
33    private readonly WorldShape root;
34    private readonly TextShape titleShape;
35    private readonly LinesShape linesShape;
36    private readonly LegendShape legendShape;
37
38    private readonly XAxis xAxis;
39
40    /// <summary>
41    /// This constructor shouldn't be called. Only required for the designer.
42    /// </summary>
43    public LineChart() {
44      InitializeComponent();
45    }
46
47    /// <summary>
48    /// Initializes the chart.
49    /// </summary>
50    /// <param name="model">Referenz to the model, for data</param>
51    public LineChart(IChartDataRowsModel model) : this() {
52      if (model == null) {
53        throw new NullReferenceException("Model cannot be null.");
54      }
55
56      //TODO: correct Rectangle to fit
57
58      RectangleD dummy = new RectangleD(0, 0, 1, 1);
59
60      root = new WorldShape(dummy, dummy);
61
62      linesShape = new LinesShape(dummy, dummy);
63      root.AddShape(linesShape);
64
65      legendShape = new LegendShape(0, 0, 0, 0, 0, Color.Black);
66      //legendShape.AddLegendItem(new LegendItem("test", Color.Red, 5));
67      //legendShape.AddLegendItem(new LegendItem("test1", Color.Blue, 5));
68      //legendShape.AddLegendItem(new LegendItem("test2", Color.Pink, 5));
69      root.AddShape(legendShape);
70
71      xAxis = new XAxis(dummy, dummy);
72      root.AddShape(xAxis);
73
74      titleShape = new TextShape(0, 0, "Title", 15);
75      root.AddShape(titleShape);
76
77
78      canvas.MainCanvas.WorldShape = root;
79      canvas.Resize += delegate { UpdateLayout(); };
80
81      UpdateLayout();
82
83      this.model = model;
84      Item = model;
85
86      maxDataRowCount = 0;
87      //The whole data rows are shown per default
88      zoomFullView = true;
89      minDataValue = Double.PositiveInfinity;
90      maxDataValue = Double.NegativeInfinity;
91    }
92
93    /// <summary>
94    /// Layout management - arranges the inner shapes.
95    /// </summary>
96    private void UpdateLayout() {
97      root.ClippingArea = new RectangleD(0, 0, canvas.Width, canvas.Height);
98
99      titleShape.X = 10;
100      titleShape.Y = canvas.Height - 10;
101
102      linesShape.BoundingBox = new RectangleD(0, 20, canvas.Width, canvas.Height);
103
104      xAxis.BoundingBox = new RectangleD(linesShape.BoundingBox.X1,
105                                         0,
106                                         linesShape.BoundingBox.X2,
107                                         linesShape.BoundingBox.Y1);
108
109      legendShape.BoundingBox = new RectangleD(10, 10, 110, canvas.Height - 50);
110    }
111
112    public void ResetView() {
113      zoomFullView = true;
114      ZoomToFullView();
115
116      canvas.Invalidate();
117    }
118
119    #region Add-/RemoveItemEvents
120
121    protected override void AddItemEvents() {
122      base.AddItemEvents();
123
124      model.DataRowAdded += OnDataRowAdded;
125      model.DataRowRemoved += OnDataRowRemoved;
126      model.ModelChanged += OnModelChanged;
127
128      foreach (IDataRow row in model.Rows) {
129        OnDataRowAdded(row);
130      }
131    }
132
133    protected override void RemoveItemEvents() {
134      base.RemoveItemEvents();
135
136      model.DataRowAdded -= OnDataRowAdded;
137      model.DataRowRemoved -= OnDataRowRemoved;
138      model.ModelChanged -= OnModelChanged;
139    }
140
141    private void OnDataRowAdded(IDataRow row) {
142      row.ValueChanged += OnRowValueChanged;
143      row.ValuesChanged += OnRowValuesChanged;
144      if (row.Count > maxDataRowCount) {
145        maxDataRowCount = row.Count;
146      }
147
148      legendShape.AddLegendItem(new LegendItem(row.Label, row.Color, row.Thickness));
149      legendShape.CreateLegend();
150      InitLineShapes(row);
151    }
152
153    private void ZoomToFullView() {
154      if (!zoomFullView) {
155        return;
156      }
157      RectangleD newClippingArea = new RectangleD(-0.1,
158                                                  minDataValue - ((maxDataValue - minDataValue)*0.05),
159                                                  maxDataRowCount - 0.9,
160                                                  maxDataValue + ((maxDataValue - minDataValue)*0.05));
161
162      SetLineClippingArea(newClippingArea);
163      historyStack.Push(newClippingArea);
164    }
165
166    /// <summary>
167    /// Sets the clipping area of the data to display.
168    /// </summary>
169    /// <param name="clippingArea"></param>
170    private void SetLineClippingArea(RectangleD clippingArea) {
171      linesShape.ClippingArea = clippingArea;
172      xAxis.ClippingArea = new RectangleD(linesShape.ClippingArea.X1,
173                                          xAxis.BoundingBox.Y1,
174                                          linesShape.ClippingArea.X2,
175                                          xAxis.BoundingBox.Y2);
176    }
177
178    private void InitLineShapes(IDataRow row) {
179      List<LineShape> lineShapes = new List<LineShape>();
180      if (row.Count > 0) {
181        maxDataValue = Math.Max(row[0], maxDataValue);
182        minDataValue = Math.Min(row[0], minDataValue);
183      }
184      for (int i = 1; i < row.Count; i++) {
185        LineShape lineShape = new LineShape(i - 1, row[i - 1], i, row[i], 0, row.Color, row.Thickness, row.Style);
186        lineShapes.Add(lineShape);
187        // TODO each DataRow needs its own WorldShape so Y Axes can be zoomed independently.
188        linesShape.AddShape(lineShape);
189        maxDataValue = Math.Max(row[i], maxDataValue);
190        minDataValue = Math.Min(row[i], minDataValue);
191      }
192
193      rowToLineShapes[row] = lineShapes;
194      ZoomToFullView();
195
196      canvas.Invalidate();
197    }
198
199    private void OnDataRowRemoved(IDataRow row) {
200      row.ValueChanged -= OnRowValueChanged;
201      row.ValuesChanged -= OnRowValuesChanged;
202    }
203
204    private readonly IDictionary<IDataRow, List<LineShape>> rowToLineShapes = new Dictionary<IDataRow, List<LineShape>>();
205
206    // TODO use action parameter
207    private void OnRowValueChanged(IDataRow row, double value, int index, Action action) {
208      xAxis.SetLabel(index, index.ToString());
209
210      List<LineShape> lineShapes = rowToLineShapes[row];
211      maxDataValue = Math.Max(value, maxDataValue);
212      minDataValue = Math.Min(value, minDataValue);
213
214      if (index > lineShapes.Count + 1) {
215        throw new NotImplementedException();
216      }
217
218      // new value was added
219      if (index > 0 && index == lineShapes.Count + 1) {
220        if (maxDataRowCount < row.Count) {
221          maxDataRowCount = row.Count;
222        }
223        LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], 0, row.Color, row.Thickness, row.Style);
224        lineShapes.Add(lineShape);
225        // TODO each DataRow needs its own WorldShape so Y Axes can be zoomed independently.
226        linesShape.AddShape(lineShape);
227      }
228
229      // not the first value
230      if (index > 0) {
231        lineShapes[index - 1].Y2 = value;
232      }
233
234      // not the last value
235      if (index > 0 && index < row.Count - 1) {
236        lineShapes[index].Y1 = value;
237      }
238      ZoomToFullView();
239
240      canvas.Invalidate();
241    }
242
243    // TODO use action parameter
244    private void OnRowValuesChanged(IDataRow row, double[] values, int index, Action action) {
245      foreach (double value in values) {
246        OnRowValueChanged(row, value, index++, action);
247      }
248    }
249
250    private void OnModelChanged() {}
251
252    #endregion
253
254    #region Begin-/EndUpdate
255
256    private int beginUpdateCount = 0;
257
258    public void BeginUpdate() {
259      beginUpdateCount++;
260    }
261
262    public void EndUpdate() {
263      if (beginUpdateCount == 0) {
264        throw new InvalidOperationException("Too many EndUpdates.");
265      }
266
267      beginUpdateCount--;
268
269      if (beginUpdateCount == 0) {
270        canvas.Invalidate();
271      }
272    }
273
274    #endregion
275
276    #region Zooming / Panning
277
278    private readonly Stack<RectangleD> historyStack = new Stack<RectangleD>();
279    private RectangleShape rectangleShape;
280
281    private void canvasUI1_KeyDown(object sender, KeyEventArgs e) {
282      if(e.KeyCode == Keys.Back && historyStack.Count > 1) {
283        historyStack.Pop();
284
285        RectangleD clippingArea = historyStack.Peek();
286 
287        SetNewClippingArea(clippingArea);
288        canvas.Invalidate();
289      }
290    }
291
292    private void canvasUI1_MouseDown(object sender, MouseEventArgs e) {
293      Focus();
294
295      if (ModifierKeys == Keys.Control) {
296        CreateZoomListener(e);
297      } else {
298        CreatePanListener(e);
299      }
300    }
301
302    private void canvasUI1_MouseWheel(object sender, MouseEventArgs e) {
303      if (ModifierKeys == Keys.Control) {
304        double zoomFactor = (e.Delta > 0) ? 0.9 : 1.1;
305
306        RectangleD clippingArea = ZoomListener.ZoomClippingArea(linesShape.ClippingArea, zoomFactor);
307
308        SetLineClippingArea(clippingArea);
309        canvas.Invalidate();
310      }
311    }
312
313    private void CreateZoomListener(MouseEventArgs e) {
314      ZoomListener zoomListener = new ZoomListener(e.Location);
315      zoomListener.DrawRectangle += DrawRectangle;
316      zoomListener.OnMouseUp += OnZoom_MouseUp;
317
318      canvas.MouseEventListener = zoomListener;
319
320      rectangleShape = new RectangleShape(e.X, e.Y, e.X, e.Y, Color.Blue);
321      rectangleShape.Opacity = 50;
322
323      linesShape.AddShape(rectangleShape);
324    }
325
326    private void OnZoom_MouseUp(object sender, MouseEventArgs e) {
327      canvas.MouseEventListener = null;
328
329      RectangleD clippingArea = rectangleShape.Rectangle;
330
331      SetLineClippingArea(clippingArea);
332      historyStack.Push(clippingArea);
333
334      linesShape.RemoveShape(rectangleShape);
335
336      zoomFullView = false; //user wants to zoom => no full view
337
338      canvas.Invalidate();
339    }
340
341    private void DrawRectangle(Rectangle rectangle) {
342      rectangleShape.Rectangle = Transform.ToWorld(rectangle, canvas.ClientRectangle, linesShape.ClippingArea);
343      canvas.Invalidate();
344    }
345
346    private void CreatePanListener(MouseEventArgs e) {
347      PanListener panListener = new PanListener(canvas.ClientRectangle, linesShape.ClippingArea, e.Location);
348
349      panListener.SetNewClippingArea += SetNewClippingArea;
350      panListener.OnMouseUp += delegate {
351        historyStack.Push(linesShape.ClippingArea);
352        canvas.MouseEventListener = null;
353      };
354
355      canvas.MouseEventListener = panListener;
356    }
357
358    private void SetNewClippingArea(RectangleD newClippingArea) {
359      SetLineClippingArea(newClippingArea);
360
361      zoomFullView = false;
362      canvas.Invalidate();
363    }
364
365    #endregion
366  }
367}
Note: See TracBrowser for help on using the repository browser.