1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Drawing.Drawing2D;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Visualization.Legend;
|
---|
8 | using HeuristicLab.Visualization.Options;
|
---|
9 | using HeuristicLab.Visualization.Test;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Visualization {
|
---|
12 | public partial class LineChart : ViewBase {
|
---|
13 | private readonly IChartDataRowsModel model;
|
---|
14 | private readonly Canvas canvas;
|
---|
15 |
|
---|
16 | private readonly TextShape titleShape = new TextShape("Title");
|
---|
17 | private readonly LegendShape legendShape = new LegendShape();
|
---|
18 | private readonly XAxis xAxis = new XAxis();
|
---|
19 | private readonly List<RowEntry> rowEntries = new List<RowEntry>();
|
---|
20 |
|
---|
21 | private readonly Dictionary<IDataRow, RowEntry> rowToRowEntry = new Dictionary<IDataRow, RowEntry>();
|
---|
22 |
|
---|
23 | private readonly ViewSettings viewSettings;
|
---|
24 |
|
---|
25 | private readonly WorldShape userInteractionShape = new WorldShape();
|
---|
26 | private readonly RectangleShape rectangleShape = new RectangleShape(0, 0, 0, 0, Color.FromArgb(50, 0, 0, 255));
|
---|
27 | private IMouseEventListener mouseEventListener;
|
---|
28 |
|
---|
29 | private const int YAxisWidth = 100;
|
---|
30 | private const int XAxisHeight = 40;
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// This constructor shouldn't be called. Only required for the designer.
|
---|
34 | /// </summary>
|
---|
35 | public LineChart() {
|
---|
36 | InitializeComponent();
|
---|
37 | }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Initializes the chart.
|
---|
41 | /// </summary>
|
---|
42 | /// <param name="model">Referenz to the model, for data</param>
|
---|
43 | public LineChart(IChartDataRowsModel model) : this() {
|
---|
44 | if (model == null) {
|
---|
45 | throw new NullReferenceException("Model cannot be null.");
|
---|
46 | }
|
---|
47 |
|
---|
48 | canvas = canvasUI.Canvas;
|
---|
49 |
|
---|
50 | this.model = model;
|
---|
51 | viewSettings = model.ViewSettings;
|
---|
52 | viewSettings.OnUpdateSettings += UpdateViewSettings;
|
---|
53 |
|
---|
54 | Item = model;
|
---|
55 |
|
---|
56 | UpdateLayout();
|
---|
57 | canvasUI.Resize += delegate { UpdateLayout(); };
|
---|
58 |
|
---|
59 | ZoomToFullView();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// updates the view settings
|
---|
64 | /// </summary>
|
---|
65 | private void UpdateViewSettings() {
|
---|
66 | titleShape.Font = viewSettings.TitleFont;
|
---|
67 | titleShape.Color = viewSettings.TitleColor;
|
---|
68 |
|
---|
69 | legendShape.Font = viewSettings.LegendFont;
|
---|
70 | legendShape.Color = viewSettings.LegendColor;
|
---|
71 |
|
---|
72 | xAxis.Font = viewSettings.XAxisFont;
|
---|
73 | xAxis.Color = viewSettings.XAxisColor;
|
---|
74 |
|
---|
75 | SetLegendPosition();
|
---|
76 |
|
---|
77 | canvasUI.Invalidate();
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Layout management - arranges the inner shapes.
|
---|
82 | /// </summary>
|
---|
83 | private void UpdateLayout() {
|
---|
84 | canvas.ClearShapes();
|
---|
85 |
|
---|
86 | titleShape.Text = model.Title;
|
---|
87 |
|
---|
88 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
89 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
90 | if (yAxisDescriptor.ShowGrid) {
|
---|
91 | info.Grid.Color = yAxisDescriptor.GridColor;
|
---|
92 | canvas.AddShape(info.Grid);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
97 | canvas.AddShape(rowEntry.LinesShape);
|
---|
98 | }
|
---|
99 |
|
---|
100 | xAxis.ShowLabel = model.ShowXAxisLabel;
|
---|
101 | xAxis.Label = model.XAxisLabel;
|
---|
102 |
|
---|
103 | canvas.AddShape(xAxis);
|
---|
104 |
|
---|
105 | int yAxesWidthLeft = 0;
|
---|
106 | int yAxesWidthRight = 0;
|
---|
107 |
|
---|
108 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
109 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
110 | if (yAxisDescriptor.ShowYAxis) {
|
---|
111 | canvas.AddShape(info.YAxis);
|
---|
112 | info.YAxis.ShowLabel = yAxisDescriptor.ShowYAxisLabel;
|
---|
113 | info.YAxis.Label = yAxisDescriptor.Label;
|
---|
114 | info.YAxis.Position = yAxisDescriptor.Position;
|
---|
115 | switch (yAxisDescriptor.Position) {
|
---|
116 | case AxisPosition.Left:
|
---|
117 | yAxesWidthLeft += YAxisWidth;
|
---|
118 | break;
|
---|
119 | case AxisPosition.Right:
|
---|
120 | yAxesWidthRight += YAxisWidth;
|
---|
121 | break;
|
---|
122 | default:
|
---|
123 | throw new NotImplementedException();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | canvas.AddShape(titleShape);
|
---|
129 | canvas.AddShape(legendShape);
|
---|
130 |
|
---|
131 | canvas.AddShape(userInteractionShape);
|
---|
132 |
|
---|
133 | titleShape.X = 10;
|
---|
134 | titleShape.Y = canvasUI.Height - 10;
|
---|
135 |
|
---|
136 | RectangleD linesAreaBoundingBox = new RectangleD(yAxesWidthLeft,
|
---|
137 | XAxisHeight,
|
---|
138 | canvasUI.Width - yAxesWidthRight,
|
---|
139 | canvasUI.Height);
|
---|
140 |
|
---|
141 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
142 | rowEntry.LinesShape.BoundingBox = linesAreaBoundingBox;
|
---|
143 | }
|
---|
144 |
|
---|
145 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
146 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
147 | info.Grid.BoundingBox = linesAreaBoundingBox;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int yAxisLeft = 0;
|
---|
151 | int yAxisRight = (int)linesAreaBoundingBox.X2;
|
---|
152 |
|
---|
153 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
154 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
155 | if (yAxisDescriptor.ShowYAxis) {
|
---|
156 | switch (yAxisDescriptor.Position) {
|
---|
157 | case AxisPosition.Left:
|
---|
158 | info.YAxis.BoundingBox = new RectangleD(yAxisLeft,
|
---|
159 | linesAreaBoundingBox.Y1,
|
---|
160 | yAxisLeft + YAxisWidth,
|
---|
161 | linesAreaBoundingBox.Y2);
|
---|
162 | yAxisLeft += YAxisWidth;
|
---|
163 | break;
|
---|
164 | case AxisPosition.Right:
|
---|
165 | info.YAxis.BoundingBox = new RectangleD(yAxisRight,
|
---|
166 | linesAreaBoundingBox.Y1,
|
---|
167 | yAxisRight + YAxisWidth,
|
---|
168 | linesAreaBoundingBox.Y2);
|
---|
169 | yAxisRight += YAxisWidth;
|
---|
170 | break;
|
---|
171 | default:
|
---|
172 | throw new NotImplementedException();
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | userInteractionShape.BoundingBox = linesAreaBoundingBox;
|
---|
178 | userInteractionShape.ClippingArea = new RectangleD(0, 0, userInteractionShape.BoundingBox.Width, userInteractionShape.BoundingBox.Height);
|
---|
179 |
|
---|
180 | xAxis.BoundingBox = new RectangleD(linesAreaBoundingBox.X1,
|
---|
181 | 0,
|
---|
182 | linesAreaBoundingBox.X2,
|
---|
183 | linesAreaBoundingBox.Y1);
|
---|
184 |
|
---|
185 | SetLegendPosition();
|
---|
186 | }
|
---|
187 |
|
---|
188 | private readonly Dictionary<YAxisDescriptor, YAxisInfo> yAxisInfos = new Dictionary<YAxisDescriptor, YAxisInfo>();
|
---|
189 |
|
---|
190 | private YAxisInfo GetYAxisInfo(YAxisDescriptor yAxisDescriptor) {
|
---|
191 | YAxisInfo info;
|
---|
192 |
|
---|
193 | if (!yAxisInfos.TryGetValue(yAxisDescriptor, out info)) {
|
---|
194 | info = new YAxisInfo();
|
---|
195 | yAxisInfos[yAxisDescriptor] = info;
|
---|
196 | }
|
---|
197 |
|
---|
198 | return info;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /// <summary>
|
---|
202 | /// sets the legend position
|
---|
203 | /// </summary>
|
---|
204 | private void SetLegendPosition() {
|
---|
205 | switch (viewSettings.LegendPosition) {
|
---|
206 | case LegendPosition.Bottom:
|
---|
207 | setLegendBottom();
|
---|
208 | break;
|
---|
209 |
|
---|
210 | case LegendPosition.Top:
|
---|
211 | setLegendTop();
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case LegendPosition.Left:
|
---|
215 | setLegendLeft();
|
---|
216 | break;
|
---|
217 |
|
---|
218 | case LegendPosition.Right:
|
---|
219 | setLegendRight();
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void setLegendRight() {
|
---|
225 | // legend right
|
---|
226 | legendShape.BoundingBox = new RectangleD(canvasUI.Width - legendShape.GetMaxLabelLength(), 10, canvasUI.Width, canvasUI.Height - 50);
|
---|
227 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
228 | legendShape.Row = false;
|
---|
229 | legendShape.CreateLegend();
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void setLegendLeft() {
|
---|
233 | // legend left
|
---|
234 | legendShape.BoundingBox = new RectangleD(10, 10, canvasUI.Width, canvasUI.Height - 50);
|
---|
235 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
236 | legendShape.Row = false;
|
---|
237 | legendShape.CreateLegend();
|
---|
238 |
|
---|
239 | canvasUI.Invalidate();
|
---|
240 | }
|
---|
241 |
|
---|
242 | public void setLegendTop() {
|
---|
243 | // legend top
|
---|
244 | legendShape.BoundingBox = new RectangleD(100, canvasUI.Height - canvasUI.Height, canvasUI.Width, canvasUI.Height - 10);
|
---|
245 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
246 | legendShape.Row = true;
|
---|
247 | legendShape.Top = true;
|
---|
248 | legendShape.CreateLegend();
|
---|
249 | }
|
---|
250 |
|
---|
251 | public void setLegendBottom() {
|
---|
252 | // legend bottom
|
---|
253 | legendShape.BoundingBox = new RectangleD(100, 10, canvasUI.Width, canvasUI.Height /*legendShape.GetHeight4Rows()*/);
|
---|
254 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
255 | legendShape.Row = true;
|
---|
256 | legendShape.Top = false;
|
---|
257 | legendShape.CreateLegend();
|
---|
258 | }
|
---|
259 |
|
---|
260 | private void optionsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
261 | OptionsDialog optionsdlg = new OptionsDialog(model);
|
---|
262 | optionsdlg.Show();
|
---|
263 | }
|
---|
264 |
|
---|
265 | public void OnDataRowChanged(IDataRow row) {
|
---|
266 | RowEntry rowEntry = rowToRowEntry[row];
|
---|
267 |
|
---|
268 | rowEntry.LinesShape.UpdateStyle(row);
|
---|
269 |
|
---|
270 | UpdateLayout();
|
---|
271 |
|
---|
272 | canvasUI.Invalidate();
|
---|
273 | }
|
---|
274 |
|
---|
275 | #region Add-/RemoveItemEvents
|
---|
276 |
|
---|
277 | protected override void AddItemEvents() {
|
---|
278 | base.AddItemEvents();
|
---|
279 |
|
---|
280 | model.DataRowAdded += OnDataRowAdded;
|
---|
281 | model.DataRowRemoved += OnDataRowRemoved;
|
---|
282 | model.ModelChanged += OnModelChanged;
|
---|
283 |
|
---|
284 | foreach (IDataRow row in model.Rows) {
|
---|
285 | OnDataRowAdded(row);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | protected override void RemoveItemEvents() {
|
---|
290 | base.RemoveItemEvents();
|
---|
291 |
|
---|
292 | model.DataRowAdded -= OnDataRowAdded;
|
---|
293 | model.DataRowRemoved -= OnDataRowRemoved;
|
---|
294 | model.ModelChanged -= OnModelChanged;
|
---|
295 | }
|
---|
296 |
|
---|
297 | private void OnDataRowAdded(IDataRow row) {
|
---|
298 | row.ValueChanged += OnRowValueChanged;
|
---|
299 | row.ValuesChanged += OnRowValuesChanged;
|
---|
300 | row.DataRowChanged += OnDataRowChanged;
|
---|
301 |
|
---|
302 | legendShape.AddLegendItem(new LegendItem(row.Label, row.Color, row.Thickness));
|
---|
303 | legendShape.CreateLegend();
|
---|
304 |
|
---|
305 | InitLineShapes(row);
|
---|
306 |
|
---|
307 | UpdateLayout();
|
---|
308 | }
|
---|
309 |
|
---|
310 | private void OnDataRowRemoved(IDataRow row) {
|
---|
311 | row.ValueChanged -= OnRowValueChanged;
|
---|
312 | row.ValuesChanged -= OnRowValuesChanged;
|
---|
313 | row.DataRowChanged -= OnDataRowChanged;
|
---|
314 |
|
---|
315 | rowToRowEntry.Remove(row);
|
---|
316 | rowEntries.RemoveAll(delegate(RowEntry rowEntry) { return rowEntry.DataRow == row; });
|
---|
317 |
|
---|
318 | UpdateLayout();
|
---|
319 | }
|
---|
320 |
|
---|
321 | #endregion
|
---|
322 |
|
---|
323 | public void ZoomToFullView() {
|
---|
324 | SetClipX(-0.1, model.MaxDataRowValues - 0.9);
|
---|
325 |
|
---|
326 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
327 | YAxisDescriptor yAxisDescriptor = rowEntry.DataRow.YAxis;
|
---|
328 |
|
---|
329 | SetClipY(rowEntry,
|
---|
330 | yAxisDescriptor.MinValue - ((yAxisDescriptor.MaxValue - yAxisDescriptor.MinValue)*0.05),
|
---|
331 | yAxisDescriptor.MaxValue + ((yAxisDescriptor.MaxValue - yAxisDescriptor.MinValue)*0.05));
|
---|
332 | }
|
---|
333 |
|
---|
334 | canvasUI.Invalidate();
|
---|
335 | }
|
---|
336 |
|
---|
337 | private void SetClipX(double x1, double x2) {
|
---|
338 | xAxis.ClippingArea = new RectangleD(x1,
|
---|
339 | 0,
|
---|
340 | x2,
|
---|
341 | XAxisHeight);
|
---|
342 |
|
---|
343 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
344 | rowEntry.LinesShape.ClippingArea = new RectangleD(x1,
|
---|
345 | rowEntry.LinesShape.ClippingArea.Y1,
|
---|
346 | x2,
|
---|
347 | rowEntry.LinesShape.ClippingArea.Y2);
|
---|
348 | }
|
---|
349 |
|
---|
350 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
351 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
352 | info.Grid.ClippingArea = new RectangleD(x1,
|
---|
353 | info.Grid.ClippingArea.Y1,
|
---|
354 | x2,
|
---|
355 | info.Grid.ClippingArea.Y2);
|
---|
356 | info.YAxis.ClippingArea = new RectangleD(0,
|
---|
357 | info.YAxis.ClippingArea.Y1,
|
---|
358 | YAxisWidth,
|
---|
359 | info.YAxis.ClippingArea.Y2);
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | private void SetClipY(RowEntry rowEntry, double y1, double y2) {
|
---|
364 | rowEntry.LinesShape.ClippingArea = new RectangleD(rowEntry.LinesShape.ClippingArea.X1,
|
---|
365 | y1,
|
---|
366 | rowEntry.LinesShape.ClippingArea.X2,
|
---|
367 | y2);
|
---|
368 |
|
---|
369 | YAxisInfo info = GetYAxisInfo(rowEntry.DataRow.YAxis);
|
---|
370 |
|
---|
371 | info.Grid.ClippingArea = new RectangleD(info.Grid.ClippingArea.X1,
|
---|
372 | y1,
|
---|
373 | info.Grid.ClippingArea.X2,
|
---|
374 | y2);
|
---|
375 | info.YAxis.ClippingArea = new RectangleD(info.YAxis.ClippingArea.X1,
|
---|
376 | y1,
|
---|
377 | info.YAxis.ClippingArea.X2,
|
---|
378 | y2);
|
---|
379 | }
|
---|
380 |
|
---|
381 | private void InitLineShapes(IDataRow row) {
|
---|
382 | RowEntry rowEntry = new RowEntry(row);
|
---|
383 | rowEntries.Add(rowEntry);
|
---|
384 | rowToRowEntry[row] = rowEntry;
|
---|
385 |
|
---|
386 | if ((row.LineType == DataRowType.SingleValue)) {
|
---|
387 | if (row.Count > 0) {
|
---|
388 | LineShape lineShape = new HorizontalLineShape(0, row[0], double.MaxValue, row[0], row.Color, row.Thickness,
|
---|
389 | row.Style);
|
---|
390 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
391 | }
|
---|
392 | } else {
|
---|
393 | rowEntry.showMarkers(row.ShowMarkers);
|
---|
394 | for (int i = 1; i < row.Count; i++) {
|
---|
395 | LineShape lineShape = new LineShape(i - 1, row[i - 1], i, row[i], row.Color, row.Thickness, row.Style);
|
---|
396 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
397 | rowEntry.LinesShape.AddMarkerShape(new MarkerShape(i - 1, row[i - 1], 8, row.Color));
|
---|
398 | }
|
---|
399 | if (row.Count > 0) {
|
---|
400 | rowEntry.LinesShape.AddMarkerShape(new MarkerShape((row.Count - 1), row[(row.Count - 1)], 8, row.Color));
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | ZoomToFullView();
|
---|
405 | }
|
---|
406 |
|
---|
407 | private void OnRowValueChanged(IDataRow row, double value, int index, Action action) {
|
---|
408 | RowEntry rowEntry = rowToRowEntry[row];
|
---|
409 |
|
---|
410 | if (row.LineType == DataRowType.SingleValue) {
|
---|
411 | if (action == Action.Added) {
|
---|
412 | LineShape lineShape = new HorizontalLineShape(0, row[0], double.MaxValue, row[0], row.Color, row.Thickness,
|
---|
413 | row.Style);
|
---|
414 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
415 | } else {
|
---|
416 | LineShape lineShape = rowEntry.LinesShape.GetShape(0);
|
---|
417 | lineShape.Y1 = value;
|
---|
418 | lineShape.Y2 = value;
|
---|
419 | }
|
---|
420 | } else {
|
---|
421 | if (index > rowEntry.LinesShape.Count + 1) {
|
---|
422 | //MarkersShape is on position zero
|
---|
423 | throw new NotImplementedException();
|
---|
424 | }
|
---|
425 |
|
---|
426 | if (action == Action.Added) {
|
---|
427 | // new value was added
|
---|
428 | if (index > 0 && index == rowEntry.LinesShape.Count + 1) {
|
---|
429 | LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], row.Color, row.Thickness,
|
---|
430 | row.Style);
|
---|
431 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
432 | rowEntry.LinesShape.AddMarkerShape(new MarkerShape(index, row[index], 8, row.Color));
|
---|
433 | }
|
---|
434 | } else if (action == Action.Modified) {
|
---|
435 | // not the first value
|
---|
436 | if (index > 0) {
|
---|
437 | rowEntry.LinesShape.GetShape(index - 1).Y2 = value;
|
---|
438 | ((MarkerShape)rowEntry.LinesShape.markersShape.GetShape(index - 1)).Y = value;
|
---|
439 | }
|
---|
440 |
|
---|
441 | // not the last value
|
---|
442 | if (index > 0 && index < row.Count - 1) {
|
---|
443 | rowEntry.LinesShape.GetShape(index).Y1 = value;
|
---|
444 | ((MarkerShape)rowEntry.LinesShape.markersShape.GetShape(index)).Y = value;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | ZoomToFullView();
|
---|
450 | }
|
---|
451 |
|
---|
452 | private void OnRowValuesChanged(IDataRow row, double[] values, int index, Action action) {
|
---|
453 | foreach (double value in values) {
|
---|
454 | OnRowValueChanged(row, value, index++, action);
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | private void OnModelChanged() {
|
---|
459 | canvasUI.Invalidate();
|
---|
460 | }
|
---|
461 |
|
---|
462 | #region Begin-/EndUpdate
|
---|
463 |
|
---|
464 | private int beginUpdateCount;
|
---|
465 |
|
---|
466 | public void BeginUpdate() {
|
---|
467 | beginUpdateCount++;
|
---|
468 | }
|
---|
469 |
|
---|
470 | public void EndUpdate() {
|
---|
471 | if (beginUpdateCount == 0) {
|
---|
472 | throw new InvalidOperationException("Too many EndUpdates.");
|
---|
473 | }
|
---|
474 |
|
---|
475 | beginUpdateCount--;
|
---|
476 |
|
---|
477 | if (beginUpdateCount == 0) {
|
---|
478 | canvasUI.Invalidate();
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 | #endregion
|
---|
483 |
|
---|
484 | #region Zooming / Panning
|
---|
485 |
|
---|
486 | private void Pan(Point startPoint, Point endPoint) {
|
---|
487 | RectangleD clippingArea = Translate.ClippingArea(startPoint, endPoint, xAxis.ClippingArea, xAxis.Viewport);
|
---|
488 |
|
---|
489 | SetClipX(clippingArea.X1, clippingArea.X2);
|
---|
490 |
|
---|
491 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
492 | if (rowEntry.DataRow.YAxis.ClipChangeable) {
|
---|
493 | clippingArea = Translate.ClippingArea(startPoint, endPoint, rowEntry.LinesShape.ClippingArea, rowEntry.LinesShape.Viewport);
|
---|
494 | SetClipY(rowEntry, clippingArea.Y1, clippingArea.Y2);
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | canvasUI.Invalidate();
|
---|
499 | }
|
---|
500 |
|
---|
501 | private void PanEnd(Point startPoint, Point endPoint) {
|
---|
502 | Pan(startPoint, endPoint);
|
---|
503 | }
|
---|
504 |
|
---|
505 | private void SetClippingArea(Rectangle rectangle) {
|
---|
506 | RectangleD clippingArea = Transform.ToWorld(rectangle, xAxis.Viewport, xAxis.ClippingArea);
|
---|
507 |
|
---|
508 | SetClipX(clippingArea.X1, clippingArea.X2);
|
---|
509 |
|
---|
510 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
511 | if (rowEntry.DataRow.YAxis.ClipChangeable) {
|
---|
512 | clippingArea = Transform.ToWorld(rectangle, rowEntry.LinesShape.Viewport, rowEntry.LinesShape.ClippingArea);
|
---|
513 |
|
---|
514 | SetClipY(rowEntry, clippingArea.Y1, clippingArea.Y2);
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | userInteractionShape.RemoveShape(rectangleShape);
|
---|
519 | canvasUI.Invalidate();
|
---|
520 | }
|
---|
521 |
|
---|
522 | private void DrawRectangle(Rectangle rectangle) {
|
---|
523 | rectangleShape.Rectangle = Transform.ToWorld(rectangle, userInteractionShape.Viewport, userInteractionShape.ClippingArea);
|
---|
524 | canvasUI.Invalidate();
|
---|
525 | }
|
---|
526 |
|
---|
527 | private void canvasUI1_KeyDown(object sender, KeyEventArgs e) {}
|
---|
528 |
|
---|
529 | private void canvasUI1_MouseDown(object sender, MouseEventArgs e) {
|
---|
530 | Focus();
|
---|
531 |
|
---|
532 | if (e.Button == MouseButtons.Right) {
|
---|
533 | contextMenuStrip1.Show(PointToScreen(e.Location));
|
---|
534 | } else if (e.Button == MouseButtons.Left) {
|
---|
535 | if (ModifierKeys == Keys.None) {
|
---|
536 | PanListener panListener = new PanListener(e.Location);
|
---|
537 | panListener.Pan += Pan;
|
---|
538 | panListener.PanEnd += PanEnd;
|
---|
539 |
|
---|
540 | mouseEventListener = panListener;
|
---|
541 | } else if (ModifierKeys == Keys.Control) {
|
---|
542 | ZoomListener zoomListener = new ZoomListener(e.Location);
|
---|
543 | zoomListener.DrawRectangle += DrawRectangle;
|
---|
544 | zoomListener.SetClippingArea += SetClippingArea;
|
---|
545 |
|
---|
546 | rectangleShape.Rectangle = RectangleD.Empty;
|
---|
547 | userInteractionShape.AddShape(rectangleShape);
|
---|
548 |
|
---|
549 | mouseEventListener = zoomListener;
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | private void canvasUI_MouseMove(object sender, MouseEventArgs e) {
|
---|
555 | if (mouseEventListener != null) {
|
---|
556 | mouseEventListener.MouseMove(sender, e);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | private void canvasUI_MouseUp(object sender, MouseEventArgs e) {
|
---|
561 | if (mouseEventListener != null) {
|
---|
562 | mouseEventListener.MouseUp(sender, e);
|
---|
563 | }
|
---|
564 |
|
---|
565 | mouseEventListener = null;
|
---|
566 | }
|
---|
567 |
|
---|
568 | private void canvasUI1_MouseWheel(object sender, MouseEventArgs e) {
|
---|
569 | if (ModifierKeys == Keys.Control) {
|
---|
570 | double zoomFactor = (e.Delta > 0) ? 0.7 : 1.3;
|
---|
571 |
|
---|
572 | PointD world;
|
---|
573 |
|
---|
574 | world = Transform.ToWorld(e.Location, xAxis.Viewport, xAxis.ClippingArea);
|
---|
575 |
|
---|
576 | double x1 = world.X - (world.X - xAxis.ClippingArea.X1)*zoomFactor;
|
---|
577 | double x2 = world.X + (xAxis.ClippingArea.X2 - world.X)*zoomFactor;
|
---|
578 |
|
---|
579 | SetClipX(x1, x2);
|
---|
580 |
|
---|
581 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
582 | world = Transform.ToWorld(e.Location, rowEntry.LinesShape.Viewport, rowEntry.LinesShape.ClippingArea);
|
---|
583 |
|
---|
584 | double y1 = world.Y - (world.Y - rowEntry.LinesShape.ClippingArea.Y1)*zoomFactor;
|
---|
585 | double y2 = world.Y + (rowEntry.LinesShape.ClippingArea.Y2 - world.Y)*zoomFactor;
|
---|
586 |
|
---|
587 | SetClipY(rowEntry, y1, y2);
|
---|
588 | }
|
---|
589 |
|
---|
590 | canvasUI.Invalidate();
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 | #endregion
|
---|
595 |
|
---|
596 | private class LinesShape : WorldShape {
|
---|
597 | public readonly CompositeShape markersShape = new CompositeShape();
|
---|
598 |
|
---|
599 | public void UpdateStyle(IDataRow row) {
|
---|
600 | foreach (IShape shape in shapes) {
|
---|
601 | LineShape lineShape = shape as LineShape;
|
---|
602 | if (lineShape != null) {
|
---|
603 | lineShape.LSColor = row.Color;
|
---|
604 | lineShape.LSDrawingStyle = row.Style;
|
---|
605 | lineShape.LSThickness = row.Thickness;
|
---|
606 | }
|
---|
607 | }
|
---|
608 | this.markersShape.ShowChildShapes = row.ShowMarkers;
|
---|
609 | }
|
---|
610 |
|
---|
611 | public override void Draw(Graphics graphics) {
|
---|
612 | GraphicsState gstate = graphics.Save();
|
---|
613 |
|
---|
614 | graphics.SetClip(Viewport);
|
---|
615 | foreach (IShape shape in shapes) {
|
---|
616 | // draw child shapes using our own clipping area
|
---|
617 | shape.Draw(graphics);
|
---|
618 | }
|
---|
619 | markersShape.Draw(graphics);
|
---|
620 | graphics.Restore(gstate);
|
---|
621 | }
|
---|
622 |
|
---|
623 | public void AddMarkerShape(IShape shape) {
|
---|
624 | shape.Parent = this;
|
---|
625 | markersShape.AddShape(shape);
|
---|
626 | }
|
---|
627 |
|
---|
628 | public int Count {
|
---|
629 | get { return shapes.Count; }
|
---|
630 | }
|
---|
631 |
|
---|
632 | public LineShape GetShape(int index) {
|
---|
633 | return (LineShape)shapes[index]; //shapes[0] is markersShape!!
|
---|
634 | }
|
---|
635 | }
|
---|
636 |
|
---|
637 | private class RowEntry {
|
---|
638 | private readonly IDataRow dataRow;
|
---|
639 |
|
---|
640 | private readonly LinesShape linesShape = new LinesShape();
|
---|
641 |
|
---|
642 | public RowEntry(IDataRow dataRow) {
|
---|
643 | this.dataRow = dataRow;
|
---|
644 | linesShape.markersShape.Parent = linesShape;
|
---|
645 | }
|
---|
646 |
|
---|
647 | public IDataRow DataRow {
|
---|
648 | get { return dataRow; }
|
---|
649 | }
|
---|
650 |
|
---|
651 | public LinesShape LinesShape {
|
---|
652 | get { return linesShape; }
|
---|
653 | }
|
---|
654 |
|
---|
655 | public void showMarkers(bool flag) {
|
---|
656 | linesShape.markersShape.ShowChildShapes = flag;
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 | private class YAxisInfo {
|
---|
661 | private readonly Grid grid = new Grid();
|
---|
662 | private readonly YAxis yAxis = new YAxis();
|
---|
663 |
|
---|
664 | public Grid Grid {
|
---|
665 | get { return grid; }
|
---|
666 | }
|
---|
667 |
|
---|
668 | public YAxis YAxis {
|
---|
669 | get { return yAxis; }
|
---|
670 | }
|
---|
671 | }
|
---|
672 | }
|
---|
673 | } |
---|