-
Notifications
You must be signed in to change notification settings - Fork 355
/
WAxis.C
1358 lines (1135 loc) · 32.5 KB
/
WAxis.C
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <cmath>
#include <limits>
#include <cstdio>
#include <boost/lexical_cast.hpp>
#include "Wt/WAbstractItemModel"
#include "Wt/WColor"
#include "Wt/WDate"
#include "Wt/WException"
#include "Wt/WLogger"
#include "Wt/WPainter"
#include "Wt/WPainterPath"
#include "Wt/WRectF"
#include "Wt/WTime"
#include "Wt/Chart/WAxis"
#include "Wt/Chart/WAbstractChartImplementation"
#include "WebUtils.h"
namespace {
const int AUTO_V_LABEL_PIXELS = 25;
const int AUTO_H_LABEL_PIXELS = 80;
double round125(double v) {
double n = std::pow(10, std::floor(std::log10(v)));
double msd = v / n;
if (msd < 1.5)
return n;
else if (msd < 3.3)
return 2*n;
else if (msd < 7)
return 5*n;
else
return 10*n;
}
double roundUp125(double v, double t) {
return t * std::ceil((v - std::numeric_limits<double>::epsilon()) / t);
}
double roundDown125(double v, double t) {
return t * std::floor((v + std::numeric_limits<double>::epsilon()) / t);
}
int roundDown(int v, int factor) {
return (v / factor) * factor;
}
Wt::WPointF interpolate(const Wt::WPointF& p1, const Wt::WPointF& p2,
double u) {
double x = p1.x();
if (p2.x() - p1.x() > 0)
x += u;
else if (p2.x() - p1.x() < 0)
x -= u;
double y = p1.y();
if (p2.y() - p1.y() > 0)
y += u;
else if (p2.y() - p1.y() < 0)
y -= u;
return Wt::WPointF(x, y);
}
class TildeStartMarker : public Wt::WPainterPath {
public:
TildeStartMarker(int segmentMargin) {
moveTo(0, 0);
lineTo(0, segmentMargin - 25);
moveTo(-15, segmentMargin - 10);
lineTo(15, segmentMargin - 20);
}
};
class TildeEndMarker : public Wt::WPainterPath {
public:
TildeEndMarker(int segmentMargin) {
moveTo(0, 0);
lineTo(0, -(segmentMargin - 25));
moveTo(-15, -(segmentMargin - 20));
lineTo(15, -(segmentMargin - 10));
}
};
}
namespace Wt {
LOGGER("Chart.WAxis");
namespace Chart {
WAxis::TickLabel::TickLabel(double v, TickLength length, const WString& l)
: u(v),
tickLength(length),
label(l)
{ }
const double WAxis::AUTO_MINIMUM = -DBL_MAX;
const double WAxis::AUTO_MAXIMUM = DBL_MAX;
WAxis::Segment::Segment()
: minimum(AUTO_MINIMUM),
maximum(AUTO_MAXIMUM),
renderMinimum(AUTO_MINIMUM),
renderMaximum(AUTO_MAXIMUM),
renderLength(AUTO_MAXIMUM),
renderStart(AUTO_MAXIMUM)
{ }
WAxis::WAxis()
: chart_(0),
axis_(XAxis),
visible_(true),
location_(MinimumValue),
scale_(LinearScale),
resolution_(0.0),
labelInterval_(0),
labelBasePoint_(0),
defaultLabelFormat_(true),
gridLines_(false),
gridLinesPen_(gray),
margin_(0),
labelAngle_(0),
roundLimits_(MinimumValue | MaximumValue),
segmentMargin_(40),
titleOffset_(0)
{
titleFont_.setFamily(WFont::SansSerif);
titleFont_.setSize(WFont::FixedSize, WLength(12, WLength::Point));
labelFont_.setFamily(WFont::SansSerif);
labelFont_.setSize(WFont::FixedSize, WLength(10, WLength::Point));
segments_.push_back(Segment());
}
void WAxis::init(WAbstractChartImplementation* chart,
Axis axis)
{
chart_ = chart;
axis_ = axis;
if (axis == XAxis || axis_ == XAxis_3D || axis_ == YAxis_3D) {
if (chart_->chartType() == CategoryChart)
scale_ = CategoryScale;
else if (scale_ != DateScale)
scale_ = LinearScale;
}
if (axis == Y2Axis)
visible_ = false;
}
void WAxis::setVisible(bool visible)
{
set(visible_, visible);
}
void WAxis::setScale(AxisScale scale)
{
set(scale_, scale);
}
void WAxis::setLocation(AxisValue location)
{
set(location_, location);
}
void WAxis::setMinimum(double minimum)
{
Segment& s = segments_.front();
#ifndef WT_TARGET_JAVA
if (set(s.minimum, minimum))
s.maximum = std::max(s.minimum, s.maximum);
#else
set(s.minimum, minimum);
set(s.maximum, std::max(s.minimum, s.maximum));
#endif // WT_TARGET_JAVA
roundLimits_.clear(MinimumValue);
update();
}
double WAxis::minimum() const
{
return autoLimits() & MinimumValue ? segments_.front().renderMinimum
: segments_.front().minimum;
}
void WAxis::setMaximum(double maximum)
{
Segment& s = segments_.back();
#ifndef WT_TARGET_JAVA
if (set(s.maximum, maximum))
s.minimum = std::min(s.minimum, s.maximum);
#else
set(s.maximum, maximum);
set(s.minimum, std::min(s.minimum, s.maximum));
#endif // WT_TARGET_JAVA
roundLimits_.clear(MaximumValue);
update();
}
double WAxis::maximum() const
{
const Segment& s = segments_.back();
return autoLimits() & MaximumValue ? s.renderMaximum
: s.maximum;
}
void WAxis::setRange(double minimum, double maximum)
{
if (maximum > minimum) {
segments_.front().minimum = minimum;
segments_.back().maximum = maximum;
roundLimits_ = 0;
update();
}
}
void WAxis::setRoundLimits(WFlags<AxisValue> locations)
{
roundLimits_ = locations;
}
void WAxis::setResolution(const double resolution)
{
resolution_ = resolution;
update();
}
void WAxis::setAutoLimits(WFlags<AxisValue> locations)
{
if (locations & MinimumValue) {
set(segments_.front().minimum, AUTO_MINIMUM);
roundLimits_ |= MinimumValue;
}
if (locations & MaximumValue) {
set(segments_.back().maximum, AUTO_MAXIMUM);
roundLimits_ |= MaximumValue;
}
}
WFlags<AxisValue> WAxis::autoLimits() const
{
WFlags<AxisValue> result = 0;
if (segments_.front().minimum == AUTO_MINIMUM)
result |= MinimumValue;
if (segments_.back().maximum == AUTO_MAXIMUM)
result |= MaximumValue;
return result;
}
void WAxis::setBreak(double minimum, double maximum)
{
if (segments_.size() != 2) {
segments_.push_back(Segment());
segments_[1].maximum = segments_[0].maximum;
}
segments_[0].maximum = minimum;
segments_[1].minimum = maximum;
update();
}
void WAxis::setLabelInterval(double labelInterval)
{
set(labelInterval_, labelInterval);
}
void WAxis::setLabelBasePoint(double labelBasePoint)
{
set(labelBasePoint_, labelBasePoint);
}
void WAxis::setLabelFormat(const WString& format)
{
set(labelFormat_, format);
defaultLabelFormat_ = false;
}
WString WAxis::labelFormat() const
{
switch (scale_) {
case CategoryScale:
return WString();
case DateScale:
case DateTimeScale:
if (defaultLabelFormat_) {
if (!segments_.empty()) {
const Segment& s = segments_[0];
return defaultDateTimeFormat(s);
} else {
return labelFormat_;
}
} else
return labelFormat_;
default:
return defaultLabelFormat_ ? WString::fromUTF8("%.4g") : labelFormat_;
}
}
void WAxis::setLabelAngle(double angle)
{
if (renderingMirror_)
labelAngle_ = angle;
else
set(labelAngle_, angle);
}
void WAxis::setGridLinesEnabled(bool enabled)
{
set(gridLines_, enabled);
}
void WAxis::setPen(const WPen& pen)
{
set(pen_, pen);
}
void WAxis::setGridLinesPen(const WPen& pen)
{
set(gridLinesPen_, pen);
}
void WAxis::setMargin(int pixels)
{
set(margin_, pixels);
}
void WAxis::setTitle(const WString& title)
{
set(title_, title);
}
void WAxis::setTitleFont(const WFont& titleFont)
{
set(titleFont_, titleFont);
}
void WAxis::setLabelFont(const WFont& labelFont)
{
set(labelFont_, labelFont);
}
void WAxis::update()
{
if (chart_)
chart_->update();
}
bool WAxis::prepareRender(Orientation orientation, double length) const
{
double totalRenderRange = 0;
for (unsigned i = 0; i < segments_.size(); ++i) {
const Segment& s = segments_[i];
computeRange(s);
totalRenderRange += s.renderMaximum - s.renderMinimum;
}
double clipMin = segments_.front().renderMinimum == 0 ?
0 : chart_->axisPadding();
double clipMax = segments_.back().renderMaximum == 0 ?
0 : chart_->axisPadding();
double totalRenderLength = length;
double totalRenderStart = clipMin;
const double SEGMENT_MARGIN = 40;
// 6 pixels additional margin to avoid clipping lines that render
// the extreme values
totalRenderLength
-= SEGMENT_MARGIN * (segments_.size() - 1) + clipMin + clipMax;
if (totalRenderLength <= 0) {
renderInterval_ = 1.0;
return false;
}
/*
* Iterate twice, since we adjust the render extrema based on the size
* and vice-versa
*/
for (unsigned it = 0; it < 2; ++it) {
double rs = totalRenderStart;
double TRR = totalRenderRange;
totalRenderRange = 0;
for (unsigned i = 0; i < segments_.size(); ++i) {
const Segment& s = segments_[i];
double diff = s.renderMaximum - s.renderMinimum;
s.renderStart = rs;
s.renderLength = diff / TRR * totalRenderLength;
if (i == 0) {
renderInterval_ = labelInterval_;
if (renderInterval_ == 0) {
if (scale_ == CategoryScale) {
double numLabels = calcAutoNumLabels(orientation, s) / 1.5;
int rc = chart_->numberOfCategories(axis_);
renderInterval_ = std::max(1.0, std::floor(rc / numLabels));
} else if (scale_ == LogScale) {
renderInterval_ = 1; // does not apply
} else {
double numLabels = calcAutoNumLabels(orientation, s);
renderInterval_ = round125(diff / numLabels);
}
}
}
if (renderInterval_ == 0) {
renderInterval_ = 1;
return false;
}
if (scale_ == LinearScale) {
if (it == 0) {
if (roundLimits_ & MinimumValue) {
s.renderMinimum
= roundDown125(s.renderMinimum, renderInterval_);
if (s.renderMinimum <= labelBasePoint_ &&
s.renderMaximum >= labelBasePoint_) {
double interv = labelBasePoint_ - s.renderMinimum;
interv = renderInterval_ * 2
* std::ceil(interv / (renderInterval_ * 2));
s.renderMinimum = labelBasePoint_ - interv;
}
}
if (roundLimits_ & MaximumValue)
s.renderMaximum
= roundUp125(s.renderMaximum, renderInterval_);
}
} else if (scale_ == DateScale || scale_ == DateTimeScale) {
double daysInterval = 0.0;
WDateTime min, max;
int interval;
if (scale_ == DateScale) {
daysInterval = renderInterval_;
min = WDateTime(WDate::fromJulianDay
(static_cast<int>(s.renderMinimum)));
max = WDateTime(WDate::fromJulianDay
(static_cast<int>(s.renderMaximum)));
} else if (scale_ == DateTimeScale) {
daysInterval = renderInterval_ / (60.0 * 60.0 * 24);
min = WDateTime::fromTime_t((std::time_t)s.renderMinimum);
max = WDateTime::fromTime_t((std::time_t)s.renderMaximum);
}
LOG_DEBUG("Range: " << min.toString() << ", " << max.toString());
if (daysInterval > 200) {
s.dateTimeRenderUnit = Years;
interval = std::max(1,
static_cast<int>(round125(daysInterval / 365)));
if (roundLimits_ & MinimumValue)
if (min.date().day() != 1 && min.date().month() != 1)
min = WDateTime(WDate(min.date().year(), 1, 1));
if (roundLimits_ & MaximumValue)
if (max.date().day() != 1 && max.date().day() != 1)
max = WDateTime(WDate(max.date().year() + 1, 1, 1));
} else if (daysInterval > 20) {
s.dateTimeRenderUnit = Months;
double d = daysInterval / 30;
if (d < 1.3)
interval = 1;
else if (d < 2.3)
interval = 2;
else if (d < 3.3)
interval = 3;
else if (d < 4.3)
interval = 4;
else
interval = 6;
/* push min and max to a round month (at interval boundary) */
if (roundLimits_ & MinimumValue) {
if ((min.date().month() - 1) % interval != 0) {
int m = roundDown(min.date().month() - 1, interval) + 1;
min = WDateTime(WDate(min.date().year(), m, 1));
} else if (min.date().day() != 1)
min = WDateTime(WDate(min.date().year(), min.date().month(), 1));
}
if (roundLimits_ & MaximumValue) {
if (max.date().day() != 1)
max = WDateTime
(WDate(max.date().year(), max.date().month(), 1).addMonths(1));
if ((max.date().month() - 1) % interval != 0) {
int m = roundDown(max.date().month() - 1, interval) + 1;
max = WDateTime(WDate(max.date().year(), m, 1)
.addMonths(interval));
}
}
} else if (daysInterval > 0.6) {
s.dateTimeRenderUnit = Days;
if (daysInterval < 1.3)
interval = 1;
else
interval = 7 * std::max(1,
static_cast<int>((daysInterval + 5) / 7));
} else {
double minutes = daysInterval * 24 * 60;
if (minutes > 40) {
s.dateTimeRenderUnit = Hours;
double d = minutes / 60;
if (d < 1.3)
interval = 1;
else if (d < 2.3)
interval = 2;
else if (d < 3.3)
interval = 3;
else if (d < 4.3)
interval = 4;
else if (d < 6.3)
interval = 6;
else
interval = 12;
/* push min and max to a round hour (at interval boundary) */
if (roundLimits_ & MinimumValue) {
if (min.time().hour() % interval != 0) {
int h = roundDown(min.time().hour(), interval);
min.setTime(WTime(h, 0));
} else if (min.time().minute() != 0)
min.setTime(WTime(min.time().hour(), 0));
}
if (roundLimits_ & MaximumValue) {
if (max.time().minute() != 0) {
max.setTime(WTime(max.time().hour(), 0));
max = max.addSecs(60 * 60);
}
if (max.time().hour() % interval != 0) {
int h = roundDown(max.time().hour(), interval);
max.setTime(WTime(h, 0));
max = max.addSecs(interval * 60 * 60);
}
}
} else if (minutes > 0.8) {
s.dateTimeRenderUnit = Minutes;
if (minutes < 1.3)
interval = 1;
else if (minutes < 2.3)
interval = 2;
else if (minutes < 5.3)
interval = 5;
else if (minutes < 10.3)
interval = 10;
else if (minutes < 15.3)
interval = 15;
else if (minutes < 20.3)
interval = 20;
else
interval = 30;
if (roundLimits_ & MinimumValue) {
/* push min and max to a round minute (at interval boundary) */
if (min.time().minute() % interval != 0) {
int m = roundDown(min.time().minute(), interval);
min.setTime(WTime(min.time().hour(), m));
} else if (min.time().second() != 0)
min.setTime(WTime(min.time().hour(), min.time().minute()));
}
if (roundLimits_ & MaximumValue) {
if (max.time().second() != 0) {
max.setTime(WTime(max.time().hour(), max.time().minute()));
max = max.addSecs(60);
}
if (max.time().minute() % interval != 0) {
int m = roundDown(max.time().minute(), interval);
max.setTime(WTime(max.time().hour(), m));
max = max.addSecs(interval * 60);
}
}
} else {
s.dateTimeRenderUnit = Seconds;
double seconds = minutes * 60;
if (seconds < 1.3)
interval = 1;
else if (seconds < 2.3)
interval = 2;
else if (seconds < 5.3)
interval = 5;
else if (seconds < 10.3)
interval = 10;
else if (seconds < 15.3)
interval = 15;
else if (seconds < 20.3)
interval = 20;
else
interval = 30;
/* push min and max to a round second (at interval boundary) */
if (roundLimits_ & MinimumValue) {
if (min.time().second() % interval != 0) {
int sec = roundDown(min.time().second(), interval);
min.setTime(WTime(min.time().hour(), min.time().minute(), sec));
} else if (min.time().msec() != 0)
min.setTime(WTime(min.time().hour(), min.time().minute(),
min.time().second()));
}
if (roundLimits_ & MaximumValue) {
if (max.time().msec() != 0) {
max.setTime(WTime(max.time().hour(), max.time().minute(),
max.time().second()));
max = max.addSecs(1);
}
if (max.time().second() % interval != 0) {
int sec = roundDown(max.time().second(), interval);
max.setTime(WTime(max.time().hour(), max.time().minute(), sec));
max = max.addSecs(interval);
}
}
}
}
s.dateTimeRenderInterval = interval;
if (scale_ == DateScale) {
s.renderMinimum = min.date().toJulianDay();
s.renderMaximum = max.date().toJulianDay();
} else if (scale_ == DateTimeScale) {
s.renderMinimum = min.toTime_t();
s.renderMaximum = max.toTime_t();
}
}
totalRenderRange += s.renderMaximum - s.renderMinimum;
rs += s.renderLength + SEGMENT_MARGIN;
}
}
return true;
}
void WAxis::setOtherAxisLocation(AxisValue otherLocation) const
{
if (scale_ != LogScale) {
for (unsigned i = 0; i < segments_.size(); ++i) {
const Segment& s = segments_[i];
int borderMin, borderMax;
if (scale_ == CategoryScale){
borderMax = borderMin = chart_->axisPadding();
}else {
borderMin = (s.renderMinimum == 0 && otherLocation == ZeroValue)
? 0 : chart_->axisPadding();
borderMax = (s.renderMinimum == 0 && otherLocation == ZeroValue)
? 0 : chart_->axisPadding();
}
s.renderLength -= (borderMin + borderMax);
s.renderStart += borderMin;
}
}
}
void WAxis::computeRange(const Segment& segment) const
{
if (scale_ == CategoryScale) {
int rc = chart_->numberOfCategories(axis_);
rc = std::max(1, rc);
segment.renderMinimum = -0.5;
segment.renderMaximum = rc - 0.5;
} else {
segment.renderMinimum = segment.minimum;
segment.renderMaximum = segment.maximum;
const bool findMinimum = segment.renderMinimum == AUTO_MINIMUM;
const bool findMaximum = segment.renderMaximum == AUTO_MAXIMUM;
if (findMinimum || findMaximum) {
double minimum = std::numeric_limits<double>::max();
double maximum = -std::numeric_limits<double>::max();
WAbstractChartImplementation::RenderRange rr =
chart_->computeRenderRange(axis_, scale_);
minimum = rr.minimum;
maximum = rr.maximum;
if (minimum == std::numeric_limits<double>::max()) {
if (scale_ == LogScale)
minimum = 1;
else if (scale_ == DateScale)
minimum = WDate::currentDate().toJulianDay() - 10;
else
minimum = 0;
}
if (maximum == -std::numeric_limits<double>::max()) {
if (scale_ == LogScale)
maximum = 10;
else if (scale_ == DateScale)
maximum = WDate::currentDate().toJulianDay();
else
maximum = 100;
}
if (findMinimum)
segment.renderMinimum
= std::min(minimum, findMaximum ? maximum : segment.maximum);
if (findMaximum)
segment.renderMaximum
= std::max(maximum, findMinimum ? minimum : segment.minimum);
}
double diff = segment.renderMaximum - segment.renderMinimum;
if (scale_ == LogScale) {
/*
* For LogScale, resolution is ignored, and we always
* show at least one log range (if it's up to us).
*/
double minLog10 = std::log10(segment.renderMinimum);
double maxLog10 = std::log10(segment.renderMaximum);
if (findMinimum && findMaximum) {
segment.renderMinimum = std::pow(10, std::floor(minLog10));
segment.renderMaximum = std::pow(10, std::ceil(maxLog10));
if (segment.renderMinimum == segment.renderMaximum)
segment.renderMaximum = std::pow(10, std::ceil(maxLog10) + 1);
} else if (findMinimum) {
segment.renderMinimum = std::pow(10, std::floor(minLog10));
if (segment.renderMinimum == segment.renderMaximum)
segment.renderMinimum = std::pow(10, std::floor(minLog10) - 1);
} else if (findMaximum) {
segment.renderMaximum = std::pow(10, std::ceil(maxLog10));
if (segment.renderMinimum == segment.renderMaximum)
segment.renderMaximum = std::pow(10, std::ceil(maxLog10) + 1);
}
} else {
double resolution = resolution_;
/*
* Old behaviour, we ignore a resolution set.
*/
if (resolution == 0) {
if (scale_ == LinearScale)
resolution = std::max(1E-3,
std::fabs(1E-3 * segment.renderMinimum));
else if (scale_ == DateScale)
resolution = 1;
else if (scale_ == DateTimeScale)
resolution = 120;
}
if (std::fabs(diff) < resolution) {
double average = (segment.renderMaximum + segment.renderMinimum) / 2.0;
double d = resolution;
if (findMinimum && findMaximum) {
segment.renderMaximum = average + d / 2.0;
segment.renderMinimum = average - d / 2.0;
} else if (findMinimum) {
segment.renderMinimum = segment.renderMaximum - d;
} else if (findMaximum) {
segment.renderMaximum = segment.renderMinimum + d;
}
diff = segment.renderMaximum - segment.renderMinimum;
}
/*
* Heuristic to extend range to include 0 or to span at least one
* log
*/
if (findMinimum && segment.renderMinimum >= 0
&& (segment.renderMinimum - 0.50 * diff <= 0))
segment.renderMinimum = 0;
if (findMaximum && segment.renderMaximum <= 0
&& (segment.renderMaximum + 0.50 * diff >= 0))
segment.renderMaximum = 0;
}
}
assert(segment.renderMinimum < segment.renderMaximum);
}
double WAxis::mapToDevice(const boost::any& value, int segment) const
{
return mapToDevice(getValue(value), segment);
}
double WAxis::getValue(const boost::any& v) const
{
switch (scale_) {
case LinearScale:
case LogScale:
return asNumber(v);
case DateScale:
if (v.type() == typeid(WDate)) {
WDate d = boost::any_cast<WDate>(v);
return static_cast<double>(d.toJulianDay());
}
#ifndef WT_TARGET_JAVA
else if (v.type() == typeid(WDateTime)) {
WDateTime dt = boost::any_cast<WDateTime>(v);
return static_cast<double>(dt.date().toJulianDay());
}
#endif
else {
return std::numeric_limits<double>::signaling_NaN();
}
case DateTimeScale:
if (v.type() == typeid(WDate)) {
WDate d = boost::any_cast<WDate>(v);
WDateTime dt;
dt.setDate(d);
return (double)dt.toTime_t();
}
#ifndef WT_TARGET_JAVA
else if (v.type() == typeid(WDateTime)) {
WDateTime dt = boost::any_cast<WDateTime>(v);
return static_cast<double>(dt.toTime_t());
}
#endif
else {
return std::numeric_limits<double>::signaling_NaN();
}
default:
return -1.0;
}
}
double WAxis::mapToDevice(double u, int segment) const
{
if (Utils::isNaN(u))
return u;
const Segment& s = segments_[segment];
double d;
if (scale_ != LogScale) {
d = (u - s.renderMinimum)
/ (s.renderMaximum - s.renderMinimum)
* s.renderLength;
} else {
u = std::max(s.renderMinimum, u);
d = (std::log(u) - std::log(s.renderMinimum))
/ (std::log(s.renderMaximum) - std::log(s.renderMinimum))
* s.renderLength;
}
return s.renderStart + d;
}
double WAxis::mapFromDevice(double d) const
{
for (unsigned i = 0; i < segments_.size(); ++i) {
const Segment& s = segments_[i];
bool lastSegment = (i == segments_.size() - 1);
if (lastSegment || d < mapToDevice(s.renderMaximum, i)) {
d = d - s.renderStart;
if (scale_ != LogScale) {
return s.renderMinimum + d * (s.renderMaximum - s.renderMinimum)
/ s.renderLength;
} else {
return std::exp(std::log(s.renderMinimum)
+ d * (std::log(s.renderMaximum)
- std::log(s.renderMinimum)) / s.renderLength);
}
}
}
return 0;
}
WString WAxis::label(double u) const
{
#ifndef WT_TARGET_JAVA
char buf[30];
#else
char *buf = 0;
#endif // WT_TARGET_JAVA
WString text;
if (scale_ == CategoryScale) {
text = chart_->categoryLabel((int)u, axis_);
if (text.empty())
text = WLocale::currentLocale().toString(u);
} else if (scale_ == DateScale) {
WDate d = WDate::fromJulianDay(static_cast<int>(u));
WString format = labelFormat();
return d.toString(format);
} else {
std::string format = labelFormat().toUTF8();
if (format.empty())
text = WLocale::currentLocale().toString(u);
else {
#ifdef WT_TARGET_JAVA
buf =
#endif // WT_TARGET_JAVA
std::sprintf(buf, format.c_str(), u);
text = WString::fromUTF8(buf);
}
}
return text;
}
void WAxis::getLabelTicks(std::vector<TickLabel>& ticks, int segment) const
{
static double EPSILON = 1E-3;
const Segment& s = segments_[segment];
int rc;
switch (scale_) {
case CategoryScale: {
rc = chart_->numberOfCategories(axis_);
int renderInterval = std::max(1, static_cast<int>(renderInterval_));
if (renderInterval == 1) {
ticks.push_back(TickLabel(-0.5, TickLabel::Long));
for (int i = 0; i < rc; ++i) {
ticks.push_back(TickLabel(i + 0.5, TickLabel::Long));
ticks.push_back(TickLabel(i, TickLabel::Zero,
label(static_cast<double>(i))));
}
} else {
/*
* We could do a special effort for date X series here...
*/
for (int i = 0; i < rc; i += renderInterval) {
ticks.push_back(TickLabel(i, TickLabel::Long,
label(static_cast<double>(i))));
}
}
break;
}
case LinearScale: {
for (int i = 0;; ++i) {
double v = s.renderMinimum + renderInterval_ * i;
if (v - s.renderMaximum > EPSILON * renderInterval_)
break;
WString t;
if (i % 2 == 0)
t = label(v);
ticks.push_back