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
/
LinearAxis.cs
650 lines (537 loc) · 18.9 KB
/
LinearAxis.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
NPlot - A charting library for .NET
LinearAxis.cs
Copyright (C) 2003
Matt Howlett
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.Drawing;
using System.Collections;
using System;
using System.Text;
using System.Diagnostics;
namespace NPlot
{
/// <summary>
/// Provides functionality for drawing axes with a linear numeric scale.
/// </summary>
public class LinearAxis : Axis, System.ICloneable
{
/// <summary>
/// Deep copy of LinearAxis.
/// </summary>
/// <returns>A copy of the LinearAxis Class</returns>
public override object Clone()
{
LinearAxis a = new LinearAxis();
// ensure that this isn't being called on a derived type. If it is, then oh no!
if (this.GetType() != a.GetType())
{
throw new NPlotException( "Clone not defined in derived type. Help!" );
}
this.DoClone( this, a );
return a;
}
/// <summary>
/// Helper method for Clone.
/// </summary>
protected void DoClone( LinearAxis b, LinearAxis a )
{
Axis.DoClone( b, a );
a.numberSmallTicks_ = b.numberSmallTicks_;
a.largeTickValue_ = b.largeTickValue_;
a.largeTickStep_ = b.largeTickStep_;
a.offset_ = b.offset_;
a.scale_ = b.scale_;
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="a">The Axis to clone</param>
public LinearAxis( Axis a )
: base( a )
{
Init();
}
/// <summary>
/// Default constructor.
/// </summary>
public LinearAxis()
: base()
{
Init();
}
/// <summary>
/// Construct a linear axis with the provided world min and max values.
/// </summary>
/// <param name="worldMin">the world minimum value of the axis.</param>
/// <param name="worldMax">the world maximum value of the axis.</param>
public LinearAxis( double worldMin, double worldMax )
: base( worldMin, worldMax )
{
Init();
}
private void Init()
{
this.NumberFormat = "{0:g5}";
}
/// <summary>
/// Draws the large and small ticks [and tick labels] for this axis.
/// </summary>
/// <param name="g">The graphics surface on which to draw.</param>
/// <param name="physicalMin">The physical position corresponding to the world minimum of the axis.</param>
/// <param name="physicalMax">The physical position corresponding to the world maximum of the axis.</param>
/// <param name="boundingBox">out: smallest box that completely surrounds all ticks and associated labels for this axis.</param>
/// <param name="labelOffset">out: offset from the axis to draw the axis label.</param>
protected override void DrawTicks(
Graphics g,
Point physicalMin,
Point physicalMax,
out object labelOffset,
out object boundingBox )
{
Point tLabelOffset;
Rectangle tBoundingBox;
labelOffset = this.getDefaultLabelOffset( physicalMin, physicalMax );
boundingBox = null;
ArrayList largeTickPositions;
ArrayList smallTickPositions;
this.WorldTickPositions( physicalMin, physicalMax, out largeTickPositions, out smallTickPositions );
labelOffset = new Point( 0, 0 );
boundingBox = null;
if (largeTickPositions.Count > 0)
{
for (int i = 0; i < largeTickPositions.Count; ++i)
{
double labelNumber = (double)largeTickPositions[i];
// TODO: Find out why zero is sometimes significantly not zero [seen as high as 10^-16].
if (Math.Abs(labelNumber) < 0.000000000000001)
{
labelNumber = 0.0;
}
StringBuilder label = new StringBuilder();
label.AppendFormat(this.NumberFormat, labelNumber);
this.DrawTick( g, ((double)largeTickPositions[i]/this.scale_-this.offset_),
this.LargeTickSize, label.ToString(),
new Point(0,0), physicalMin, physicalMax,
out tLabelOffset, out tBoundingBox );
Axis.UpdateOffsetAndBounds( ref labelOffset, ref boundingBox,
tLabelOffset, tBoundingBox );
}
}
for (int i = 0; i<smallTickPositions.Count; ++i)
{
this.DrawTick( g, ((double)smallTickPositions[i]/this.scale_-this.offset_),
this.SmallTickSize, "",
new Point(0, 0), physicalMin, physicalMax,
out tLabelOffset, out tBoundingBox );
// assume bounding box and label offset unchanged by small tick bounds.
}
}
/// <summary>
/// Determines the positions, in world coordinates, of the small ticks
/// if they have not already been generated.
///
/// </summary>
/// <param name="physicalMin">The physical position corresponding to the world minimum of the axis.</param>
/// <param name="physicalMax">The physical position corresponding to the world maximum of the axis.</param>
/// <param name="largeTickPositions">The positions of the large ticks.</param>
/// <param name="smallTickPositions">If null, small tick positions are returned via this parameter. Otherwise this function does nothing.</param>
internal override void WorldTickPositions_SecondPass(
Point physicalMin,
Point physicalMax,
ArrayList largeTickPositions,
ref ArrayList smallTickPositions )
{
// return if already generated.
if (smallTickPositions != null)
return;
int physicalAxisLength = Utils.Distance( physicalMin, physicalMax );
double adjustedMax = this.AdjustedWorldValue( WorldMax );
double adjustedMin = this.AdjustedWorldValue( WorldMin );
smallTickPositions = new ArrayList();
// TODO: Can optimize this now.
bool shouldCullMiddle;
double bigTickSpacing = this.DetermineLargeTickStep( physicalAxisLength, out shouldCullMiddle );
int nSmall = this.DetermineNumberSmallTicks( bigTickSpacing );
double smallTickSpacing = bigTickSpacing / (double)nSmall;
// if there is at least one big tick
if (largeTickPositions.Count > 0)
{
double pos1 = (double)largeTickPositions[0] - smallTickSpacing;
while (pos1 > adjustedMin)
{
smallTickPositions.Add( pos1 );
pos1 -= smallTickSpacing;
}
}
for (int i = 0; i < largeTickPositions.Count; ++i )
{
for (int j = 1; j < nSmall; ++j )
{
double pos = (double)largeTickPositions[i] + ((double)j) * smallTickSpacing;
if (pos <= adjustedMax)
{
smallTickPositions.Add( pos );
}
}
}
}
/// <summary>
/// Adjusts a real world value to one that has been modified to
/// reflect the Axis Scale and Offset properties.
/// </summary>
/// <param name="world">world value to adjust</param>
/// <returns>adjusted world value</returns>
public double AdjustedWorldValue( double world )
{
return world * this.scale_ + this.offset_;
}
/// <summary>
/// Determines the positions, in world coordinates, of the large ticks.
/// When the physical extent of the axis is small, some of the positions
/// that were generated in this pass may be converted to small tick
/// positions and returned as well.
///
/// If the LargeTickStep isn't set then this is calculated automatically and
/// depends on the physical extent of the axis.
/// </summary>
/// <param name="physicalMin">The physical position corresponding to the world minimum of the axis.</param>
/// <param name="physicalMax">The physical position corresponding to the world maximum of the axis.</param>
/// <param name="largeTickPositions">ArrayList containing the positions of the large ticks.</param>
/// <param name="smallTickPositions">ArrayList containing the positions of the small ticks if calculated, null otherwise.</param>
internal override void WorldTickPositions_FirstPass(
Point physicalMin,
Point physicalMax,
out ArrayList largeTickPositions,
out ArrayList smallTickPositions
)
{
// (1) error check
if ( double.IsNaN(WorldMin) || double.IsNaN(WorldMax) )
{
throw new NPlotException( "world extent of axis not set." );
}
double adjustedMax = this.AdjustedWorldValue( WorldMax );
double adjustedMin = this.AdjustedWorldValue( WorldMin );
// (2) determine distance between large ticks.
bool shouldCullMiddle;
double tickDist = this.DetermineLargeTickStep(
Utils.Distance(physicalMin, physicalMax),
out shouldCullMiddle );
// (3) determine starting position.
double first = 0.0f;
if (!double.IsNaN(largeTickValue_))
{
// this works for both case when largTickValue_ lt or gt adjustedMin.
first = largeTickValue_ + (Math.Ceiling((adjustedMin-largeTickValue_)/tickDist))*tickDist;
}
else
{
if( adjustedMin > 0.0 )
{
double nToFirst = Math.Floor(adjustedMin / tickDist) + 1.0f;
first = nToFirst * tickDist;
}
else
{
double nToFirst = Math.Floor(-adjustedMin/tickDist) - 1.0f;
first = -nToFirst * tickDist;
}
// could miss one, if first is just below zero.
if ((first - tickDist) >= adjustedMin)
{
first -= tickDist;
}
}
// (4) now make list of large tick positions.
largeTickPositions = new ArrayList();
if (tickDist < 0.0) // some sanity checking. TODO: remove this.
throw new NPlotException( "Tick dist is negative" );
double position = first;
int safetyCount = 0;
while (
(position <= adjustedMax) &&
(++safetyCount < 5000) )
{
largeTickPositions.Add( position );
position += tickDist;
}
// (5) if the physical extent is too small, and the middle
// ticks should be turned into small ticks, then do this now.
smallTickPositions = null;
if (shouldCullMiddle)
{
smallTickPositions = new ArrayList();
if (largeTickPositions.Count > 2)
{
for (int i=1; i<largeTickPositions.Count-1; ++i)
{
smallTickPositions.Add( largeTickPositions[i] );
}
}
ArrayList culledPositions = new ArrayList();
culledPositions.Add( largeTickPositions[0] );
culledPositions.Add( largeTickPositions[largeTickPositions.Count-1] );
largeTickPositions = culledPositions;
}
}
/// <summary>
/// Calculates the world spacing between large ticks, based on the physical
/// axis length (parameter), world axis length, Mantissa values and
/// MinPhysicalLargeTickStep. A value such that at least two
/// </summary>
/// <param name="physicalLength">physical length of the axis</param>
/// <param name="shouldCullMiddle">Returns true if we were forced to make spacing of
/// large ticks too small in order to ensure that there are at least two of
/// them. The draw ticks method should not draw more than two large ticks if this
/// returns true.</param>
/// <returns>Large tick spacing</returns>
/// <remarks>TODO: This can be optimised a bit.</remarks>
private double DetermineLargeTickStep( float physicalLength, out bool shouldCullMiddle )
{
shouldCullMiddle = false;
if ( double.IsNaN(WorldMin) || double.IsNaN(WorldMax) )
{
throw new NPlotException( "world extent of axis not set." );
}
// if the large tick has been explicitly set, then return this.
if ( !double.IsNaN(largeTickStep_) )
{
if ( largeTickStep_ <= 0.0f )
{
throw new NPlotException(
"can't have negative or zero tick step - reverse WorldMin WorldMax instead."
);
}
return largeTickStep_;
}
// otherwise we need to calculate the large tick step ourselves.
// adjust world max and min for offset and scale properties of axis.
double adjustedMax = this.AdjustedWorldValue( WorldMax );
double adjustedMin = this.AdjustedWorldValue( WorldMin );
double range = adjustedMax - adjustedMin;
// if axis has zero world length, then return arbitrary number.
if ( Utils.DoubleEqual( adjustedMax, adjustedMin ) )
{
return 1.0f;
}
double approxTickStep;
if (TicksIndependentOfPhysicalExtent)
{
approxTickStep = range / 6.0f;
}
else
{
approxTickStep = (MinPhysicalLargeTickStep / physicalLength) * range;
}
double exponent = Math.Floor( Math.Log10( approxTickStep ) );
double mantissa = Math.Pow( 10.0, Math.Log10( approxTickStep ) - exponent );
// determine next whole mantissa below the approx one.
int mantissaIndex = Mantissas.Length-1;
for (int i=1; i<Mantissas.Length; ++i)
{
if (mantissa < Mantissas[i])
{
mantissaIndex = i-1;
break;
}
}
// then choose next largest spacing.
mantissaIndex += 1;
if (mantissaIndex == Mantissas.Length)
{
mantissaIndex = 0;
exponent += 1.0;
}
if (!TicksIndependentOfPhysicalExtent)
{
// now make sure that the returned value is such that at least two
// large tick marks will be displayed.
double tickStep = Math.Pow( 10.0, exponent ) * Mantissas[mantissaIndex];
float physicalStep = (float)((tickStep / range) * physicalLength);
while (physicalStep > physicalLength/2)
{
shouldCullMiddle = true;
mantissaIndex -= 1;
if (mantissaIndex == -1)
{
mantissaIndex = Mantissas.Length-1;
exponent -= 1.0;
}
tickStep = Math.Pow( 10.0, exponent ) * Mantissas[mantissaIndex];
physicalStep = (float)((tickStep / range) * physicalLength);
}
}
// and we're done.
return Math.Pow( 10.0, exponent ) * Mantissas[mantissaIndex];
}
/// <summary>
/// Given the large tick step, determine the number of small ticks that should
/// be placed in between.
/// </summary>
/// <param name="bigTickDist">the large tick step.</param>
/// <returns>the number of small ticks to place between large ticks.</returns>
private int DetermineNumberSmallTicks( double bigTickDist )
{
if (this.numberSmallTicks_ != null)
{
return (int)this.numberSmallTicks_+1;
}
if (this.SmallTickCounts.Length != this.Mantissas.Length)
{
throw new NPlotException( "Mantissa.Length != SmallTickCounts.Length" );
}
if (bigTickDist > 0.0f)
{
double exponent = Math.Floor( Math.Log10( bigTickDist ) );
double mantissa = Math.Pow( 10.0, Math.Log10( bigTickDist ) - exponent );
for (int i=0; i<Mantissas.Length; ++i)
{
if ( Math.Abs(mantissa-Mantissas[i]) < 0.001 )
{
return SmallTickCounts[i]+1;
}
}
}
return 0;
}
/// <summary>
/// The distance between large ticks. If this is set to NaN [default],
/// this distance will be calculated automatically.
/// </summary>
public double LargeTickStep
{
set
{
largeTickStep_ = value;
}
get
{
return largeTickStep_;
}
}
/// <summary>
/// If set !NaN, gives the distance between large ticks.
/// </summary>
private double largeTickStep_ = double.NaN;
/// <summary>
/// If set, a large tick will be placed at this position, and other large ticks will
/// be placed relative to this position.
/// </summary>
public double LargeTickValue
{
set
{
largeTickValue_ = value;
}
get
{
return largeTickValue_;
}
}
private double largeTickValue_ = double.NaN;
/// <summary>
/// The number of small ticks between large ticks.
/// </summary>
public int NumberOfSmallTicks
{
set
{
numberSmallTicks_ = value;
}
get
{
// TODO: something better here.
return (int)numberSmallTicks_;
}
}
private object numberSmallTicks_ = null;
/// <summary>
/// Scale to apply to world values when labelling axis:
/// (labelWorld = world * scale + offset). This does not
/// affect the "real" world range of the axis.
/// </summary>
public double Scale
{
get
{
return scale_;
}
set
{
scale_ = value;
}
}
/// <summary>
/// Offset to apply to world values when labelling the axis:
/// (labelWorld = axisWorld * scale + offset). This does not
/// affect the "real" world range of the axis.
/// </summary>
public double Offset
{
get
{
return offset_;
}
set
{
offset_ = value;
}
}
/// <summary>
/// If LargeTickStep isn't specified, then a suitable value is
/// calculated automatically. To determine the tick spacing, the
/// world axis length is divided by ApproximateNumberLargeTicks
/// and the next lowest distance m*10^e for some m in the Mantissas
/// set and some integer e is used as the large tick spacing.
/// </summary>
public float ApproxNumberLargeTicks = 3.0f;
/// <summary>
/// If LargeTickStep isn't specified, then a suitable value is
/// calculated automatically. The value will be of the form
/// m*10^e for some m in this set.
/// </summary>
public double[] Mantissas = {1.0, 2.0, 5.0};
/// <summary>
/// If NumberOfSmallTicks isn't specified then ....
/// If specified LargeTickStep manually, then no small ticks unless
/// NumberOfSmallTicks specified.
/// </summary>
public int[] SmallTickCounts = {4, 1, 4};
private double offset_ = 0.0;
private double scale_ = 1.0;
}
}