forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathChart.cs
440 lines (389 loc) · 15.2 KB
/
Chart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// AForge Controls Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2011
// contacts@aforgenet.com
//
namespace AForge.Controls
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using AForge;
/// <summary>
/// Chart control.
/// </summary>
///
/// <remarks><para>The chart control allows to display multiple charts at time
/// of different types: dots, lines, connected dots.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create data series array
/// double[,] testValues = new double[10, 2];
/// // fill data series
/// for ( int i = 0; i < 10; i++ )
/// {
/// testValues[i, 0] = i; // X values
/// testValues[i, 1] = Math.Sin( i / 18.0 * Math.PI ); // Y values
/// }
/// // add new data series to the chart
/// chart.AddDataSeries( "Test", Color.DarkGreen, Chart.SeriesType.ConnectedDots, 3 );
/// // set X range to display
/// chart.RangeX = new AForge.Range( 0, 9 );
/// // update the chart
/// chart.UpdateDataSeries( "Test", testValues );
/// </code>
/// </remarks>
///
public class Chart : System.Windows.Forms.Control
{
/// <summary>
/// Chart series type enumeration.
/// </summary>
public enum SeriesType
{
/// <summary>
/// Line style.
/// </summary>
Line,
/// <summary>
/// Dots style.
/// </summary>
Dots,
/// <summary>
/// Connected dots style.
/// </summary>
ConnectedDots
}
// series data
private class DataSeries
{
public double[,] data = null;
public Color color = Color.Blue;
public SeriesType type = SeriesType.Line;
public int width = 1;
public bool updateYRange = true;
}
// data series table
private Dictionary<string, DataSeries> seriesTable = new Dictionary<string, DataSeries>( );
private Pen blackPen = new Pen( Color.Black );
private Range rangeX = new Range( 0, 1 );
private Range rangeY = new Range( 0, 1 );
/// <summary>
/// Chart's X range.
/// </summary>
///
/// <remarks><para>The value sets the X range of data to be displayed on the chart.</para></remarks>
///
[Browsable( false )]
public Range RangeX
{
get { return rangeX; }
set
{
rangeX = value;
UpdateYRange( );
Invalidate( );
}
}
/// <summary>
/// Chart's Y range.
/// </summary>
///
/// <remarks>The value sets the Y range of data to be displayed on the chart.</remarks>
///
[Browsable( false )]
public Range RangeY
{
get { return rangeY; }
set
{
rangeY = value;
Invalidate( );
}
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// Initializes a new instance of the <see cref="Chart"/> class.
/// </summary>
///
public Chart( )
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent( );
// update control style
SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true );
}
/// <summary>
/// Dispose the object.
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if ( components != null )
components.Dispose( );
// free graphics resources
blackPen.Dispose( );
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent( )
{
this.SuspendLayout( );
//
// Chart
//
this.Paint += new System.Windows.Forms.PaintEventHandler( this.Chart_Paint );
this.ResumeLayout( false );
}
#endregion
// Paint the control.
private void Chart_Paint( object sender, PaintEventArgs e )
{
Graphics g = e.Graphics;
int clientWidth = ClientRectangle.Width;
int clientHeight = ClientRectangle.Height;
// fill with white background
Brush backgroundBrush = new SolidBrush( BackColor );
g.FillRectangle( backgroundBrush, 0, 0, clientWidth - 1, clientHeight - 1 );
backgroundBrush.Dispose( );
// draw a black rectangle
g.DrawRectangle( blackPen, 0, 0, clientWidth - 1, clientHeight - 1 );
// set clipping rectangle
g.SetClip( new Rectangle( 2, 2, clientWidth - 4, clientHeight - 4 ) );
// check if there are any data series
if ( rangeX.Length != 0 )
{
double xFactor = (double) ( clientWidth - 10 ) / ( rangeX.Length );
double yFactor = (double) ( clientHeight - 10 ) / ( ( rangeY.Length != 0 ) ? rangeY.Length : 1 );
// walk through all data series
foreach ( KeyValuePair<string, DataSeries> kvp in seriesTable )
{
DataSeries series = kvp.Value;
// get data of the series
double[,] data = series.data;
// check for available data
if ( data == null )
continue;
// check series type
if ( series.type == SeriesType.Dots )
{
// draw dots
Brush brush = new SolidBrush( series.color );
int width = series.width;
int r = width >> 1;
// draw all points
for ( int i = 0, n = data.GetLength( 0 ); i < n; i++ )
{
int x = (int) ( ( data[i, 0] - rangeX.Min ) * xFactor );
int y = (int) ( ( data[i, 1] - rangeY.Min ) * yFactor );
x += 5;
y = clientHeight - 6 - y;
g.FillRectangle( brush, x - r, y - r, width, width );
}
brush.Dispose( );
}
else if ( series.type == SeriesType.ConnectedDots )
{
// draw dots connected with 1-pixel width line
Brush brush = new SolidBrush( series.color );
Pen pen = new Pen( series.color, 1 );
int width = series.width;
int r = width >> 1;
int x1 = (int) ( ( data[0, 0] - rangeX.Min ) * xFactor );
int y1 = (int) ( ( data[0, 1] - rangeY.Min ) * yFactor );
x1 += 5;
y1 = clientHeight - 6 - y1;
g.FillRectangle( brush, x1 - r, y1 - r, width, width );
// draw all lines
for ( int i = 1, n = data.GetLength( 0 ); i < n; i++ )
{
int x2 = (int) ( ( data[i, 0] - rangeX.Min ) * xFactor );
int y2 = (int) ( ( data[i, 1] - rangeY.Min ) * yFactor );
x2 += 5;
y2 = clientHeight - 6 - y2;
g.FillRectangle( brush, x2 - r, y2 - r, width, width );
g.DrawLine( pen, x1, y1, x2, y2 );
x1 = x2;
y1 = y2;
}
pen.Dispose( );
brush.Dispose( );
}
else if ( series.type == SeriesType.Line )
{
// draw line
Pen pen = new Pen( series.color, series.width );
int x1 = (int) ( ( data[0, 0] - rangeX.Min ) * xFactor );
int y1 = (int) ( ( data[0, 1] - rangeY.Min ) * yFactor );
x1 += 5;
y1 = clientHeight - 6 - y1;
// draw all lines
for ( int i = 1, n = data.GetLength( 0 ); i < n; i++ )
{
int x2 = (int) ( ( data[i, 0] - rangeX.Min ) * xFactor );
int y2 = (int) ( ( data[i, 1] - rangeY.Min ) * yFactor );
x2 += 5;
y2 = clientHeight - 6 - y2;
g.DrawLine( pen, x1, y1, x2, y2 );
x1 = x2;
y1 = y2;
}
pen.Dispose( );
}
}
}
}
/// <summary>
/// Add data series to the chart.
/// </summary>
///
/// <param name="name">Data series name.</param>
/// <param name="color">Data series color.</param>
/// <param name="type">Data series type.</param>
/// <param name="width">Width (depends on the data series type, see remarks).</param>
///
/// <remarks><para>Adds new empty data series to the collection of data series. To update this
/// series the <see cref="UpdateDataSeries"/> method should be used.</para>
///
/// <para>The meaning of the width parameter depends on the data series type:
/// <list type="bullet">
/// <item><b>Line</b> - width of the line;</item>
/// <item><b>Dots</b> - size of dots (rectangular dots with specified width and the same height);</item>
/// <item><b>Connected dots</b> - size of dots (dots are connected with one pixel width line).</item>
/// </list>
/// </para>
/// </remarks>
///
public void AddDataSeries( string name, Color color, SeriesType type, int width )
{
AddDataSeries( name, color, type, width, true );
}
/// <summary>
/// Add data series to the chart.
/// </summary>
///
/// <param name="name">Data series name.</param>
/// <param name="color">Data series color.</param>
/// <param name="type">Data series type.</param>
/// <param name="width">Width (depends on the data series type, see remarks).</param>
/// <param name="updateYRange">Specifies if <see cref="RangeY"/> should be updated.</param>
///
/// <remarks><para>Adds new empty data series to the collection of data series.</para>
///
/// <para>The <b>updateYRange</b> parameter specifies if the data series may affect displayable
/// Y range. If the value is set to false, then displayable Y range is not updated, but used the
/// range, which was specified by user (see <see cref="RangeY"/> property). In the case if the
/// value is set to true, the displayable Y range is recalculated to fully fit the new data
/// series.</para>
/// </remarks>
///
public void AddDataSeries( string name, Color color, SeriesType type, int width, bool updateYRange )
{
// create new series definition ...
DataSeries series = new DataSeries( );
// ... add fill it
series.color = color;
series.type = type;
series.width = width;
series.updateYRange = updateYRange;
// add to series table
seriesTable.Add( name, series );
}
/// <summary>
/// Update data series on the chart.
/// </summary>
///
/// <param name="name">Data series name to update.</param>
/// <param name="data">Data series values.</param>
///
public void UpdateDataSeries( string name, double[,] data )
{
if ( !seriesTable.ContainsKey( name ) )
throw new ArgumentException( "The chart does not contain data series with name: " + name );
// get data series
DataSeries series = seriesTable[name];
// update data
series.data = ( data != null ) ? (double[,]) data.Clone( ) : null;
// update Y range
if ( series.updateYRange )
UpdateYRange( );
// invalidate the control
Invalidate( );
}
/// <summary>
/// Remove data series from the chart.
/// </summary>
///
/// <param name="name">Data series name to remove.</param>
///
public void RemoveDataSeries( string name )
{
// remove data series from table
seriesTable.Remove( name );
// invalidate the control
Invalidate( );
}
/// <summary>
/// Remove all data series from the chart.
/// </summary>
public void RemoveAllDataSeries( )
{
// remove all data series from table
seriesTable.Clear( );
// invalidate the control
Invalidate( );
}
/// <summary>
/// Update Y range.
/// </summary>
private void UpdateYRange( )
{
float minY = float.MaxValue;
float maxY = float.MinValue;
// walk through all data series
foreach ( KeyValuePair<string, DataSeries> kvp in seriesTable )
{
DataSeries series = kvp.Value;
// get data of the series
double[,] data = series.data;
if ( ( series.updateYRange ) && ( data != null ) )
{
for ( int i = 0, n = data.GetLength( 0 ); i < n; i++ )
{
if ( rangeX.IsInside( (float) data[i, 0] ) )
{
float v = (float) data[i, 1];
// check for max
if ( v > maxY )
maxY = v;
// check for min
if ( v < minY )
minY = v;
}
}
}
}
// update Y range, if there are any data
if ( ( minY != double.MaxValue ) || ( maxY != double.MinValue ) )
{
rangeY = new Range( minY, maxY );
}
}
}
}