/
WCartesianChart.C
4262 lines (3680 loc) · 121 KB
/
WCartesianChart.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 "Wt/Chart/WAbstractChartModel.h"
#include "Wt/Chart/WAxisSliderWidget.h"
#include "Wt/Chart/WChart2DImplementation.h"
#include "Wt/Chart/WDataSeries.h"
#include "Wt/Chart/WCartesianChart.h"
#include "Wt/Chart/WStandardPalette.h"
#include "Wt/WAbstractArea.h"
#include "Wt/WAbstractItemModel.h"
#include "Wt/WApplication.h"
#include "Wt/WCanvasPaintDevice.h"
#include "Wt/WCircleArea.h"
#include "Wt/WException.h"
#include "Wt/WEnvironment.h"
#include "Wt/WJavaScriptHandle.h"
#include "Wt/WJavaScriptObjectStorage.h"
#include "Wt/WMeasurePaintDevice.h"
#include "Wt/WPainter.h"
#include "Wt/WPolygonArea.h"
#include "Wt/WRectArea.h"
#include "Wt/WText.h"
#include "Wt/WTheme.h"
#include "DomElement.h"
#include "WebUtils.h"
#ifndef WT_DEBUG_JS
#include "js/ChartCommon.min.js"
#include "js/WCartesianChart.min.js"
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace {
const int TICK_LENGTH = 5;
const int CURVE_LABEL_PADDING = 10;
const int DEFAULT_CURVE_LABEL_WIDTH = 100;
const int CURVE_SELECTION_DISTANCE_SQUARED = 400; // Maximum selection distance in pixels, squared
inline int toZoomLevel(double zoomFactor)
{
return ((int)(std::floor(std::log(zoomFactor) / std::log(2.0) + 0.5))) + 1;
}
}
namespace Wt {
LOGGER("WCartesianChart");
namespace Chart {
WColor WCartesianChart::lightenColor(const WColor& in)
{
double hsl[3];
in.toHSL(hsl);
double h = hsl[0], s = hsl[1], l = hsl[2];
return WColor::fromHSL(h, std::max(s - 0.2, 0.0), std::min(l + 0.2, 1.0), in.alpha());
}
CurveLabel::CurveLabel(const WDataSeries &series, const WPointF &point, const WT_USTRING &label)
: series_(&series),
point_(point),
label_(label),
offset_(60, -20),
width_(0),
linePen_(WColor(0,0,0)),
textPen_(WColor(0,0,0)),
boxBrush_(WColor(255,255,255)),
markerBrush_(WColor(0,0,0))
{ }
void CurveLabel::setSeries(const WDataSeries &series)
{
series_ = &series;
}
void CurveLabel::setPoint(const WPointF &point)
{
point_ = point;
}
void CurveLabel::setLabel(const WT_USTRING &label)
{
label_ = label;
}
void CurveLabel::setOffset(const WPointF &offset)
{
offset_ = offset;
}
void CurveLabel::setWidth(int width)
{
width_ = width;
}
void CurveLabel::setLinePen(const WPen &pen)
{
linePen_ = pen;
}
void CurveLabel::setTextPen(const WPen &pen)
{
textPen_ = pen;
}
void CurveLabel::setBoxBrush(const WBrush &brush)
{
boxBrush_ = brush;
}
void CurveLabel::setMarkerBrush(const WBrush &brush)
{
markerBrush_ = brush;
}
// Checks if edge [p1,p2] intersects the horizontal edge [(minX,y),(maxX,y)]
bool CurveLabel::checkIntersectHorizontal(const WPointF &p1, const WPointF &p2, double minX, double maxX, double y)
{
if (p1.y() == p2.y()) return p1.y() == y;
double t = (y - p1.y()) / (p2.y() - p1.y());
if (t <= 0 || t >= 1) return false; // The edge does not reach y, we do not consider the ends
double x = p1.x() * (1 - t) + p2.x() * t;
return x > minX && x < maxX; // do not consider the ends
}
// Checks if edge [p1,p2] intersects the vertical edge [(x,minY),(x,maxY)]
bool CurveLabel::checkIntersectVertical(const WPointF &p1, const WPointF &p2, double minY, double maxY, double x)
{
return checkIntersectHorizontal(WPointF(p1.y(), p1.x()), WPointF(p2.y(), p2.x()), minY, maxY, x);
}
void CurveLabel::render(WPainter &painter) const
{
WRectF rect;
{
// Determine rectangle width, width if nonzero, otherwise calculated using font metrics or, if unavailable, DEFAULT_CURVE_LABEL_WIDTH
double rectWidth = DEFAULT_CURVE_LABEL_WIDTH;
if (width() != 0) {
rectWidth = width();
} else if (painter.device()->features().test(
PaintDeviceFeatureFlag::FontMetrics)) {
WMeasurePaintDevice device(painter.device());
WPainter measPainter(&device);
measPainter.drawText(WRectF(0,0,100,100),
AlignmentFlag::Middle | AlignmentFlag::Center,
TextFlag::SingleLine, label(), nullptr);
rectWidth = device.boundingRect().width() + CURVE_LABEL_PADDING / 2;
}
rect = WRectF(offset().x() - rectWidth / 2, offset().y() - 10,
rectWidth, 20).normalized();
}
WPointF closestAnchor;
{
// Find the closest point on the side of the rectangle to connect to,
// that does not cause the line to it from the starting point to intersect
// with an edge of the rectangle.
std::vector<WPointF> anchorPoints;
anchorPoints.push_back(WPointF(rect.left(), rect.center().y()));
anchorPoints.push_back(WPointF(rect.right(), rect.center().y()));
anchorPoints.push_back(WPointF(rect.center().x(), rect.top()));
anchorPoints.push_back(WPointF(rect.center().x(), rect.bottom()));
double minSquareDist = std::numeric_limits<double>::infinity();
for (std::size_t k = 0; k < anchorPoints.size(); ++k) {
const WPointF &anchorPoint = anchorPoints[k];
double d = anchorPoint.x() * anchorPoint.x() + anchorPoint.y() * anchorPoint.y();
if (d < minSquareDist &&
(k == 0 || !checkIntersectVertical(WPointF(), anchorPoint, rect.top(), rect.bottom(), rect.left())) &&
(k == 1 || !checkIntersectVertical(WPointF(), anchorPoint, rect.top(), rect.bottom(), rect.right())) &&
(k == 2 || !checkIntersectHorizontal(WPointF(), anchorPoint, rect.left(), rect.right(), rect.top())) &&
(k == 3 || !checkIntersectHorizontal(WPointF(), anchorPoint, rect.left(), rect.right(), rect.bottom()))) {
closestAnchor = anchorPoint;
minSquareDist = d;
}
}
}
WTransform translation = painter.worldTransform();
painter.setWorldTransform(WTransform());
WPainterPath connectorLine;
connectorLine.moveTo(0, 0);
connectorLine.lineTo(closestAnchor);
painter.strokePath(translation.map(connectorLine).crisp(), linePen());
WPainterPath circle;
circle.addEllipse(-2.5, -2.5, 5, 5);
painter.fillPath(translation.map(circle), markerBrush());
WPainterPath rectPath;
rectPath.addRect(rect);
painter.fillPath(translation.map(rectPath), boxBrush());
painter.strokePath(translation.map(rectPath).crisp(), linePen());
painter.setPen(textPen());
painter.drawText(translation.map(rect),
AlignmentFlag::Middle | AlignmentFlag::Center,
TextFlag::SingleLine, label(), nullptr);
}
SeriesIterator::~SeriesIterator()
{ }
void SeriesIterator::startSegment(int currentXSegment, int currentYSegment,
const WRectF& currentSegmentArea)
{
currentXSegment_ = currentXSegment;
currentYSegment_ = currentYSegment;
}
void SeriesIterator::endSegment()
{
}
bool SeriesIterator::startSeries(const WDataSeries& series, double groupWidth,
int numBarGroups, int currentBarGroup)
{
return true;
}
void SeriesIterator::endSeries()
{ }
void SeriesIterator::newValue(const WDataSeries& series,
double x, double y, double stackY,
int xRow, int xColumn,
int yRow, int yColumn)
{ }
void SeriesIterator::setPenColor(WPen& pen, const WDataSeries &series,
int xRow, int xColumn,
int yRow, int yColumn,
int colorRole)
{
const WColor *color = nullptr;
if (yRow >= 0 && yColumn >= 0) {
if (colorRole == ItemDataRole::MarkerPenColor) {
color = series.model()->markerPenColor(yRow, yColumn);
} else if (colorRole == ItemDataRole::MarkerBrushColor) {
color = series.model()->markerBrushColor(yRow, yColumn);
}
}
if (!color && xRow >= 0 && xColumn >= 0) {
if (colorRole == ItemDataRole::MarkerPenColor) {
color = series.model()->markerPenColor(xRow, xColumn);
} else if (colorRole == ItemDataRole::MarkerBrushColor) {
color = series.model()->markerBrushColor(xRow, xColumn);
}
}
if (color)
pen.setColor(*color);
}
void SeriesIterator::setBrushColor(WBrush& brush, const WDataSeries &series,
int xRow, int xColumn,
int yRow, int yColumn,
int colorRole)
{
const WColor *color = nullptr;
if (yRow >= 0 && yColumn >= 0) {
if (colorRole == ItemDataRole::MarkerBrushColor) {
color = series.model()->markerBrushColor(yRow, yColumn);
} else if (colorRole == ItemDataRole::BarBrushColor) {
color = series.model()->barBrushColor(yRow, yColumn);
}
}
if (!color && xRow >= 0 && xColumn >= 0) {
if (colorRole == ItemDataRole::MarkerBrushColor) {
color = series.model()->markerBrushColor(xRow, xColumn);
} else if (colorRole == ItemDataRole::BarBrushColor) {
color = series.model()->barBrushColor(xRow, xColumn);
}
}
if (color)
brush.setColor(*color);
}
class SeriesRenderer;
class SeriesRenderIterator final : public SeriesIterator
{
public:
SeriesRenderIterator(const WCartesianChart& chart, WPainter& painter);
virtual void startSegment(int currentXSegment, int currentYSegment,
const WRectF& currentSegmentArea) override;
virtual void endSegment() override;
virtual bool startSeries(const WDataSeries& series, double groupWidth,
int numBarGroups, int currentBarGroup) override;
virtual void endSeries() override;
virtual void newValue(const WDataSeries& series, double x, double y,
double stackY,
int xRow, int xColumn,
int yRow, int yColumn) override;
double breakY(double y);
private:
const WCartesianChart& chart_;
WPainter& painter_;
const WDataSeries *series_;
SeriesRenderer *seriesRenderer_;
double minY_, maxY_;
};
class SeriesRenderer {
public:
virtual ~SeriesRenderer() { }
virtual void addBreak() = 0;
virtual void addValue(double x, double y, double stacky,
int xRow, int xColumn, int yRow, int yColumn)
= 0;
virtual void paint() = 0;
protected:
const WCartesianChart& chart_;
WPainter& painter_;
const WDataSeries& series_;
SeriesRenderIterator& it_;
SeriesRenderer(const WCartesianChart& chart, WPainter& painter,
const WDataSeries& series, SeriesRenderIterator& it)
: chart_(chart),
painter_(painter),
series_(series),
it_(it)
{ }
static double crisp(double u) {
return std::floor(u) + 0.5;
}
WPointF hv(const WPointF& p) {
return chart_.hv(p);
}
WPointF hv(double x, double y) {
return chart_.hv(x, y);
}
};
class LineSeriesRenderer final : public SeriesRenderer {
public:
LineSeriesRenderer(const WCartesianChart& chart, WPainter& painter,
const WDataSeries& series,
SeriesRenderIterator& it)
: SeriesRenderer(chart, painter, series, it),
curveLength_(0),
curveFragmentLength_(0)
{
curve_.setOpenSubPathsEnabled(true);
}
virtual void addValue(double x, double y, double stacky,
int xRow, int xColumn, int yRow, int yColumn) override {
WPointF p = chart_.map(x, y, series_.axis(),
it_.currentXSegment(), it_.currentYSegment());
if (curveFragmentLength_ == 0) {
curve_.moveTo(hv(p));
if (series_.fillRange() != FillRangeType::None
&& series_.brush() != BrushStyle::None) {
fill_.moveTo(hv(fillOtherPoint(x)));
fill_.lineTo(hv(p));
}
} else {
if (series_.type() == SeriesType::Line) {
curve_.lineTo(hv(p));
fill_.lineTo(hv(p));
} else {
if (curveFragmentLength_ == 1) {
computeC(p0, p, c_);
} else {
WPointF c1, c2;
computeC(p_1, p0, p, c1, c2);
curve_.cubicTo(hv(c_), hv(c1), hv(p0));
fill_.cubicTo(hv(c_), hv(c1), hv(p0));
c_ = c2;
}
}
}
p_1 = p0;
p0 = p;
lastX_ = x;
++curveLength_;
++curveFragmentLength_;
}
virtual void addBreak() override
{
if (curveFragmentLength_ > 1) {
if (series_.type() == SeriesType::Curve) {
WPointF c1;
computeC(p0, p_1, c1);
curve_.cubicTo(hv(c_), hv(c1), hv(p0));
fill_.cubicTo(hv(c_), hv(c1), hv(p0));
}
if (series_.fillRange() != FillRangeType::None
&& series_.brush() != BrushStyle::None) {
fill_.lineTo(hv(fillOtherPoint(lastX_)));
fill_.closeSubPath();
}
}
curveFragmentLength_ = 0;
}
virtual void paint() override {
WCartesianChart::PainterPathMap::iterator curveHandle = const_cast<WCartesianChart&>(chart_).curvePaths_.find(&series_);
WCartesianChart::TransformMap::iterator transformHandle = const_cast<WCartesianChart&>(chart_).curveTransforms_.find(&series_);
WTransform transform = chart_.zoomRangeTransform();
if (curveLength_ > 1) {
if (series_.type() == SeriesType::Curve) {
WPointF c1;
computeC(p0, p_1, c1);
curve_.cubicTo(hv(c_), hv(c1), hv(p0));
fill_.cubicTo(hv(c_), hv(c1), hv(p0));
}
if (series_.fillRange() != FillRangeType::None
&& series_.brush() != BrushStyle::None && !series_.isHidden()) {
fill_.lineTo(hv(fillOtherPoint(lastX_)));
fill_.closeSubPath();
painter_.setShadow(series_.shadow());
WBrush brush = series_.brush();
if (chart_.seriesSelectionEnabled() &&
chart_.selectedSeries() != nullptr &&
chart_.selectedSeries() != &series_) {
brush.setColor(WCartesianChart::lightenColor(brush.color()));
}
painter_.fillPath(transform.map(fill_), brush); // FIXME: support curve transforms
}
if (series_.fillRange() == FillRangeType::None)
painter_.setShadow(series_.shadow());
else
painter_.setShadow(WShadow());
WTransform ct;
WTransform t = chart_.calculateCurveTransform(series_);
if (transformHandle != chart_.curveTransforms_.end()) {
transformHandle->second.setValue(t);
ct = chart_.curveTransform(series_);
} else {
ct = t;
if (chart_.orientation() == Orientation::Horizontal) {
ct = WTransform(0,1,1,0,0,0) * ct * WTransform(0,1,1,0,0,0);
}
}
series_.scaleDirty_ = false;
series_.offsetDirty_ = false;
const WPainterPath *curve = nullptr;
if (curveHandle != chart_.curvePaths_.end()) {
curveHandle->second.setValue(curve_);
curve = &curveHandle->second.value();
} else {
curve = &curve_;
}
if (!series_.isHidden()) {
WPen pen = series_.pen();
if (chart_.seriesSelectionEnabled() &&
chart_.selectedSeries() != nullptr &&
chart_.selectedSeries() != &series_) {
pen.setColor(WCartesianChart::lightenColor(pen.color()));
}
painter_.strokePath((transform * ct).map(*curve), pen);
}
}
curveLength_ = 0;
curveFragmentLength_ = 0;
curve_ = WPainterPath();
fill_ = WPainterPath();
}
private:
int curveLength_;
int curveFragmentLength_;
WPainterPath curve_;
WPainterPath fill_;
double lastX_;
WPointF p_1, p0, c_;
static double dist(const WPointF& p1, const WPointF& p2) {
double dx = p2.x() - p1.x();
double dy = p2.y() - p1.y();
return std::sqrt (dx*dx + dy*dy);
}
static void computeC(const WPointF& p, const WPointF& p1, WPointF& c) {
c.setX(p.x() + 0.3 * (p1.x() - p.x()));
c.setY(p.y() + 0.3 * (p1.y() - p.y()));
}
static void computeC(const WPointF& p_1, const WPointF& p0,
const WPointF& p1,
WPointF& c1, WPointF& c2) {
double m1x = (p_1.x() + p0.x())/2.0;
double m1y = (p_1.y() + p0.y())/2.0;
double m2x = (p0.x() + p1.x())/2.0;
double m2y = (p0.y() + p1.y())/2.0;
double L1 = dist(p_1, p0);
double L2 = dist(p0, p1);
double r = L1/(L1 + L2);
c1.setX(p0.x() - r * (m2x - m1x));
c1.setY(p0.y() - r * (m2y - m1y));
r = 1-r;
c2.setX(p0.x() - r * (m1x - m2x));
c2.setY(p0.y() - r * (m1y - m2y));
}
WPointF fillOtherPoint(double x) const {
FillRangeType fr = series_.fillRange();
switch (fr) {
case FillRangeType::MinimumValue:
return WPointF(chart_.map(x, 0, series_.axis(),
it_.currentXSegment(),
it_.currentYSegment()).x(),
chart_.chartArea_.bottom());
case FillRangeType::MaximumValue:
return WPointF(chart_.map(x, 0, series_.axis(),
it_.currentXSegment(),
it_.currentYSegment()).x(),
chart_.chartArea_.top());
case FillRangeType::ZeroValue:
return WPointF(chart_.map(x, 0, series_.axis(),
it_.currentXSegment(),
it_.currentYSegment()));
default:
return WPointF();
}
}
};
class BarSeriesRenderer final : public SeriesRenderer {
public:
BarSeriesRenderer(const WCartesianChart& chart, WPainter& painter,
const WDataSeries& series,
SeriesRenderIterator& it,
double groupWidth, int numGroups, int group)
: SeriesRenderer(chart, painter, series, it),
groupWidth_(groupWidth),
numGroups_(numGroups),
group_(group)
{ }
virtual void addValue(double x, double y, double stacky,
int xRow, int xColumn, int yRow, int yColumn) override {
WPainterPath bar;
const WAxis& yAxis = chart_.axis(series_.axis());
WPointF topMid = chart_.map(x, y, yAxis.id(),
it_.currentXSegment(),
it_.currentYSegment());
WPointF bottomMid = chart_.map(x, stacky, yAxis.id(),
it_.currentXSegment(),
it_.currentYSegment());
FillRangeType fr = series_.fillRange();
switch (fr) {
case FillRangeType::MinimumValue:
bottomMid = WPointF(chart_.map(x, stacky, yAxis.id(),
it_.currentXSegment(),
it_.currentYSegment()).x(),
chart_.chartArea_.bottom());
break;
case FillRangeType::MaximumValue:
bottomMid = WPointF(chart_.map(x, stacky, yAxis.id(),
it_.currentXSegment(),
it_.currentYSegment()).x(),
chart_.chartArea_.top());
break;
default:
break;
}
double g = numGroups_ + (numGroups_ - 1) * chart_.barMargin();
double width = groupWidth_ / g;
double left = topMid.x() - groupWidth_ / 2
+ group_ * width * (1 + chart_.barMargin());
bool nonZeroWidth = chart_.isInteractive() || crisp(left) != crisp(left + width);
bar.moveTo(hv(left, topMid.y()));
if (nonZeroWidth) {
bar.lineTo(hv(left + width, topMid.y()));
bar.lineTo(hv(left + width, bottomMid.y()));
}
bar.lineTo(hv(left, bottomMid.y()));
if (nonZeroWidth) {
bar.closeSubPath();
}
painter_.setShadow(series_.shadow());
WTransform transform = chart_.zoomRangeTransform();
if (nonZeroWidth) {
WBrush brush = WBrush(series_.brush());
SeriesIterator::setBrushColor(brush, series_, xRow, xColumn, yRow, yColumn, ItemDataRole::BarBrushColor);
painter_.fillPath(transform.map(bar), brush);
}
painter_.setShadow(WShadow());
WPen pen = WPen(series_.pen());
SeriesIterator::setPenColor(pen, series_, xRow, xColumn, yRow, yColumn, ItemDataRole::BarPenColor);
painter_.strokePath(transform.map(bar).crisp(), pen);
WString toolTip = series_.model()->toolTip(yRow, yColumn);
if (!toolTip.empty()) {
WTransform t = painter_.worldTransform();
WPointF tl = t.map(segmentPoint(bar, 0));
WPointF tr = t.map(segmentPoint(bar, 1));
WPointF br = t.map(segmentPoint(bar, 2));
WPointF bl = t.map(segmentPoint(bar, 3));
if (series_.model()->flags(yRow, yColumn).test(ItemFlag::DeferredToolTip) ||
// Force deferred tooltips if XHTML text
series_.model()->flags(yRow, yColumn).test(ItemFlag::XHTMLText)) {
chart_.hasDeferredToolTips_ = true;
WCartesianChart::BarTooltip btt(series_, xRow, xColumn, yRow, yColumn);
btt.xs[0] = tl.x();
btt.ys[0] = tl.y();
btt.xs[1] = tr.x();
btt.ys[1] = tr.y();
btt.xs[2] = br.x();
btt.ys[2] = br.y();
btt.xs[3] = bl.x();
btt.ys[3] = bl.y();
chart_.barTooltips_.push_back(btt);
} else {
double tlx = 0, tly = 0, brx = 0, bry = 0;
bool useRect = false;
if (fequal(tl.y(), tr.y())) {
tlx = std::min(tl.x(), tr.x());
brx = std::max(tl.x(), tr.x());
tly = std::min(tl.y(), bl.y());
bry = std::max(tl.y(), br.y());
useRect = true;
} else if (fequal(tl.x(), tr.x())) {
tlx = std::min(tl.x(), bl.x());
brx = std::max(tl.x(), bl.x());
tly = std::min(tl.y(), tr.y());
bry = std::max(tl.y(), tr.y());
useRect = true;
}
std::unique_ptr<WAbstractArea> area;
if (useRect)
area.reset(new WRectArea(tlx, tly, (brx - tlx), (bry - tly)));
else {
WPolygonArea *poly = new WPolygonArea();
poly->addPoint(tl.x(), tl.y());
poly->addPoint(tr.x(), tr.y());
poly->addPoint(br.x(), br.y());
poly->addPoint(bl.x(), bl.y());
area.reset(poly);
}
area->setToolTip(toolTip);
const_cast<WCartesianChart&>(chart_)
.addDataPointArea(series_, xRow, xColumn, std::move(area));
}
}
double bTopMidY = it_.breakY(topMid.y());
double bBottomMidY = it_.breakY(bottomMid.y());
if (bTopMidY > topMid.y() && bBottomMidY <= bottomMid.y()) {
WPainterPath breakPath;
breakPath.moveTo(hv(left - 10, bTopMidY + 10));
breakPath.lineTo(hv(left + width + 10, bTopMidY + 1));
breakPath.lineTo(hv(left + width + 10, bTopMidY - 1));
breakPath.lineTo(hv(left - 10, bTopMidY - 1));
painter_.setPen(PenStyle::None);
painter_.setBrush(chart_.background());
painter_.drawPath(transform.map(breakPath).crisp());
painter_.setPen(WPen());
WPainterPath line;
line.moveTo(hv(left - 10, bTopMidY + 10));
line.lineTo(hv(left + width + 10, bTopMidY + 1));
painter_.drawPath(transform.map(line).crisp());
}
if (bBottomMidY < bottomMid.y() && bTopMidY >= topMid.y()) {
WPainterPath breakPath;
breakPath.moveTo(hv(left + width + 10, bBottomMidY - 10));
breakPath.lineTo(hv(left - 10, bBottomMidY - 1));
breakPath.lineTo(hv(left - 10, bBottomMidY + 1));
breakPath.lineTo(hv(left + width + 10, bBottomMidY + 1));
painter_.setBrush(chart_.background());
painter_.setPen(PenStyle::None);
painter_.drawPath(transform.map(breakPath).crisp());
painter_.setPen(WPen());
WPainterPath line;
line.moveTo(hv(left - 10, bBottomMidY - 1));
line.lineTo(hv(left + width + 10, bBottomMidY - 10));
painter_.drawPath(transform.map(line).crisp());
}
}
virtual void addBreak() override { }
virtual void paint() override { }
private:
static Wt::WPointF segmentPoint(const Wt::WPainterPath& path, int segment)
{
const Wt::WPainterPath::Segment& s = path.segments()[segment];
return Wt::WPointF(s.x(), s.y());
}
static bool fequal(double d1, double d2) {
return std::fabs(d1 - d2) < 1E-5;
}
private:
double groupWidth_;
int numGroups_;
int group_;
};
SeriesRenderIterator::SeriesRenderIterator(const WCartesianChart& chart,
WPainter& painter)
: chart_(chart),
painter_(painter),
series_(nullptr)
{ }
void SeriesRenderIterator::startSegment(int currentXSegment,
int currentYSegment,
const WRectF& currentSegmentArea)
{
SeriesIterator::startSegment(currentXSegment, currentYSegment,
currentSegmentArea);
const WAxis& yAxis = chart_.axis(series_->axis());
if (currentYSegment == 0)
maxY_ = DBL_MAX;
else
maxY_ = currentSegmentArea.bottom();
if (currentYSegment == yAxis.segmentCount() - 1)
minY_ = -DBL_MAX;
else
minY_ = currentSegmentArea.top();
}
void SeriesRenderIterator::endSegment()
{
SeriesIterator::endSegment();
seriesRenderer_->paint();
}
bool SeriesRenderIterator::startSeries(const WDataSeries& series,
double groupWidth,
int numBarGroups, int currentBarGroup)
{
seriesRenderer_ = nullptr;
switch (series.type()) {
case SeriesType::Line:
case SeriesType::Curve:
seriesRenderer_ = new LineSeriesRenderer(chart_, painter_, series, *this);
break;
case SeriesType::Bar:
seriesRenderer_ = new BarSeriesRenderer(chart_, painter_, series, *this,
groupWidth,
numBarGroups, currentBarGroup);
default:
break;
}
series_ = &series;
if (seriesRenderer_ != nullptr) painter_.save();
return seriesRenderer_ != nullptr;
}
void SeriesRenderIterator::endSeries()
{
seriesRenderer_->paint();
painter_.restore();
delete seriesRenderer_;
series_ = nullptr;
}
void SeriesRenderIterator::newValue(const WDataSeries& series,
double x, double y,
double stackY,
int xRow, int xColumn,
int yRow, int yColumn)
{
if (Utils::isNaN(x) || Utils::isNaN(y))
seriesRenderer_->addBreak();
else
seriesRenderer_->addValue(x, y, stackY, xRow, xColumn, yRow, yColumn);
}
double SeriesRenderIterator::breakY(double y)
{
if (y < minY_)
return minY_;
else if (y > maxY_)
return maxY_;
else
return y;
}
class LabelRenderIterator : public SeriesIterator
{
public:
LabelRenderIterator(const WCartesianChart& chart, WPainter& painter)
: chart_(chart),
painter_(painter)
{ }
virtual bool startSeries(const WDataSeries& series, double groupWidth,
int numBarGroups, int currentBarGroup) override
{
if (series.isLabelsEnabled(Axis::X) ||
series.isLabelsEnabled(Axis::Y)) {
groupWidth_ = groupWidth;
numGroups_ = numBarGroups;
group_ = currentBarGroup;
return true;
} else
return false;
}
virtual void newValue(const WDataSeries& series, double x, double y,
double stackY,
int xRow, int xColumn,
int yRow, int yColumn) override
{
if (Utils::isNaN(x) || Utils::isNaN(y))
return;
WString text;
if (series.isLabelsEnabled(Axis::X)) {
text = chart_.axis(Axis::X).label(x);
}
if (series.isLabelsEnabled(Axis::Y)) {
if (!text.empty())
text += ": ";
text += chart_.axis(series.axis()).label(y - stackY);
}
if (!text.empty()) {
WPointF point = chart_.map(x, y, series.axis(),
currentXSegment(), currentYSegment());
WPointF p = point;
if (series.type() == SeriesType::Bar) {
double g = numGroups_ + (numGroups_ - 1) * chart_.barMargin();
double width = groupWidth_ / g;
double left = p.x() - groupWidth_ / 2
+ group_ * width * (1 + chart_.barMargin());
p = WPointF(left + width/2, p.y());
}
WFlags<AlignmentFlag> alignment;
if (series.type() == SeriesType::Bar) {
if (y < 0)
alignment = AlignmentFlag::Center | AlignmentFlag::Bottom;
else
alignment = AlignmentFlag::Center | AlignmentFlag::Top;
} else {
alignment = AlignmentFlag::Center | AlignmentFlag::Bottom;
p.setY(p.y() - 3);
}
WCartesianChart &chart = const_cast<WCartesianChart &>(chart_);
WPen oldPen = WPen(chart.textPen_);
chart.textPen_.setColor(series.labelColor());
WTransform t = chart_.zoomRangeTransform();
WTransform ct;
WCartesianChart::TransformMap::const_iterator transformHandle
= chart_.curveTransforms_.find(&series);
if (transformHandle != chart_.curveTransforms_.end()) {
ct = chart_.curveTransform(series);
}
if (series.type() == SeriesType::Bar) {
chart.renderLabel(painter_, text,
chart_.inverseHv((t * ct).map(chart_.hv(p))),
alignment, 0, 3);
} else {
double dx = p.x() - point.x();
double dy = p.y() - point.y();
chart.renderLabel(painter_, text,
chart_.inverseHv((t * ct).translate(WPointF(dx, dy)).map(chart_.hv(point))),
alignment, 0, 3);
}
chart.textPen_ = oldPen;
}
}
private:
const WCartesianChart& chart_;
WPainter& painter_;
double groupWidth_;
int numGroups_;
int group_;
};
class MarkerRenderIterator final : public SeriesIterator
{
public:
MarkerRenderIterator(const WCartesianChart& chart, WPainter& painter)
: chart_(chart),
painter_(painter),
currentScale_(0),
series_(0)
{ }
virtual bool startSeries(const WDataSeries& series, double groupWidth,
int numBarGroups, int currentBarGroup) override
{
marker_ = WPainterPath();
if (series.marker() != MarkerType::None) {
chart_.drawMarker(series, marker_);
painter_.save();
needRestore_ = true;
} else
needRestore_ = false;
return true;
}
virtual void endSeries() override
{
finishPathFragment(*series_);
series_ = 0;
if (needRestore_)
painter_.restore();
}
virtual void newValue(const WDataSeries& series, double x, double y,
double stackY,
int xRow, int xColumn,
int yRow, int yColumn) override
{
if (!Utils::isNaN(x) && !Utils::isNaN(y)) {
WPointF p = chart_.map(x, y, series.axis(),
currentXSegment(), currentYSegment());
if (!marker_.isEmpty()) {
WPen pen = WPen(series.markerPen());
SeriesIterator::setPenColor(pen, series, xRow, xColumn, yRow, yColumn, ItemDataRole::MarkerPenColor);
if (chart_.seriesSelectionEnabled() &&
chart_.selectedSeries() != nullptr &&
chart_.selectedSeries() != &series) {
pen.setColor(WCartesianChart::lightenColor(pen.color()));
}
WBrush brush = WBrush(series.markerBrush());
SeriesIterator::setBrushColor(brush, series, xRow, xColumn, yRow, yColumn, ItemDataRole::MarkerBrushColor);
double scale = calculateMarkerScale(series, xRow, xColumn, yRow, yColumn, series.markerSize());
if (chart_.seriesSelectionEnabled() &&