-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfigure.d
1271 lines (1049 loc) · 37.5 KB
/
figure.d
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
/**This file contains Figure, which is a holds and draws one or more Plots
* onto a drawable surface.
*
* Copyright (C) 2010-2011 David Simcha
*
* License:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
module plot2kill.figure;
import plot2kill.plot, plot2kill.util, std.typetuple, std.random;
version(dfl) {
public import plot2kill.dflwrapper;
} else {
public import plot2kill.gtkwrapper;
}
package enum legendSymbolSize = 15; // 30 by 30 pixels.
package enum legendSymbolTextSpace = 3;
/**A container form for one or more Plot objects.
*
* Examples:
* ---
* auto nums = [1,1,1,1,2,2,3,3,4,5,6,7];
* auto hist = Histogram(nums, 10);
* auto fig = new Figure;
* fig.addPllot(hist);
* fig.title = "A plot";
* fig.xLabel = "X label";
* fig.yLabel = "Y label";
* fig.showAsMain();
* ---
*/
class Figure : FigureBase {
private:
double upperLim = -double.infinity;
double lowerLim = double.infinity;
double leftLim = double.infinity;
double rightLim = -double.infinity;
// These control whether to auto set the axes.
bool userSetXAxis = false;
bool userSetYAxis = false;
bool _horizontalGrid;
bool _verticalGrid;
bool _rotatedXTick;
bool _rotatedYTick;
enum tickPixels = 10;
enum legendMarginHoriz = 20;
enum legendMarginVert = 10;
double xTickLabelWidth;
double yTickLabelWidth;
double tickLabelHeight;
Pen axesPen;
ubyte _gridIntensity = 128;
Pen gridPen;
Font _axesFont;
Font _legendFont;
Color[] _xTickColors;
Color[] _yTickColors;
LegendLocation _legendLoc = LegendLocation.bottom;
void fixTickSizes() {
void fixTickLabelSize(ref double toFix, string[] axisText) {
toFix = 0;
foreach(lbl; axisText) {
auto lblSize = measureText(lbl, _axesFont);
if(lblSize.height > tickLabelHeight) {
tickLabelHeight = lblSize.height;
}
if(lblSize.width > toFix) {
toFix = lblSize.width;
}
}
}
tickLabelHeight = 0;
fixTickLabelSize(xTickLabelWidth, xAxisText);
fixTickLabelSize(yTickLabelWidth, yAxisText);
}
void fixMargins() {
fixTickSizes();
immutable legendMeasure = measureLegend();
immutable legendHeight = legendMeasure.height;
immutable legendWidth = legendMeasure.width;
immutable xLabelSize = measureText(xLabel(), xLabelFont());
immutable bottomTickHeight = (rotatedXTick()) ?
xTickLabelWidth : tickLabelHeight;
immutable leftTickWidth = (rotatedYTick()) ?
tickLabelHeight : yTickLabelWidth;
bottomMargin = bottomTickHeight + tickPixels + xLabelSize.height
+ (legendLocation() == LegendLocation.bottom) * legendHeight + 20;
topMargin = measureText(title(), titleFont(), plotWidth).height
+ (legendLocation() == LegendLocation.top) * legendHeight + 20;
leftMargin = measureText(yLabel(), yLabelFont()).height +
tickPixels + leftTickWidth
+ (legendLocation() == LegendLocation.left) * legendWidth + 30;
rightMargin = (legendLocation() == LegendLocation.right)
* legendWidth + 30;
}
Tuple!(double, "height", double, "width", int, "nRows") measureLegend() {
immutable alwaysWrap = legendLocation() == LegendLocation.right ||
legendLocation() == LegendLocation.left;
PlotSize ret = PlotSize(0, 0);
double maxHeight = 0;
double maxWidth = 0;
int nRows = 1;
double rowPos = legendMarginHoriz;
bool shouldReturnZero = true;
foreach(plot; plotData) {
if(plot.hasLegend && plot.legendText().length) {
shouldReturnZero = false;
}
}
if(shouldReturnZero) {
return typeof(return)(0, 0, 0);
}
foreach(plot; plotData) if(plot.hasLegend) {
auto itemSize = plot.measureLegend(legendFont(), this);
maxHeight = max(maxHeight, itemSize.height);
maxWidth = max(maxWidth, itemSize.width);
if(alwaysWrap ||
(itemSize.width + rowPos >= this.width - legendMarginHoriz
&& rowPos > legendMarginHoriz)) {
nRows++;
rowPos = itemSize.width + legendMarginHoriz;
}
rowPos += itemSize.width + legendMarginHoriz;
}
return typeof(return)(
(maxHeight + legendMarginVert) * nRows,
maxWidth + legendMarginHoriz,
nRows
);
}
FigureLine[] extraLines;
final double plotWidth() {
return this.width - leftMargin - rightMargin;
}
final double plotHeight() {
return this.height - topMargin - bottomMargin;
}
mixin(toPixels);
void drawTitle() {
if(nullOrInit(titleFont())) {
return;
}
auto height = measureText(title(), titleFont()).height;
auto rect = PlotRect(leftMargin,
10, this.plotWidth, height);
auto format = TextAlignment.Center;
drawText(title(), titleFont(), getColor(0, 0, 0), rect, format);
}
void drawXlabel() {
if(nullOrInit(xLabelFont())) {
return;
}
immutable textSize = measureText(xLabel(), xLabelFont());
immutable yTop = this.height - textSize.height - 10
- (legendLocation() == LegendLocation.bottom) *
measureLegend().height;
auto rect = PlotRect(leftMargin, yTop,
this.width - leftMargin - rightMargin, textSize.height);
auto format = TextAlignment.Center;
drawText(xLabel(), xLabelFont(), getColor(0, 0, 0), rect, format);
}
void drawYlabel() {
if(nullOrInit(yLabelFont()) || yLabel().length == 0) {
return;
}
immutable textSize = measureText(yLabel(), yLabelFont());
immutable margin = (plotHeight - textSize.width) / 2 + topMargin;
immutable xCoord = 10 + measureLegend().width
* (legendLocation() == LegendLocation.left);
auto rect = PlotRect(xCoord, margin, textSize.height, textSize.width);
drawRotatedText(yLabel(),
yLabelFont(), getColor(0, 0, 0), rect, TextAlignment.Center);
}
void drawExtraLines() {
foreach(line; extraLines) {
auto pen = getPen(line.lineColor, line.lineWidth);
scope(exit) doneWith(pen);
auto start = PlotPoint(toPixelsX(line.x1), toPixelsY(line.y1));
auto end = PlotPoint(toPixelsX(line.x2), toPixelsY(line.y2));
drawClippedLine(pen, start, end);
}
}
void drawAxes() {
immutable origin = PlotPoint(toPixelsX(leftLim), toPixelsY(lowerLim));
immutable topLeft = PlotPoint(origin.x, toPixelsY(upperLim));
immutable bottomRight = PlotPoint(toPixelsX(rightLim), origin.y);
drawLine(axesPen, origin, topLeft);
drawLine(axesPen, origin, bottomRight);
}
void drawTicks() {
auto black = getColor(0, 0, 0);
foreach(i, tickPoint; xAxisLocations) {
if(!(tickPoint >= leftLim && tickPoint <= rightLim)) continue;
auto color = (_xTickColors.length) ? _xTickColors[i] : black;
drawXTick(tickPoint, xAxisText[i], color);
}
foreach(i, tickPoint; yAxisLocations) {
if(!(tickPoint >= lowerLim && tickPoint <= upperLim)) continue;
auto color = (_yTickColors.length) ? _yTickColors[i] : black;
drawYTick(tickPoint, yAxisText[i], color);
}
}
void drawLegend() {
immutable loc = legendLocation();
if(loc == LegendLocation.top || loc == LegendLocation.bottom) {
drawLegendImplTopBottom();
} else {
drawLegendImplLeftRight();
}
}
void drawLegendImplTopBottom() {
immutable measurements = measureLegend();
if(measurements.height == 0) return; // No legend.
immutable rowHeight = measurements.height / measurements.nRows;
// This needs to be precomputed for centering purposes.
double[] rowStarts;
double curX = legendMarginHoriz;
immutable loc = legendLocation();
double curY;
if(loc == LegendLocation.bottom) {
curY = this.height - measurements.height - 10;
} else {
assert(loc == LegendLocation.top);
curY = measureText(title(), titleFont()).height + 10;
}
size_t rowStartIndex = 0;
foreach(plot; plotData) {
if(!plot.legendText.length) continue;
immutable itemSize = plot.measureLegend(legendFont(), this);
if(itemSize.width + curX >= this.width - legendMarginHoriz
&& curX > legendMarginHoriz) {
// Find centering.
auto rowSize = curX - legendMarginHoriz;
rowStarts ~= max(0, (this.width - rowSize) / 2);
curX = legendMarginHoriz;
}
curX += itemSize.width + legendMarginHoriz;
}
// Append last row.
auto rowSize = curX - legendMarginHoriz;
rowStarts ~= max(0, (this.width - rowSize) / 2);
curX = rowStarts[rowStartIndex];
double nextX;
foreach(plot; plotData) {
if(!plot.legendText.length) continue;
immutable itemSize = plot.measureLegend(legendFont(), this);
if(itemSize.width + curX >= this.width - legendMarginHoriz
&& curX > legendMarginHoriz) {
curY += rowHeight;
rowStartIndex++;
curX = rowStarts[rowStartIndex];
nextX = curX;
}
drawLegendElem(curX, curY, plot, rowHeight);
curX += itemSize.width + legendMarginHoriz;
}
}
void drawLegendImplLeftRight() {
int nRows;
foreach(plot; plotData) {
if(plot.legendText().length) nRows++;
}
immutable measurements = measureLegend();
immutable rowHeight = (measurements.height / measurements.nRows);
double curY = this.height / 2 - nRows * rowHeight / 2;
immutable loc = legendLocation();
immutable x = (loc == LegendLocation.left) ? 10 :
(this.width - measurements.width - 10);
foreach(plot; plotData) if(plot.legendText().length) {
drawLegendElem(x, curY, plot, rowHeight);
curY += rowHeight;
}
}
void drawLegendElem(double curX, double curY, Plot plot, double rowHeight) {
immutable textSize = measureText(plot.legendText(), legendFont());
assert(textSize.height <= rowHeight);
immutable stdLetterHeight = measureText("A", legendFont()).height;
immutable textX = curX + legendSymbolSize + legendSymbolTextSpace;
auto textRect = PlotRect(
textX,
curY + rowHeight / 2 - stdLetterHeight / 2,
textSize.width,
textSize.height
);
drawText(plot.legendText(), legendFont(), getColor(0, 0, 0), textRect,
TextAlignment.Left);
auto ySlack = (stdLetterHeight - legendSymbolSize) / 2;
auto where = PlotRect(
curX, curY + rowHeight / 2 - legendSymbolSize / 2,
legendSymbolSize, legendSymbolSize
);
plot.drawLegendSymbol(this, where);
}
// Controls the space between a tick line and the tick label.
enum lineLabelSpace = 2;
void drawXTick(double where, string text, Color color) {
immutable wherePixels = toPixelsX(where);
drawLine(
axesPen,
PlotPoint(wherePixels, this.height - bottomMargin),
PlotPoint(wherePixels, this.height - bottomMargin + tickPixels)
);
if(verticalGrid()) {
drawLine(gridPen,
PlotPoint(wherePixels, topMargin),
PlotPoint(wherePixels, this.height - bottomMargin));
}
if(nullOrInit(_axesFont)) {
return;
}
auto format = TextAlignment.Center;
immutable textSize = measureText(text, _axesFont, format);
immutable tickTextStart =
this.height - bottomMargin + tickPixels + lineLabelSpace;
if(rotatedXTick()) {
auto rect = PlotRect(wherePixels - tickLabelHeight / 2,
tickTextStart + tickPixels / 2,
textSize.height,
textSize.width
);
drawRotatedText(text, _axesFont, color, rect, format);
} else {
auto rect = PlotRect(wherePixels - textSize.width / 2,
tickTextStart,
textSize.width,
textSize.height
);
drawText(text, _axesFont, color, rect, format);
}
}
void drawYTick(double where, string text, Color color) {
immutable wherePixels = this.height - toPixelsY(where);
drawLine(
axesPen,
PlotPoint(leftMargin, this.height - wherePixels),
PlotPoint(leftMargin - tickPixels, this.height - wherePixels)
);
if(nullOrInit(_axesFont)) {
return;
}
if(horizontalGrid()) {
drawLine(
gridPen,
PlotPoint(leftMargin, this.height - wherePixels),
PlotPoint(this.width - rightMargin, this.height - wherePixels));
}
auto format = TextAlignment.Right;
immutable textSize = measureText(text, _axesFont, format);
if(rotatedYTick()) {
auto rect = PlotRect(
leftMargin - textSize.height - tickPixels - lineLabelSpace,
this.height - wherePixels - textSize.width / 2,
textSize.height,
textSize.width
);
drawRotatedText(text, _axesFont, color, rect, format);
} else {
auto rect = PlotRect(
leftMargin - textSize.width - tickPixels - lineLabelSpace,
this.height - wherePixels - textSize.height / 2,
textSize.width,
textSize.height
);
drawText(text, _axesFont, color, rect, format);
}
}
// Used in setupAxes() via delegate.
double marginSizeX() {
return leftMargin;
}
// Used in setupAxes() via delegate.
double marginSizeY() {
return topMargin + bottomMargin;
}
void setupAxes(
double lower,
double upper,
ref double[] axisLocations,
ref string[] axisText,
double axisSize,
ref double labelSize,
double delegate() marginSize
)
in {
assert(upper > lower, std.conv.text(lower, '\t', upper));
} body {
immutable diff = upper - lower;
double tickWidth = 10.0 ^^ floor(log10(diff));
if(diff / tickWidth < 2) {
tickWidth /= 10;
}
if(diff / tickWidth > 9) {
tickWidth *= 2;
}
if(diff / tickWidth < 4) {
tickWidth /= 2;
}
void updateAxes() {
double startPoint = ceil(lower / tickWidth) * tickWidth;
// The tickWidth * 0.01 is a fudge factor to make the last tick
// get drawn in the presence of rounding error.
axisLocations = array(
iota(startPoint, upper + tickWidth * 0.01, tickWidth)
);
axisText = doublesToStrings(axisLocations);
fixMargins();
}
do {
updateAxes();
// Prevent labels from running together on small plots.
if((axisSize - marginSize()) / axisLocations.length < labelSize * 4
&& diff / tickWidth > 2) {
tickWidth *= 2;
continue;
} else {
break;
}
} while(true);
// Force at least two ticks come hell or high water.
while(axisLocations.length < 2) {
tickWidth /= 2;
updateAxes();
}
}
void setLim(
double lower,
double upper,
ref double oldLower,
ref double oldUpper,
) {
enforce(upper > lower, "Can't have upper limit < lower limit.");
oldLower = lower;
oldUpper = upper;
}
void nullFontsToDefaults() {
if(nullOrInit(titleFont())) {
_titleFont = getFont(plot2kill.util.defaultFont, 14 + fontSizeAdjust);
}
if(nullOrInit(xLabelFont())) {
_xLabelFont = getFont(plot2kill.util.defaultFont, 14 + fontSizeAdjust);
}
if(nullOrInit(yLabelFont())) {
_yLabelFont = getFont
(plot2kill.util.defaultFont, 14 + fontSizeAdjust);
}
if(nullOrInit(axesFont())) {
_axesFont = getFont(plot2kill.util.defaultFont, 12 + fontSizeAdjust);
}
if(nullOrInit(legendFont())) {
_legendFont = getFont(plot2kill.util.defaultFont, 12 + fontSizeAdjust);
}
}
static bool isValidPlot(Plot plot) {
if(plot is null) {
return false;
}
return plot.leftMost <= plot.rightMost &&
plot.bottomMost <= plot.topMost;
}
package:
this() {}
this(Plot[] plots...) {
this();
addPlot!(Figure)(plots);
}
// These goodies need to be known by various GUI-related code, but end
// users have no business fiddling with them.
Plot[] plotData;
double[] xAxisLocations;
string[] xAxisText;
double[] yAxisLocations;
string[] yAxisText;
double topMargin = 10;
double bottomMargin = 10;
double leftMargin = 10;
double rightMargin = 30;
public:
override int defaultWindowWidth() {
return 800;
}
override int defaultWindowHeight() {
return 600;
}
override int minWindowWidth() {
return 400;
}
override int minWindowHeight() {
return 300;
}
// These drawing commands aren't documented for now b/c they're subject
// to change.
// Returns whether any part of the rectangle is on screen.
bool clipRectangle(ref double x, ref double y, ref double width, ref double height) {
// Do clipping.
auto bottom = y + height;
auto right = x + width;
if(x < leftMargin) {
x = leftMargin;
}
if(right > this.width - rightMargin) {
right = this.width - rightMargin;
}
if(y < topMargin) {
y = topMargin;
}
if(bottom > this.height - bottomMargin) {
bottom = this.height - bottomMargin;
}
width = right - x;
height = bottom - y;
return width > 0 && height > 0;
}
// Convenience
static bool between(T, U, V)(T num, U lower, V upper) {
return lower <= num && num <= upper;
}
bool clipLine(ref double x1, ref double y1, ref double x2, ref double y2) {
immutable topPixel = topMargin;
immutable bottomPixel = this.height - bottomMargin - 1;
immutable leftPixel = leftMargin + 1;
immutable rightPixel = this.width - rightMargin;
if(between(x1, leftPixel, rightPixel) &&
between(x2, leftPixel, rightPixel) &&
between(y1, topPixel, bottomPixel) &&
between(y2, topPixel, bottomPixel)) {
return true;
}
// Handle slope of zero or infinity as a special case.
if(x1 == x2) {
if(!between(x1, leftPixel, rightPixel)) {
return false;
} else if(y1 < topPixel && y2 < topPixel) {
return false;
} else if(y1 > bottomPixel && y2 > bottomPixel) {
return false;
}
y1 = max(y1, topPixel);
y1 = min(y1, bottomPixel);
y2 = max(y2, topPixel);
y2 = min(y2, bottomPixel);
return true;
} else if(y1 == y2) {
if(!between(y1, topPixel, bottomPixel)) {
return false;
} else if(x1 < leftPixel && x2 < leftPixel) {
return false;
} else if(x1 > rightPixel && x2 > rightPixel) {
return false;
}
x1 = max(x1, leftPixel);
x1 = min(x1, rightPixel);
x2 = max(x2, leftPixel);
x2 = min(x2, rightPixel);
return true;
}
immutable slope = (y2 - y1) / (x2 - x1);
enum tol = 0; // Compensate for rounding error.
void fixX(ref double x, ref double y) {
if(x < leftPixel) {
immutable diff = leftPixel - x;
x = leftPixel;
y = diff * slope + y;
} else if(x > rightPixel) {
immutable diff = rightPixel - x;
x = rightPixel;
y = diff * slope + y;
}
}
void fixY(ref double x, ref double y) {
if(y < topPixel) {
immutable diff = topPixel - y;
y = topPixel;
x = diff / slope + x;
} else if(y > bottomPixel) {
immutable diff = bottomPixel - y;
y = bottomPixel;
x = diff / slope + x;
}
}
fixX(x1, y1);
fixX(x2, y2);
fixY(x1, y1);
fixY(x2, y2);
// This prevents weird rounding artifacts where a line appears as a
// single point on the edge of the figure.
if(y1 == y2 && x1 == x2 && (
(y1 == topPixel || y1 == bottomPixel) ||
(x1 == leftPixel || x1 == rightPixel))) {
return false;
}
// The minuses and pluses are to deal w/ rounding error.
return between(x1, leftPixel - tol, rightPixel + tol) &&
between(x2, leftPixel - tol, rightPixel + tol) &&
between(y1, topPixel - tol, bottomPixel + tol) &&
between(y2, topPixel - tol, bottomPixel + tol);
}
void drawClippedRectangle
(Pen pen, double x, double y, double width, double height) {
if(clipRectangle(x, y, width, height)) {
drawRectangle(pen, x, y, width, height);
}
}
void drawClippedRectangle(Pen pen, PlotRect r) {
drawClippedRectangle(pen, r.x, r.y, r.width, r.height);
}
void fillClippedRectangle
(Brush brush, double x, double y, double width, double height) {
if(clipRectangle(x, y, width, height)) {
fillRectangle(brush, x, y, width, height);
}
}
void fillClippedRectangle(Brush brush, PlotRect rect) {
fillClippedRectangle(brush, rect.x, rect.y, rect.width, rect.height);
}
void drawClippedLine(Pen pen, PlotPoint from, PlotPoint to) {
auto x1 = from.x;
auto y1 = from.y;
auto x2 = to.x;
auto y2 = to.y;
immutable shouldDraw = clipLine(x1, y1, x2, y2);
if(!shouldDraw) {
return;
}
drawLine(pen, PlotPoint(x1, y1), PlotPoint(x2, y2));
}
bool insideAxes(PlotPoint point) {
if(between(point.x, leftMargin, this.width - rightMargin) &&
between(point.y, topMargin, this.height - bottomMargin)) {
return true;
} else {
return false;
}
}
void drawClippedText(string text, Font font,
Color pointColor, PlotRect rect) {
// To avoid cutting points off of scatter plots, this function only
// checks whether the center of each point is on the graph. Therefore,
// it may allow points to extend slightly off the graph. This is
// annoying, but there's no easy way to fix it w/o risking cutting off
// points.
immutable xMid = rect.x + rect.width / 2;
immutable yMid = rect.y + rect.height / 2;
if(insideAxes(PlotPoint(xMid, yMid))) {
drawText(text, font, pointColor, rect);
}
}
///
final Font axesFont()() {
return _axesFont;
}
///
final This axesFont(this This)(Font newFont) {
_axesFont = newFont;
return cast(This) this;
}
///
final Font legendFont()() {
return _legendFont;
}
///
final This legendFont(this This)(Font newFont) {
_legendFont = newFont;
return cast(This) this;
}
///
static Figure opCall()() {
return new Figure;
}
/**Convenience factory that adds all plots provided to the Figure.*/
static Figure opCall()(Plot[] plots...) {
return new Figure(plots);
}
/// Ditto
static Figure opCall(P)(P[] plots)
if(is(P : Plot) && !is(P == Plot)) {
return new Figure(cast(Plot[]) plots);
}
/**Manually set the X axis limits.
*/
final This xLim(this This)(double newLower, double newUpper) {
setLim(newLower, newUpper, leftLim, rightLim);
return cast(This) this;
}
/**Manually set the Y axis limits.
*/
This yLim(this This)(double newLower, double newUpper) {
setLim(newLower, newUpper, lowerLim, upperLim);
return cast(This) this;
}
/**
Set the zoom back to the default value, i.e. just large enough to fit
everything on the screen.
*/
This defaultZoom(this This)() {
upperLim = -double.infinity;
lowerLim = double.infinity;
leftLim = double.infinity;
rightLim = -double.infinity;
foreach(plot; plotData) {
upperLim = max(upperLim, plot.topMost);
rightLim = max(rightLim, plot.rightMost);
leftLim = min(leftLim, plot.leftMost);
lowerLim = min(lowerLim, plot.bottomMost);
}
return cast(This) this;
}
/**Set the X axis labels. If text is null (default) the axis text is
* just the text of the axis locations. R should be any range with
* length identical to text (unless text is null) and elements implicitly
* convertible to double. If colors is empty, all labels will be made
* black. Otherwise it must be the same length as locations.
*/
This xTickLabels(R, this This)
(R locations, const string[] text = null, Color[] colors = null)
if(isInputRange!R && is(ElementType!R : double)) {
userSetXAxis = true;
xAxisLocations = toDoubleArray(locations);
enforce(colors.length == xAxisLocations.length || colors.length == 0);
this._xTickColors = colors.dup;
if(text.length > 0) {
enforce(text.length == xAxisLocations.length,
"Length mismatch between X axis locations and X axis text.");
xAxisText = text.dup;
} else {
xAxisText = doublesToStrings(xAxisLocations);
}
return cast(This) this;
}
/**
Resets the X tick labels to the default, effectively undoing a call to
xTickLabels.
*/
This defaultXTick(this This)() {
userSetXAxis = false;
return cast(This) this;
}
/**Set the Y axis labels. If text is null (default) the axis text is
* just the text of the axis locations. R should be any range with
* length identical to text (unless text is null) and elements implicitly
* convertible to double. If colors is empty, all labels will be made
* black. Otherwise it must be the same length as locations.
*/
This yTickLabels(R, this This)
(R locations, const string[] text = null, Color[] colors = null)
if(isInputRange!R && is(ElementType!R : double)) {
userSetYAxis = true;
yAxisLocations = toDoubleArray(locations);
enforce(colors.length == xAxisLocations.length || colors.length == 0);
this._yTickColors = colors.dup;
if(text.length > 0) {
enforce(text.length == yAxisLocations.length,
"Length mismatch between Y axis locations and Y axis text.");
yAxisText = text.dup;
} else {
yAxisText = doublesToStrings(yAxisLocations);
}
return cast(This) this;
}
/**
Resets the X tick labels to the default, effectively undoing a call to
xTickLabels.
*/
This defaultYTick(this This)() {
userSetYAxis = false;
return cast(This) this;
}
/**Determines whether vertical gridlines are drawn. Default is false.*/
bool verticalGrid()() {
return _verticalGrid;
}
///
This verticalGrid(this This)(bool val) {
this._verticalGrid = val;
return cast(This) this;
}
/**Determines whether horizontal gridlines are drawn. Default is false.*/
bool horizontalGrid()() {
return _horizontalGrid;
}
///
This horizontalGrid(this This)(bool val) {
this._horizontalGrid = val;
return cast(This) this;
}
/// Grid intensity from zero (pure white) to 255 (pure black).
ubyte gridIntensity()() {
return _gridIntensity;
}
/// Setter.
This gridIntensity(this This)(ubyte newIntensity) {
_gridIntensity = newIntensity;
return cast(This) this;
}
///
LegendLocation legendLocation()() {
return _legendLoc;
}
///
This legendLocation(this This)(LegendLocation newLoc) {
this._legendLoc = newLoc;
return cast(This) this;
}