Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 757 was 754, checked in by dwagner, 16 years ago

#345 First implementation of LineChart

File size: 4.9 KB
Line 
1using System;
2using System.Drawing;
3using HeuristicLab.Core;
4
5namespace HeuristicLab.Visualization
6{
7  public partial class LineChart : ViewBase
8  {
9    private readonly IChartDataRowsModel model;
10    private Color[] lineColors;
11
12    /// <summary>
13    /// This constructor shouldn't be called. Only required for the designer.
14    /// </summary>
15    public LineChart()
16    {
17      InitializeComponent();
18    }
19
20    /// <summary>
21    /// Initializes the chart.
22    /// </summary>
23    /// <param name="model">Referenz to the model, for data</param>
24    public LineChart(IChartDataRowsModel model) : this()
25    {
26      if (model == null)
27      {
28        throw new NullReferenceException("Model cannot be null.");
29      }
30
31      this.model = model;
32
33      //TODO: read line colors instead of static ones
34      lineColors = new Color[3];
35      lineColors[0] = Color.Red;
36      lineColors[1] = Color.Green;
37      lineColors[2] = Color.Blue;
38     
39      //TODO: correct Rectangle to fit
40      RectangleD clientRectangle = new RectangleD(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Right,
41                                                  ClientRectangle.Bottom);
42      canvasUI1.MainCanvas.WorldShape = new WorldShape(clientRectangle, clientRectangle);
43      Reset();
44    }
45
46    /// <summary>
47    /// Resets the line chart by deleting all shapes and reloading all data from the model.
48    /// </summary>
49    private void Reset()
50    {
51      BeginUpdate();
52
53      // TODO clear existing shapes
54
55      WorldShape mainWorld = canvasUI1.MainCanvas.WorldShape;
56
57      double spacing = mainWorld.BoundingBox.Width/model.Columns.Count;
58      double oldX = 0;
59      double currentX = spacing;
60      ChartDataRowsModelColumn oldColumn = null;
61      // reload data from the model and create shapes
62      foreach (ChartDataRowsModelColumn column in model.Columns)
63      {
64        if (oldColumn != null)
65        {
66          if (column.Values != null)
67          {
68            for (int i = 0; i < column.Values.Length; i++)
69            {
70              LineShape line = new LineShape(oldX, oldColumn.Values[i], currentX, column.Values[i], 0, lineColors[i]);
71              mainWorld.AddShape(line);
72            }
73            oldX = currentX;
74            currentX += spacing;
75          }
76          oldColumn = column;
77        }
78        else
79        {
80          oldColumn = column;
81        }
82
83        canvasUI1.Invalidate();
84
85        //   AddColumn(column.ColumnId, column.Values);
86      }
87
88      EndUpdate();
89    }
90
91    /// <summary>
92    /// Event handler which gets called when data in the model changes.
93    /// </summary>
94    /// <param name="type">Type of change</param>
95    /// <param name="columnId">Id of the changed column</param>
96    /// <param name="values">Values contained within the changed column</param>
97    private void OnDataChanged(ChangeType type, long columnId, double[] values)
98    {
99      switch (type)
100      {
101        case ChangeType.Add:
102          AddColumn(columnId, values);
103          break;
104        case ChangeType.Modify:
105          ModifyColumn(columnId, values);
106          break;
107        case ChangeType.Remove:
108          RemoveColumn(columnId);
109          break;
110        default:
111          throw new ArgumentOutOfRangeException("type");
112      }
113    }
114
115    /// <summary>
116    /// Adds a new column to the chart.
117    /// </summary>
118    /// <param name="columnId">Id of the column</param>
119    /// <param name="values">Values of the column</param>
120    private void AddColumn(long columnId, double[] values)
121    {
122      throw new NotImplementedException();
123    }
124
125    /// <summary>
126    /// Modifies an existing column of the chart.
127    /// </summary>
128    /// <param name="columnId">Id of the column</param>
129    /// <param name="values">Values of the column</param>
130    private void ModifyColumn(long columnId, double[] values)
131    {
132      throw new NotImplementedException();
133    }
134
135    /// <summary>
136    /// Removes a column from the chart.
137    /// </summary>
138    /// <param name="columnId">Id of the column</param>
139    private void RemoveColumn(long columnId)
140    {
141      throw new NotImplementedException();
142    }
143
144    #region Add-/RemoveItemEvents
145
146    protected override void AddItemEvents()
147    {
148      base.AddItemEvents();
149      model.ColumnChanged += OnDataChanged;
150    }
151
152    protected override void RemoveItemEvents()
153    {
154      base.RemoveItemEvents();
155      model.ColumnChanged -= OnDataChanged;
156    }
157
158    #endregion
159
160    #region Begin-/EndUpdate
161
162    private int beginUpdateCount = 0;
163
164    public void BeginUpdate()
165    {
166      beginUpdateCount++;
167    }
168
169    public void EndUpdate()
170    {
171      if (beginUpdateCount == 0)
172      {
173        throw new InvalidOperationException("Too many EndUpdates.");
174      }
175
176      beginUpdateCount--;
177
178      if (beginUpdateCount == 0)
179      {
180        Invalidate();
181      }
182    }
183
184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.