Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adjustments on LineChart for new interface. #345

File size: 4.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Core;
4
5namespace 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      //TODO: correct Rectangle to fit
25      RectangleD clientRectangle = new RectangleD(-1, -1, 11, 11);
26      canvasUI1.MainCanvas.WorldShape = new WorldShape(clientRectangle, clientRectangle);
27
28      this.model = model;
29      this.Item = (IItem)model;
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      foreach (IDataRow row in model.Rows) {
42        OnDataRowAdded(row);
43      }
44    }
45
46    protected override void RemoveItemEvents() {
47      base.RemoveItemEvents();
48
49      model.DataRowAdded -= OnDataRowAdded;
50      model.DataRowRemoved -= OnDataRowRemoved;
51      model.ModelChanged -= OnModelChanged;
52    }
53
54    private void OnDataRowAdded(IDataRow row) {
55      row.ValueChanged += OnRowValueChanged;
56      row.ValuesChanged += OnRowValuesChanged;
57
58      InitShapes(row);
59    }
60
61    private void InitShapes(IDataRow row) {
62      List<LineShape> lineShapes = new List<LineShape>();
63
64      for (int i = 1; i < row.Count; i++) {
65        LineShape lineShape = new LineShape(i - 1, row[i - 1], i, row[i], 0, row.Color, row.Thickness);
66        lineShapes.Add(lineShape);
67        // TODO each DataRow needs its own WorldShape so Y Axes can be zoomed independently.
68        canvasUI1.MainCanvas.WorldShape.AddShape(lineShape);
69      }
70
71      rowToLineShapes[row] = lineShapes;
72
73      canvasUI1.Invalidate();
74    }
75
76    private void OnDataRowRemoved(IDataRow row) {
77      row.ValueChanged -= OnRowValueChanged;
78      row.ValuesChanged -= OnRowValuesChanged;
79    }
80
81    private readonly IDictionary<IDataRow, List<LineShape>> rowToLineShapes = new Dictionary<IDataRow, List<LineShape>>();
82
83    // TODO use action parameter
84    private void OnRowValueChanged(IDataRow row, double value, int index, Action action) {
85      List<LineShape> lineShapes = rowToLineShapes[row];
86
87      if (index > lineShapes.Count+1)
88        throw new NotImplementedException();
89
90      // new value was added
91      if (index > 0 && index == lineShapes.Count+1) {
92        LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], 0, row.Color, row.Thickness);
93        lineShapes.Add(lineShape);
94        // TODO each DataRow needs its own WorldShape so Y Axes can be zoomed independently.
95        canvasUI1.MainCanvas.WorldShape.AddShape(lineShape);
96      }
97
98      // not the first value
99      if (index > 0)
100        lineShapes[index-1].Y2 = value;
101
102      // not the last value
103      if (index > 0 && index < row.Count-1)
104        lineShapes[index].Y1 = value;
105
106      canvasUI1.Invalidate();
107    }
108
109    // TODO use action parameter
110    private void OnRowValuesChanged(IDataRow row, double[] values, int index, Action action) {
111      foreach (double value in values) {
112        OnRowValueChanged(row, value, index++, action);
113      }
114    }
115
116    private void OnModelChanged() {
117    }
118
119    #endregion
120
121    #region Begin-/EndUpdate
122
123    private int beginUpdateCount = 0;
124
125    public void BeginUpdate() {
126      beginUpdateCount++;
127    }
128
129    public void EndUpdate() {
130      if (beginUpdateCount == 0)
131        throw new InvalidOperationException("Too many EndUpdates.");
132
133      beginUpdateCount--;
134
135      if (beginUpdateCount == 0)
136        Invalidate();
137    }
138
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.