1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Visualization {
|
---|
6 | public partial class LineChart : ViewBase {
|
---|
7 | private readonly IChartDataRowsModel model;
|
---|
8 |
|
---|
9 | /// <summary>
|
---|
10 | /// This constructor shouldn't be called. Only required for the designer.
|
---|
11 | /// </summary>
|
---|
12 | public LineChart() {
|
---|
13 | InitializeComponent();
|
---|
14 | }
|
---|
15 |
|
---|
16 | /// <summary>
|
---|
17 | /// Initializes the chart.
|
---|
18 | /// </summary>
|
---|
19 | /// <param name="model">Referenz to the model, for data</param>
|
---|
20 | public LineChart(IChartDataRowsModel model) : this() {
|
---|
21 | if (model == null)
|
---|
22 | throw new NullReferenceException("Model cannot be null.");
|
---|
23 |
|
---|
24 | this.model = model;
|
---|
25 | this.Item = (IItem)model;
|
---|
26 |
|
---|
27 | //TODO: correct Rectangle to fit
|
---|
28 | RectangleD clientRectangle = new RectangleD(-1, -1, 11, 11);
|
---|
29 | canvasUI1.MainCanvas.WorldShape = new WorldShape(clientRectangle, clientRectangle);
|
---|
30 | }
|
---|
31 |
|
---|
32 | #region Add-/RemoveItemEvents
|
---|
33 |
|
---|
34 | protected override void AddItemEvents() {
|
---|
35 | base.AddItemEvents();
|
---|
36 |
|
---|
37 | model.DataRowAdded += OnDataRowAdded;
|
---|
38 | model.DataRowRemoved += OnDataRowRemoved;
|
---|
39 | model.ModelChanged += OnModelChanged;
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected override void RemoveItemEvents() {
|
---|
43 | base.RemoveItemEvents();
|
---|
44 |
|
---|
45 | model.DataRowAdded -= OnDataRowAdded;
|
---|
46 | model.DataRowRemoved -= OnDataRowRemoved;
|
---|
47 | model.ModelChanged -= OnModelChanged;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void OnDataRowAdded(IDataRow row) {
|
---|
51 | row.ValueChanged += OnRowValueChanged;
|
---|
52 | row.ValuesChanged += OnRowValuesChanged;
|
---|
53 |
|
---|
54 | InitShapes(row);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void InitShapes(IDataRow row) {
|
---|
58 | List<LineShape> lineShapes = new List<LineShape>();
|
---|
59 |
|
---|
60 | for (int i = 1; i < row.Count; i++) {
|
---|
61 | LineShape lineShape = new LineShape(i - 1, row[i - 1], i, row[i], 0, row.Color);
|
---|
62 | lineShapes.Add(lineShape);
|
---|
63 | canvasUI1.MainCanvas.WorldShape.AddShape(lineShape);
|
---|
64 | }
|
---|
65 |
|
---|
66 | rowToLineShapes[row] = lineShapes;
|
---|
67 |
|
---|
68 | canvasUI1.Invalidate();
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void OnDataRowRemoved(IDataRow row) {
|
---|
72 | row.ValueChanged -= OnRowValueChanged;
|
---|
73 | row.ValuesChanged -= OnRowValuesChanged;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private readonly IDictionary<IDataRow, List<LineShape>> rowToLineShapes = new Dictionary<IDataRow, List<LineShape>>();
|
---|
77 |
|
---|
78 | private void OnRowValueChanged(IDataRow row, double value, int index) {
|
---|
79 | List<LineShape> lineShapes = rowToLineShapes[row];
|
---|
80 |
|
---|
81 | if (index > lineShapes.Count+1)
|
---|
82 | throw new NotImplementedException();
|
---|
83 |
|
---|
84 | // new value was added
|
---|
85 | if (index > 0 && index == lineShapes.Count+1) {
|
---|
86 | LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], 0, row.Color);
|
---|
87 | lineShapes.Add(lineShape);
|
---|
88 | canvasUI1.MainCanvas.WorldShape.AddShape(lineShape);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // not the first value
|
---|
92 | if (index > 0)
|
---|
93 | lineShapes[index-1].Y2 = value;
|
---|
94 |
|
---|
95 | // not the last value
|
---|
96 | if (index > 0 && index < row.Count)
|
---|
97 | lineShapes[index].Y1 = value;
|
---|
98 |
|
---|
99 | canvasUI1.Invalidate();
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void OnRowValuesChanged(IDataRow row, double[] values, int index) {
|
---|
103 | foreach (double value in values) {
|
---|
104 | OnRowValueChanged(row, value, index++);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void OnModelChanged() {
|
---|
109 | }
|
---|
110 |
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region Begin-/EndUpdate
|
---|
114 |
|
---|
115 | private int beginUpdateCount = 0;
|
---|
116 |
|
---|
117 | public void BeginUpdate() {
|
---|
118 | beginUpdateCount++;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void EndUpdate() {
|
---|
122 | if (beginUpdateCount == 0)
|
---|
123 | throw new InvalidOperationException("Too many EndUpdates.");
|
---|
124 |
|
---|
125 | beginUpdateCount--;
|
---|
126 |
|
---|
127 | if (beginUpdateCount == 0)
|
---|
128 | Invalidate();
|
---|
129 | }
|
---|
130 |
|
---|
131 | #endregion
|
---|
132 | }
|
---|
133 | } |
---|