This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
HistogramPlot.cs
521 lines (443 loc) · 12.8 KB
/
HistogramPlot.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
NPlot - A charting library for .NET
HistogramPlot.cs
Copyright (C) 2003
Matt Howlett, Paolo Pierini
Redistribution and use of NPlot or parts there-of in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Re-distributions in source form must retain at the head of each
source file the above copyright notice, this list of conditions
and the following disclaimer.
2. Any product ("the product") that makes use NPlot or parts
there-of must either:
(a) allow any user of the product to obtain a complete machine-
readable copy of the corresponding source code for the
product and the version of NPlot used for a charge no more
than your cost of physically performing source distribution,
on a medium customarily used for software interchange, or:
(b) reproduce the following text in the documentation, about
box or other materials intended to be read by human users
of the product that is provided to every human user of the
product:
"This product includes software developed as
part of the NPlot library project available
from: http://www.nplot.com/"
The words "This product" may optionally be replace with
the actual name of the product.
------------------------------------------------------------------------
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
namespace NPlot
{
/// <summary>
/// Provides ability to draw histogram plots.
/// </summary>
public class HistogramPlot : BaseSequencePlot, IPlot, ISequencePlot
{
/// <summary>
/// Set/Get the brush to use if the histogram is filled.
/// </summary>
public IRectangleBrush RectangleBrush
{
get
{
return rectangleBrush_;
}
set
{
rectangleBrush_ = value;
}
}
private IRectangleBrush rectangleBrush_ = new RectangleBrushes.Solid( Color.Black );
/// <summary>
/// Constructor
/// </summary>
public HistogramPlot()
{
}
/// <summary>
/// Renders the histogram.
/// </summary>
/// <param name="g">The Graphics surface on which to draw</param>
/// <param name="xAxis">The X-Axis to draw against.</param>
/// <param name="yAxis">The Y-Axis to draw against.</param>
public void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
{
SequenceAdapter data =
new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
float yoff;
for ( int i=0; i<data.Count; ++i )
{
// (1) determine the top left hand point of the bar (assuming not centered)
PointD p1 = data[i];
if ( double.IsNaN(p1.X) || double.IsNaN(p1.Y) )
continue;
// (2) determine the top right hand point of the bar (assuming not centered)
PointD p2;
if (i+1 != data.Count)
{
p2 = data[i+1];
if ( double.IsNaN(p2.X) || double.IsNaN(p2.Y) )
continue;
p2.Y = p1.Y;
}
else if (i != 0)
{
p2 = data[i-1];
if ( double.IsNaN(p2.X) || double.IsNaN(p2.Y) )
continue;
double offset = p1.X - p2.X;
p2.X = p1.X + offset;
p2.Y = p1.Y;
}
else
{
double offset = 1.0f;
p2.X = p1.X + offset;
p2.Y = p1.Y;
}
// (3) now account for plots this may be stacked on top of.
HistogramPlot currentPlot = this;
yoff = 0.0f;
double yval = 0.0f;
while (currentPlot.isStacked_)
{
SequenceAdapter stackedToData = new SequenceAdapter(
currentPlot.stackedTo_.DataSource,
currentPlot.stackedTo_.DataMember,
currentPlot.stackedTo_.OrdinateData,
currentPlot.stackedTo_.AbscissaData );
yval += stackedToData[i].Y;
yoff = yAxis.WorldToPhysical( yval, false ).Y;
p1.Y += stackedToData[i].Y;
p2.Y += stackedToData[i].Y;
currentPlot = currentPlot.stackedTo_;
}
// (4) now account for centering
if ( center_ )
{
double offset = ( p2.X - p1.X ) / 2.0f;
p1.X -= offset;
p2.X -= offset;
}
// (5) now account for BaseOffset (shift of bar sideways).
p1.X += baseOffset_;
p2.X += baseOffset_;
// (6) now get physical coordinates of top two points.
PointF xPos1 = xAxis.WorldToPhysical( p1.X, false );
PointF yPos1 = yAxis.WorldToPhysical( p1.Y, false );
PointF xPos2 = xAxis.WorldToPhysical( p2.X, false );
PointF yPos2 = yAxis.WorldToPhysical( p2.Y, false );
if (isStacked_)
{
currentPlot = this;
while (currentPlot.isStacked_)
{
currentPlot = currentPlot.stackedTo_;
}
this.baseWidth_ = currentPlot.baseWidth_;
}
float width = xPos2.X - xPos1.X;
float height;
if (isStacked_)
{
height = -yPos1.Y+yoff;
}
else
{
height = -yPos1.Y+yAxis.PhysicalMin.Y;
}
float xoff = (1.0f - baseWidth_)/2.0f*width;
Rectangle r = new Rectangle( (int)(xPos1.X+xoff), (int)yPos1.Y, (int)(width-2*xoff), (int)height );
if (this.Filled)
{
if (r.Height != 0 && r.Width != 0)
{
// room for optimization maybe.
g.FillRectangle( rectangleBrush_.Get(r), r );
}
}
g.DrawRectangle( Pen, r.X, r.Y, r.Width, r.Height );
}
}
/// <summary>
/// Whether or not the histogram columns will be filled.
/// </summary>
public bool Filled
{
get
{
return filled_;
}
set
{
filled_ = value;
}
}
private bool filled_ = false;
private float baseWidth_ = 1.0f;
/// <summary>
/// The width of the histogram bar as a proportion of the data spacing
/// (in range 0.0 - 1.0).
/// </summary>
public float BaseWidth
{
get
{
return baseWidth_;
}
set
{
if (value > 0.0 && value <= 1.0)
{
baseWidth_ = value;
}
else
{
throw new NPlotException( "Base width must be between 0.0 and 1.0" );
}
}
}
/// <summary>
/// Returns an x-axis that is suitable for drawing this plot.
/// </summary>
/// <returns>A suitable x-axis.</returns>
public Axis SuggestXAxis()
{
SequenceAdapter data =
new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
Axis a = data.SuggestXAxis();
PointD p1;
PointD p2;
PointD p3;
PointD p4;
if (data.Count < 2)
{
p1 = data[0];
p1.X -= 1.0;
p2 = data[0];
p3 = p1;
p4 = p2;
}
else
{
p1 = data[0];
p2 = data[1];
p3 = data[data.Count-2];
p4 = data[data.Count-1];
}
double offset1;
double offset2;
if (!center_)
{
offset1 = 0.0f;
offset2 = p4.X - p3.X;
}
else
{
offset1 = (p2.X - p1.X)/2.0f;
offset2 = (p4.X - p3.X)/2.0f;
}
a.WorldMin -= offset1;
a.WorldMax += offset2;
return a;
}
/// <summary>
/// Returns a y-axis that is suitable for drawing this plot.
/// </summary>
/// <returns>A suitable y-axis.</returns>
public Axis SuggestYAxis()
{
if ( this.isStacked_ )
{
double tmpMax = 0.0f;
ArrayList adapterList = new ArrayList();
HistogramPlot currentPlot = this;
do
{
adapterList.Add( new SequenceAdapter(
currentPlot.DataSource,
currentPlot.DataMember,
currentPlot.OrdinateData,
currentPlot.AbscissaData )
);
} while ((currentPlot = currentPlot.stackedTo_) != null);
SequenceAdapter[] adapters =
(SequenceAdapter[])adapterList.ToArray(typeof(SequenceAdapter));
for (int i=0; i<adapters[0].Count; ++i)
{
double tmpHeight = 0.0f;
for (int j=0; j<adapters.Length; ++j)
{
tmpHeight += adapters[j][i].Y;
}
tmpMax = Math.Max(tmpMax, tmpHeight);
}
Axis a = new LinearAxis(0.0f,tmpMax);
// TODO make 0.08 a parameter.
a.IncreaseRange( 0.08 );
return a;
}
else
{
SequenceAdapter data =
new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
return data.SuggestYAxis();
}
}
/*
private double centerLine_ = 0.0;
/// <summary>
/// Histogram bars extend from the data value to this value. Default value is 0.
/// </summary>
public double CenterLine
{
get
{
return centerLine_;
}
set
{
centerLine_ = value;
}
}
*/
private bool center_ = true;
/// <summary>
/// If true, each histogram column will be centered on the associated abscissa value.
/// If false, each histogram colum will be drawn between the associated abscissa value, and the next abscissa value.
/// Default value is true.
/// </summary>
public bool Center
{
set
{
center_ = value;
}
get
{
return center_;
}
}
/// <summary>
/// If this histogram plot has another stacked on top, this will be true. Else false.
/// </summary>
public bool IsStacked
{
get
{
return isStacked_;
}
}
private bool isStacked_;
private HistogramPlot stackedTo_;
/// <summary>
/// Stack the histogram to another HistogramPlot.
/// </summary>
public void StackedTo(HistogramPlot hp)
{
SequenceAdapter data =
new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
SequenceAdapter hpData =
new SequenceAdapter( hp.DataSource, hp.DataMember, hp.OrdinateData, hp.AbscissaData );
if ( hp != null )
{
isStacked_ = true;
if ( hpData.Count != data.Count )
{
throw new NPlotException("Can stack HistogramPlot data only with the same number of datapoints.");
}
for ( int i=0; i < data.Count; ++i )
{
if ( data[i].X != hpData[i].X )
{
throw new NPlotException("Can stack HistogramPlot data only with the same X coordinates.");
}
if ( hpData[i].Y < 0.0f)
{
throw new NPlotException("Can stack HistogramPlot data only with positive Y coordinates.");
}
}
}
stackedTo_ = hp;
}
/// <summary>
/// Draws a representation of this plot in the legend.
/// </summary>
/// <param name="g">The graphics surface on which to draw.</param>
/// <param name="startEnd">A rectangle specifying the bounds of the area in the legend set aside for drawing.</param>
public void DrawInLegend( Graphics g, Rectangle startEnd )
{
if (Filled)
{
g.FillRectangle( rectangleBrush_.Get(startEnd), startEnd );
}
g.DrawRectangle( Pen, startEnd.X, startEnd.Y, startEnd.Width, startEnd.Height );
}
/// <summary>
/// The pen used to draw the plot
/// </summary>
public System.Drawing.Pen Pen
{
get
{
return pen_;
}
set
{
pen_ = value;
}
}
private System.Drawing.Pen pen_ = new Pen(Color.Black);
/// <summary>
/// The color of the pen used to draw lines in this plot.
/// </summary>
public System.Drawing.Color Color
{
set
{
if (pen_ != null)
{
pen_.Color = value;
}
else
{
pen_ = new Pen(value);
}
}
get
{
return pen_.Color;
}
}
/// <summary>
/// Horizontal position of histogram columns is offset by this much (in world coordinates).
/// </summary>
public double BaseOffset
{
set
{
baseOffset_ = value;
}
get
{
return baseOffset_;
}
}
private double baseOffset_;
}
}