Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 987 was 987, checked in by mstoeger, 16 years ago

Implemented simple XAxis (#433)

File size: 9.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Windows.Forms;
5using HeuristicLab.Core;
6
7namespace HeuristicLab.Visualization {
8  public partial class LineChart : ViewBase {
9    private readonly IChartDataRowsModel model;
10    private int maxDataRowCount;
11    private Boolean zoomFullView;
12    private double minDataValue;
13    private double maxDataValue;
14
15    private readonly WorldShape root;
16    private readonly XAxis xAxis;
17
18    /// <summary>
19    /// This constructor shouldn't be called. Only required for the designer.
20    /// </summary>
21    public LineChart() {
22      InitializeComponent();
23    }
24
25    /// <summary>
26    /// Initializes the chart.
27    /// </summary>
28    /// <param name="model">Referenz to the model, for data</param>
29    public LineChart(IChartDataRowsModel model) : this() {
30      if (model == null) {
31        throw new NullReferenceException("Model cannot be null.");
32      }
33
34      //TODO: correct Rectangle to fit
35      RectangleD clientRectangle = new RectangleD(-1, -1, 1, 1);
36
37      root = new WorldShape(clientRectangle, clientRectangle);
38
39      xAxis = new XAxis();
40      root.AddShape(xAxis);
41
42      canvasUI1.MainCanvas.WorldShape = root;
43         
44      CreateMouseEventListeners();
45         
46      this.model = model;
47      Item = model;
48      maxDataRowCount = 0;
49      //The whole data rows are shown per default
50      zoomFullView = true;
51      minDataValue = Double.PositiveInfinity;
52      maxDataValue = Double.NegativeInfinity;
53    }
54
55    public void ResetView() {
56      zoomFullView = true;
57      ZoomToFullView();
58      canvasUI1.Invalidate();
59    }
60
61    #region Add-/RemoveItemEvents
62
63    protected override void AddItemEvents() {
64      base.AddItemEvents();
65
66      model.DataRowAdded += OnDataRowAdded;
67      model.DataRowRemoved += OnDataRowRemoved;
68      model.ModelChanged += OnModelChanged;
69
70      foreach (IDataRow row in model.Rows) {
71        OnDataRowAdded(row);
72      }
73    }
74
75    protected override void RemoveItemEvents() {
76      base.RemoveItemEvents();
77
78      model.DataRowAdded -= OnDataRowAdded;
79      model.DataRowRemoved -= OnDataRowRemoved;
80      model.ModelChanged -= OnModelChanged;
81    }
82
83    private void OnDataRowAdded(IDataRow row) {
84      row.ValueChanged += OnRowValueChanged;
85      row.ValuesChanged += OnRowValuesChanged;
86      if (row.Count > maxDataRowCount)
87        maxDataRowCount = row.Count;
88     
89      InitLineShapes(row);
90      InitXAxis();
91    }
92
93    private void InitXAxis() {
94      int numLabels = 0;
95
96      foreach (IDataRow row in model.Rows) {
97        numLabels = Math.Max(numLabels, row.Count);
98      }
99
100      xAxis.ClearLabels();
101
102      for (int i = 0; i < numLabels; i++) {
103        xAxis.SetLabel(i, i.ToString());
104      }
105    }
106
107    private void ZoomToFullView() {
108      if(!zoomFullView)
109        return;
110      RectangleD newClippingArea =  new RectangleD(-0.1,
111        minDataValue-((maxDataValue-minDataValue)*0.05),
112        maxDataRowCount-0.9,
113        maxDataValue + ((maxDataValue - minDataValue) * 0.05));
114      root.ClippingArea = newClippingArea;
115    }
116
117    private void InitLineShapes(IDataRow row) {
118      List<LineShape> lineShapes = new List<LineShape>();
119      if (row.Count > 0) {
120        maxDataValue = Math.Max(row[0], this.maxDataValue);
121        minDataValue = Math.Min(row[0], minDataValue);
122      }
123      for (int i = 1; i < row.Count; i++) {
124        LineShape lineShape = new LineShape(i - 1, row[i - 1], i, row[i], 0, row.Color, row.Thickness, row.Style);
125        lineShapes.Add(lineShape);
126        // TODO each DataRow needs its own WorldShape so Y Axes can be zoomed independently.
127        root.AddShape(lineShape);
128        maxDataValue = Math.Max(row[i], maxDataValue);
129        minDataValue = Math.Min(row[i], minDataValue);
130      }
131
132      rowToLineShapes[row] = lineShapes;
133      ZoomToFullView();
134      canvasUI1.Invalidate();
135    }
136
137    private void OnDataRowRemoved(IDataRow row) {
138      row.ValueChanged -= OnRowValueChanged;
139      row.ValuesChanged -= OnRowValuesChanged;
140    }
141
142    private readonly IDictionary<IDataRow, List<LineShape>> rowToLineShapes = new Dictionary<IDataRow, List<LineShape>>();
143
144    // TODO use action parameter
145    private void OnRowValueChanged(IDataRow row, double value, int index, Action action) {
146      xAxis.SetLabel(index, index.ToString());
147
148      List<LineShape> lineShapes = rowToLineShapes[row];
149      maxDataValue = Math.Max(value, maxDataValue);
150      minDataValue = Math.Min(value, minDataValue);
151
152      if (index > lineShapes.Count + 1) {
153        throw new NotImplementedException();
154      }
155
156      // new value was added
157      if (index > 0 && index == lineShapes.Count + 1) {
158        if (maxDataRowCount < row.Count)
159          maxDataRowCount = row.Count;
160        LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], 0, row.Color, row.Thickness, row.Style);
161        lineShapes.Add(lineShape);
162        // TODO each DataRow needs its own WorldShape so Y Axes can be zoomed independently.
163        root.AddShape(lineShape);
164      }
165
166      // not the first value
167      if (index > 0) {
168        lineShapes[index - 1].Y2 = value;
169      }
170
171      // not the last value
172      if (index > 0 && index < row.Count - 1) {
173        lineShapes[index].Y1 = value;
174      }
175      ZoomToFullView();
176      canvasUI1.Invalidate();
177    }
178
179    // TODO use action parameter
180    private void OnRowValuesChanged(IDataRow row, double[] values, int index, Action action) {
181      foreach (double value in values) {
182        OnRowValueChanged(row, value, index++, action);
183      }
184    }
185
186    private void OnModelChanged() {
187      InitXAxis();
188    }
189
190    #endregion
191
192    #region Begin-/EndUpdate
193
194    private int beginUpdateCount = 0;
195
196    public void BeginUpdate() {
197      beginUpdateCount++;
198    }
199
200    public void EndUpdate() {
201      if (beginUpdateCount == 0) {
202        throw new InvalidOperationException("Too many EndUpdates.");
203      }
204
205      beginUpdateCount--;
206
207      if (beginUpdateCount == 0) {
208        Invalidate();
209      }
210    }
211
212    #endregion
213
214    private MouseEventListener panListener;
215    private MouseEventListener zoomListener;
216
217    private void CreateMouseEventListeners() {
218      panListener = new MouseEventListener();
219      panListener.OnMouseMove += Pan_OnMouseMove;
220      panListener.OnMouseUp += Pan_OnMouseUp;
221
222      zoomListener = new MouseEventListener();
223      zoomListener.OnMouseMove += Zoom_OnMouseMove;
224      zoomListener.OnMouseUp += Zoom_OnMouseUp;
225    }
226
227    private RectangleD startClippingArea;
228
229    private void canvasUI1_MouseDown(object sender, MouseEventArgs e) {
230      if (ModifierKeys == Keys.Control) {
231        zoomListener.StartPoint = e.Location;
232        canvasUI1.MouseEventListener = zoomListener;
233
234        r = Rectangle.Empty;
235        rectangleShape = new RectangleShape(e.X, e.Y, e.X, e.Y, 1000, Color.Blue);
236
237        root.AddShape(rectangleShape);
238      } else {
239        panListener.StartPoint = e.Location;
240        canvasUI1.MouseEventListener = panListener;
241
242        startClippingArea = root.ClippingArea;
243      }
244    }
245
246    private void Pan_OnMouseUp(Point startPoint, Point actualPoint) {
247      canvasUI1.MouseEventListener = null;
248    }
249
250    private void Pan_OnMouseMove(Point startPoint, Point actualPoint) {
251      Rectangle viewPort = canvasUI1.ClientRectangle;
252
253      PointD worldStartPoint = Transform.ToWorld(startPoint, viewPort, startClippingArea);
254      PointD worldActualPoint = Transform.ToWorld(actualPoint, viewPort, startClippingArea);
255
256      double xDiff = worldActualPoint.X - worldStartPoint.X;
257      double yDiff = worldActualPoint.Y - worldStartPoint.Y;
258
259      RectangleD newClippingArea = new RectangleD();
260      newClippingArea.X1 = startClippingArea.X1 - xDiff;
261      newClippingArea.X2 = startClippingArea.X2 - xDiff;
262      newClippingArea.Y1 = startClippingArea.Y1 - yDiff;
263      newClippingArea.Y2 = startClippingArea.Y2 - yDiff;
264
265      root.ClippingArea = newClippingArea;
266      panListener.StartPoint = startPoint;
267
268      zoomFullView = false; //user wants to pan => no full view
269
270      canvasUI1.Invalidate();
271    }
272
273    private void Zoom_OnMouseUp(Point startPoint, Point actualPoint) {
274      canvasUI1.MouseEventListener = null;
275
276      RectangleD newClippingArea = Transform.ToWorld(r, canvasUI1.ClientRectangle, root.ClippingArea);
277      root.ClippingArea = newClippingArea;
278      root.RemoveShape(rectangleShape);
279
280      zoomFullView = false; //user wants to pan => no full view
281
282      canvasUI1.Invalidate();
283    }
284
285    private Rectangle r;
286    private RectangleShape rectangleShape;
287
288    private void Zoom_OnMouseMove(Point startPoint, Point actualPoint) {
289      r = new Rectangle();
290
291      if (startPoint.X < actualPoint.X) {
292        r.X = startPoint.X;
293        r.Width = actualPoint.X - startPoint.X;
294      } else {
295        r.X = actualPoint.X;
296        r.Width = startPoint.X - actualPoint.X;
297      }
298
299      if (startPoint.Y < actualPoint.Y) {
300        r.Y = startPoint.Y;
301        r.Height = actualPoint.Y - startPoint.Y;
302      } else {
303        r.Y = actualPoint.Y;
304        r.Height = startPoint.Y - actualPoint.Y;
305      }
306
307      rectangleShape.Rectangle = Transform.ToWorld(r, canvasUI1.ClientRectangle, root.ClippingArea);
308      canvasUI1.Invalidate();
309    }
310  }
311}
Note: See TracBrowser for help on using the repository browser.