-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathpbPlots.cs
7693 lines (5895 loc) · 194 KB
/
pbPlots.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
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
// Downloaded from https://repo.progsbase.com - Code Developed Using progsbase.
using static System.Math;
public class RGBABitmapImageReference{
public RGBABitmapImage image;
}
public class Rectangle{
public double x1;
public double x2;
public double y1;
public double y2;
}
public class ScatterPlotSeries{
public bool linearInterpolation;
public char [] pointType;
public char [] lineType;
public double lineThickness;
public double [] xs;
public double [] ys;
public RGBA color;
}
public class ScatterPlotSettings{
public ScatterPlotSeries [] scatterPlotSeries;
public bool autoBoundaries;
public double xMax;
public double xMin;
public double yMax;
public double yMin;
public bool autoPadding;
public double xPadding;
public double yPadding;
public char [] xLabel;
public char [] yLabel;
public char [] title;
public bool showGrid;
public RGBA gridColor;
public bool xAxisAuto;
public bool xAxisTop;
public bool xAxisBottom;
public bool yAxisAuto;
public bool yAxisLeft;
public bool yAxisRight;
public double width;
public double height;
}
public class BarPlotSeries{
public double [] ys;
public RGBA color;
}
public class BarPlotSettings{
public double width;
public double height;
public bool autoBoundaries;
public double yMax;
public double yMin;
public bool autoPadding;
public double xPadding;
public double yPadding;
public char [] title;
public bool showGrid;
public RGBA gridColor;
public BarPlotSeries [] barPlotSeries;
public char [] yLabel;
public bool autoColor;
public bool grayscaleAutoColor;
public bool autoSpacing;
public double groupSeparation;
public double barSeparation;
public bool autoLabels;
public StringReference [] xLabels;
public bool barBorder;
}
public class RGBA{
public double r;
public double g;
public double b;
public double a;
}
public class RGBABitmap{
public RGBA [] y;
}
public class RGBABitmapImage{
public RGBABitmap [] x;
}
public class BooleanArrayReference{
public bool [] booleanArray;
}
public class BooleanReference{
public bool booleanValue;
}
public class CharacterReference{
public char characterValue;
}
public class NumberArrayReference{
public double [] numberArray;
}
public class NumberReference{
public double numberValue;
}
public class StringArrayReference{
public StringReference [] stringArray;
}
public class StringReference{
public char [] stringx;
}
public class Chunk{
public double length;
public char [] type;
public double [] data;
public double crc;
}
public class IHDR{
public double Width;
public double Height;
public double BitDepth;
public double ColourType;
public double CompressionMethod;
public double FilterMethod;
public double InterlaceMethod;
}
public class PHYS{
public double pixelsPerMeter;
}
public class PNGImage{
public double [] signature;
public IHDR ihdr;
public ZLIBStruct zlibStruct;
public bool physPresent;
public PHYS phys;
}
public class ZLIBStruct{
public double CMF;
public double CM;
public double CINFO;
public double FLG;
public double FCHECK;
public double FDICT;
public double FLEVEL;
public double [] CompressedDataBlocks;
public double Adler32CheckValue;
}
public class LinkedListNodeStrings{
public bool end;
public char [] value;
public LinkedListNodeStrings next;
}
public class LinkedListStrings{
public LinkedListNodeStrings first;
public LinkedListNodeStrings last;
}
public class LinkedListNodeNumbers{
public LinkedListNodeNumbers next;
public bool end;
public double value;
}
public class LinkedListNumbers{
public LinkedListNodeNumbers first;
public LinkedListNodeNumbers last;
}
public class LinkedListCharacters{
public LinkedListNodeCharacters first;
public LinkedListNodeCharacters last;
}
public class LinkedListNodeCharacters{
public bool end;
public char value;
public LinkedListNodeCharacters next;
}
public class DynamicArrayNumbers{
public double [] array;
public double length;
}
public class pbPlots{
public static bool CropLineWithinBoundary(NumberReference x1Ref, NumberReference y1Ref, NumberReference x2Ref, NumberReference y2Ref, double xMin, double xMax, double yMin, double yMax){
double x1, y1, x2, y2;
bool success, p1In, p2In;
double dx, dy, f1, f2, f3, f4, f;
x1 = x1Ref.numberValue;
y1 = y1Ref.numberValue;
x2 = x2Ref.numberValue;
y2 = y2Ref.numberValue;
p1In = x1 >= xMin && x1 <= xMax && y1 >= yMin && y1 <= yMax;
p2In = x2 >= xMin && x2 <= xMax && y2 >= yMin && y2 <= yMax;
if(p1In && p2In){
success = true;
}else if(!p1In && p2In){
dx = x1 - x2;
dy = y1 - y2;
if(dx != 0d){
f1 = (xMin - x2)/dx;
f2 = (xMax - x2)/dx;
}else{
f1 = 1d;
f2 = 1d;
}
if(dy != 0d){
f3 = (yMin - y2)/dy;
f4 = (yMax - y2)/dy;
}else{
f3 = 1d;
f4 = 1d;
}
if(f1 < 0d){
f1 = 1d;
}
if(f2 < 0d){
f2 = 1d;
}
if(f3 < 0d){
f3 = 1d;
}
if(f4 < 0d){
f4 = 1d;
}
f = Min(f1, Min(f2, Min(f3, f4)));
x1 = x2 + f*dx;
y1 = y2 + f*dy;
success = true;
}else if(p1In && !p2In){
dx = x2 - x1;
dy = y2 - y1;
if(dx != 0d){
f1 = (xMin - x1)/dx;
f2 = (xMax - x1)/dx;
}else{
f1 = 1d;
f2 = 1d;
}
if(dy != 0d){
f3 = (yMin - y1)/dy;
f4 = (yMax - y1)/dy;
}else{
f3 = 1d;
f4 = 1d;
}
if(f1 < 0d){
f1 = 1d;
}
if(f2 < 0d){
f2 = 1d;
}
if(f3 < 0d){
f3 = 1d;
}
if(f4 < 0d){
f4 = 1d;
}
f = Min(f1, Min(f2, Min(f3, f4)));
x2 = x1 + f*dx;
y2 = y1 + f*dy;
success = true;
}else{
success = false;
}
x1Ref.numberValue = x1;
y1Ref.numberValue = y1;
x2Ref.numberValue = x2;
y2Ref.numberValue = y2;
return success;
}
public static double IncrementFromCoordinates(double x1, double y1, double x2, double y2){
return (x2 - x1)/(y2 - y1);
}
public static double InterceptFromCoordinates(double x1, double y1, double x2, double y2){
double a, b;
a = IncrementFromCoordinates(x1, y1, x2, y2);
b = y1 - a*x1;
return b;
}
public static RGBA [] Get8HighContrastColors(){
RGBA [] colors;
colors = new RGBA [8];
colors[0] = CreateRGBColor(3d/256d, 146d/256d, 206d/256d);
colors[1] = CreateRGBColor(253d/256d, 83d/256d, 8d/256d);
colors[2] = CreateRGBColor(102d/256d, 176d/256d, 50d/256d);
colors[3] = CreateRGBColor(208d/256d, 234d/256d, 43d/256d);
colors[4] = CreateRGBColor(167d/256d, 25d/256d, 75d/256d);
colors[5] = CreateRGBColor(254d/256d, 254d/256d, 51d/256d);
colors[6] = CreateRGBColor(134d/256d, 1d/256d, 175d/256d);
colors[7] = CreateRGBColor(251d/256d, 153d/256d, 2d/256d);
return colors;
}
public static void DrawFilledRectangleWithBorder(RGBABitmapImage image, double x, double y, double w, double h, RGBA borderColor, RGBA fillColor){
if(h > 0d && w > 0d){
DrawFilledRectangle(image, x, y, w, h, fillColor);
DrawRectangle1px(image, x, y, w, h, borderColor);
}
}
public static RGBABitmapImageReference CreateRGBABitmapImageReference(){
RGBABitmapImageReference reference;
reference = new RGBABitmapImageReference();
reference.image = new RGBABitmapImage();
reference.image.x = new RGBABitmap [0];
return reference;
}
public static bool RectanglesOverlap(Rectangle r1, Rectangle r2){
bool overlap;
overlap = false;
overlap = overlap || (r2.x1 >= r1.x1 && r2.x1 <= r1.x2 && r2.y1 >= r1.y1 && r2.y1 <= r1.y2);
overlap = overlap || (r2.x2 >= r1.x1 && r2.x2 <= r1.x2 && r2.y1 >= r1.y1 && r2.y1 <= r1.y2);
overlap = overlap || (r2.x1 >= r1.x1 && r2.x1 <= r1.x2 && r2.y2 >= r1.y1 && r2.y2 <= r1.y2);
overlap = overlap || (r2.x2 >= r1.x1 && r2.x2 <= r1.x2 && r2.y2 >= r1.y1 && r2.y2 <= r1.y2);
return overlap;
}
public static Rectangle CreateRectangle(double x1, double y1, double x2, double y2){
Rectangle r;
r = new Rectangle();
r.x1 = x1;
r.y1 = y1;
r.x2 = x2;
r.y2 = y2;
return r;
}
public static void CopyRectangleValues(Rectangle rd, Rectangle rs){
rd.x1 = rs.x1;
rd.y1 = rs.y1;
rd.x2 = rs.x2;
rd.y2 = rs.y2;
}
public static void DrawXLabelsForPriority(double p, double xMin, double oy, double xMax, double xPixelMin, double xPixelMax, NumberReference nextRectangle, RGBA gridLabelColor, RGBABitmapImage canvas, double [] xGridPositions, StringArrayReference xLabels, NumberArrayReference xLabelPriorities, Rectangle [] occupied, bool textOnBottom){
bool overlap, currentOverlaps;
double i, j, x, px, padding;
char [] text;
Rectangle r;
r = new Rectangle();
padding = 10d;
overlap = false;
for(i = 0d; i < xLabels.stringArray.Length; i = i + 1d){
if(xLabelPriorities.numberArray[(int)(i)] == p){
x = xGridPositions[(int)(i)];
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax);
text = xLabels.stringArray[(int)(i)].stringx;
r.x1 = Floor(px - GetTextWidth(text)/2d);
if(textOnBottom){
r.y1 = Floor(oy + 5d);
}else{
r.y1 = Floor(oy - 20d);
}
r.x2 = r.x1 + GetTextWidth(text);
r.y2 = r.y1 + GetTextHeight(text);
/* Add padding*/
r.x1 = r.x1 - padding;
r.y1 = r.y1 - padding;
r.x2 = r.x2 + padding;
r.y2 = r.y2 + padding;
currentOverlaps = false;
for(j = 0d; j < nextRectangle.numberValue; j = j + 1d){
currentOverlaps = currentOverlaps || RectanglesOverlap(r, occupied[(int)(j)]);
}
if(!currentOverlaps && p == 1d){
DrawText(canvas, r.x1 + padding, r.y1 + padding, text, gridLabelColor);
CopyRectangleValues(occupied[(int)(nextRectangle.numberValue)], r);
nextRectangle.numberValue = nextRectangle.numberValue + 1d;
}
overlap = overlap || currentOverlaps;
}
}
if(!overlap && p != 1d){
for(i = 0d; i < xGridPositions.Length; i = i + 1d){
x = xGridPositions[(int)(i)];
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax);
if(xLabelPriorities.numberArray[(int)(i)] == p){
text = xLabels.stringArray[(int)(i)].stringx;
r.x1 = Floor(px - GetTextWidth(text)/2d);
if(textOnBottom){
r.y1 = Floor(oy + 5d);
}else{
r.y1 = Floor(oy - 20d);
}
r.x2 = r.x1 + GetTextWidth(text);
r.y2 = r.y1 + GetTextHeight(text);
DrawText(canvas, r.x1, r.y1, text, gridLabelColor);
CopyRectangleValues(occupied[(int)(nextRectangle.numberValue)], r);
nextRectangle.numberValue = nextRectangle.numberValue + 1d;
}
}
}
}
public static void DrawYLabelsForPriority(double p, double yMin, double ox, double yMax, double yPixelMin, double yPixelMax, NumberReference nextRectangle, RGBA gridLabelColor, RGBABitmapImage canvas, double [] yGridPositions, StringArrayReference yLabels, NumberArrayReference yLabelPriorities, Rectangle [] occupied, bool textOnLeft){
bool overlap, currentOverlaps;
double i, j, y, py, padding;
char [] text;
Rectangle r;
r = new Rectangle();
padding = 10d;
overlap = false;
for(i = 0d; i < yLabels.stringArray.Length; i = i + 1d){
if(yLabelPriorities.numberArray[(int)(i)] == p){
y = yGridPositions[(int)(i)];
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax);
text = yLabels.stringArray[(int)(i)].stringx;
if(textOnLeft){
r.x1 = Floor(ox - GetTextWidth(text) - 10d);
}else{
r.x1 = Floor(ox + 10d);
}
r.y1 = Floor(py - 6d);
r.x2 = r.x1 + GetTextWidth(text);
r.y2 = r.y1 + GetTextHeight(text);
/* Add padding*/
r.x1 = r.x1 - padding;
r.y1 = r.y1 - padding;
r.x2 = r.x2 + padding;
r.y2 = r.y2 + padding;
currentOverlaps = false;
for(j = 0d; j < nextRectangle.numberValue; j = j + 1d){
currentOverlaps = currentOverlaps || RectanglesOverlap(r, occupied[(int)(j)]);
}
/* Draw labels with priority 1 if they do not overlap anything else.*/
if(!currentOverlaps && p == 1d){
DrawText(canvas, r.x1 + padding, r.y1 + padding, text, gridLabelColor);
CopyRectangleValues(occupied[(int)(nextRectangle.numberValue)], r);
nextRectangle.numberValue = nextRectangle.numberValue + 1d;
}
overlap = overlap || currentOverlaps;
}
}
if(!overlap && p != 1d){
for(i = 0d; i < yGridPositions.Length; i = i + 1d){
y = yGridPositions[(int)(i)];
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax);
if(yLabelPriorities.numberArray[(int)(i)] == p){
text = yLabels.stringArray[(int)(i)].stringx;
if(textOnLeft){
r.x1 = Floor(ox - GetTextWidth(text) - 10d);
}else{
r.x1 = Floor(ox + 10d);
}
r.y1 = Floor(py - 6d);
r.x2 = r.x1 + GetTextWidth(text);
r.y2 = r.y1 + GetTextHeight(text);
DrawText(canvas, r.x1, r.y1, text, gridLabelColor);
CopyRectangleValues(occupied[(int)(nextRectangle.numberValue)], r);
nextRectangle.numberValue = nextRectangle.numberValue + 1d;
}
}
}
}
public static double [] ComputeGridLinePositions(double cMin, double cMax, StringArrayReference labels, NumberArrayReference priorities){
double [] positions;
double cLength, p, pMin, pMax, pInterval, pNum, i, num, rem, priority, mode;
cLength = cMax - cMin;
p = Floor(Log10(cLength));
pInterval = Pow(10d, p);
/* gives 10-1 lines for 100-10 diff*/
pMin = Ceiling(cMin/pInterval)*pInterval;
pMax = Floor(cMax/pInterval)*pInterval;
pNum = Roundx((pMax - pMin)/pInterval + 1d);
mode = 1d;
if(pNum <= 3d){
p = Floor(Log10(cLength) - 1d);
/* gives 100-10 lines for 100-10 diff*/
pInterval = Pow(10d, p);
pMin = Ceiling(cMin/pInterval)*pInterval;
pMax = Floor(cMax/pInterval)*pInterval;
pNum = Roundx((pMax - pMin)/pInterval + 1d);
mode = 4d;
}else if(pNum <= 6d){
p = Floor(Log10(cLength));
pInterval = Pow(10d, p)/4d;
/* gives 40-5 lines for 100-10 diff*/
pMin = Ceiling(cMin/pInterval)*pInterval;
pMax = Floor(cMax/pInterval)*pInterval;
pNum = Roundx((pMax - pMin)/pInterval + 1d);
mode = 3d;
}else if(pNum <= 10d){
p = Floor(Log10(cLength));
pInterval = Pow(10d, p)/2d;
/* gives 20-3 lines for 100-10 diff*/
pMin = Ceiling(cMin/pInterval)*pInterval;
pMax = Floor(cMax/pInterval)*pInterval;
pNum = Roundx((pMax - pMin)/pInterval + 1d);
mode = 2d;
}
positions = new double [(int)(pNum)];
labels.stringArray = new StringReference [(int)(pNum)];
priorities.numberArray = new double [(int)(pNum)];
for(i = 0d; i < pNum; i = i + 1d){
num = pMin + pInterval*i;
positions[(int)(i)] = num;
/* Always print priority 1 labels. Only draw priority 2 if they can all be drawn. Then, only draw priority 3 if they can all be drawn.*/
priority = 1d;
/* Prioritize x.25, x.5 and x.75 lower.*/
if(mode == 2d || mode == 3d){
rem = Abs((double)Round(num/Pow(10d, p - 2d)))%100d;
priority = 1d;
if(rem == 50d){
priority = 2d;
}else if(rem == 25d || rem == 75d){
priority = 3d;
}
}
/* Prioritize x.1-x.4 and x.6-x.9 lower*/
if(mode == 4d){
rem = Abs(Roundx(num/Pow(10d, p)))%10d;
priority = 1d;
if(rem == 1d || rem == 2d || rem == 3d || rem == 4d || rem == 6d || rem == 7d || rem == 8d || rem == 9d){
priority = 2d;
}
}
/* 0 has lowest priority.*/
if(EpsilonCompare(num, 0d, Pow(10d, p - 5d))){
priority = 3d;
}
priorities.numberArray[(int)(i)] = priority;
/* The label itself.*/
labels.stringArray[(int)(i)] = new StringReference();
if(p < 0d){
if(mode == 2d || mode == 3d){
num = RoundToDigits(num, -(p - 1d));
}else{
num = RoundToDigits(num, -p);
}
}
labels.stringArray[(int)(i)].stringx = CreateStringDecimalFromNumber(num);
}
return positions;
}
public static double MapYCoordinate(double y, double yMin, double yMax, double yPixelMin, double yPixelMax){
double yLength, yPixelLength;
yLength = yMax - yMin;
yPixelLength = yPixelMax - yPixelMin;
y = y - yMin;
y = y*yPixelLength/yLength;
y = yPixelLength - y;
y = y + yPixelMin;
return y;
}
public static double MapXCoordinate(double x, double xMin, double xMax, double xPixelMin, double xPixelMax){
double xLength, xPixelLength;
xLength = xMax - xMin;
xPixelLength = xPixelMax - xPixelMin;
x = x - xMin;
x = x*xPixelLength/xLength;
x = x + xPixelMin;
return x;
}
public static double MapXCoordinateAutoSettings(double x, RGBABitmapImage image, double [] xs){
return MapXCoordinate(x, GetMinimum(xs), GetMaximum(xs), GetDefaultPaddingPercentage()*ImageWidth(image), (1d - GetDefaultPaddingPercentage())*ImageWidth(image));
}
public static double MapYCoordinateAutoSettings(double y, RGBABitmapImage image, double [] ys){
return MapYCoordinate(y, GetMinimum(ys), GetMaximum(ys), GetDefaultPaddingPercentage()*ImageHeight(image), (1d - GetDefaultPaddingPercentage())*ImageHeight(image));
}
public static double MapXCoordinateBasedOnSettings(double x, ScatterPlotSettings settings){
double xMin, xMax, xPadding, xPixelMin, xPixelMax;
Rectangle boundaries;
boundaries = new Rectangle();
ComputeBoundariesBasedOnSettings(settings, boundaries);
xMin = boundaries.x1;
xMax = boundaries.x2;
if(settings.autoPadding){
xPadding = Floor(GetDefaultPaddingPercentage()*settings.width);
}else{
xPadding = settings.xPadding;
}
xPixelMin = xPadding;
xPixelMax = settings.width - xPadding;
return MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax);
}
public static double MapYCoordinateBasedOnSettings(double y, ScatterPlotSettings settings){
double yMin, yMax, yPadding, yPixelMin, yPixelMax;
Rectangle boundaries;
boundaries = new Rectangle();
ComputeBoundariesBasedOnSettings(settings, boundaries);
yMin = boundaries.y1;
yMax = boundaries.y2;
if(settings.autoPadding){
yPadding = Floor(GetDefaultPaddingPercentage()*settings.height);
}else{
yPadding = settings.yPadding;
}
yPixelMin = yPadding;
yPixelMax = settings.height - yPadding;
return MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax);
}
public static double GetDefaultPaddingPercentage(){
return 0.10;
}
public static void DrawText(RGBABitmapImage canvas, double x, double y, char [] text, RGBA color){
double i, charWidth, spacing;
charWidth = 8d;
spacing = 2d;
for(i = 0d; i < text.Length; i = i + 1d){
DrawAsciiCharacter(canvas, x + i*(charWidth + spacing), y, text[(int)(i)], color);
}
}
public static void DrawTextUpwards(RGBABitmapImage canvas, double x, double y, char [] text, RGBA color){
RGBABitmapImage buffer, rotated;
buffer = CreateImage(GetTextWidth(text), GetTextHeight(text), GetTransparent());
DrawText(buffer, 0d, 0d, text, color);
rotated = RotateAntiClockwise90Degrees(buffer);
DrawImageOnImage(canvas, rotated, x, y);
DeleteImage(buffer);
DeleteImage(rotated);
}
public static ScatterPlotSettings GetDefaultScatterPlotSettings(){
ScatterPlotSettings settings;
settings = new ScatterPlotSettings();
settings.autoBoundaries = true;
settings.xMax = 0d;
settings.xMin = 0d;
settings.yMax = 0d;
settings.yMin = 0d;
settings.autoPadding = true;
settings.xPadding = 0d;
settings.yPadding = 0d;
settings.title = "".ToCharArray();
settings.xLabel = "".ToCharArray();
settings.yLabel = "".ToCharArray();
settings.scatterPlotSeries = new ScatterPlotSeries [0];
settings.showGrid = true;
settings.gridColor = GetGray(0.1);
settings.xAxisAuto = true;
settings.xAxisTop = false;
settings.xAxisBottom = false;
settings.yAxisAuto = true;
settings.yAxisLeft = false;
settings.yAxisRight = false;
return settings;
}
public static ScatterPlotSeries GetDefaultScatterPlotSeriesSettings(){
ScatterPlotSeries series;
series = new ScatterPlotSeries();
series.linearInterpolation = true;
series.pointType = "pixels".ToCharArray();
series.lineType = "solid".ToCharArray();
series.lineThickness = 1d;
series.xs = new double [0];
series.ys = new double [0];
series.color = GetBlack();
return series;
}
public static bool DrawScatterPlot(RGBABitmapImageReference canvasReference, double width, double height, double [] xs, double [] ys, StringReference errorMessage){
ScatterPlotSettings settings;
bool success;
settings = GetDefaultScatterPlotSettings();
settings.width = width;
settings.height = height;
settings.scatterPlotSeries = new ScatterPlotSeries [1];
settings.scatterPlotSeries[0] = GetDefaultScatterPlotSeriesSettings();
delete(settings.scatterPlotSeries[0].xs);
settings.scatterPlotSeries[0].xs = xs;
delete(settings.scatterPlotSeries[0].ys);
settings.scatterPlotSeries[0].ys = ys;
success = DrawScatterPlotFromSettings(canvasReference, settings, errorMessage);
return success;
}
public static bool DrawScatterPlotFromSettings(RGBABitmapImageReference canvasReference, ScatterPlotSettings settings, StringReference errorMessage){
double xMin, xMax, yMin, yMax, xLength, yLength, i, x, y, xPrev, yPrev, px, py, pxPrev, pyPrev, originX, originY, p, l, plot;
Rectangle boundaries;
double xPadding, yPadding, originXPixels, originYPixels;
double xPixelMin, yPixelMin, xPixelMax, yPixelMax, xLengthPixels, yLengthPixels, axisLabelPadding;
NumberReference nextRectangle, x1Ref, y1Ref, x2Ref, y2Ref, patternOffset;
bool prevSet, success;
RGBA gridLabelColor;
RGBABitmapImage canvas;
double [] xs, ys;
bool linearInterpolation;
ScatterPlotSeries sp;
double [] xGridPositions, yGridPositions;
StringArrayReference xLabels, yLabels;
NumberArrayReference xLabelPriorities, yLabelPriorities;
Rectangle [] occupied;
bool [] linePattern;
bool originXInside, originYInside, textOnLeft, textOnBottom;
double originTextX, originTextY, originTextXPixels, originTextYPixels, side;
canvas = CreateImage(settings.width, settings.height, GetWhite());
patternOffset = CreateNumberReference(0d);
success = ScatterPlotFromSettingsValid(settings, errorMessage);
if(success){
boundaries = new Rectangle();
ComputeBoundariesBasedOnSettings(settings, boundaries);
xMin = boundaries.x1;
yMin = boundaries.y1;
xMax = boundaries.x2;
yMax = boundaries.y2;
/* If zero, set to defaults.*/
if(xMin - xMax == 0d){
xMin = 0d;
xMax = 10d;
}
if(yMin - yMax == 0d){
yMin = 0d;
yMax = 10d;
}
xLength = xMax - xMin;
yLength = yMax - yMin;
if(settings.autoPadding){
xPadding = Floor(GetDefaultPaddingPercentage()*settings.width);
yPadding = Floor(GetDefaultPaddingPercentage()*settings.height);
}else{
xPadding = settings.xPadding;
yPadding = settings.yPadding;
}
/* Draw title*/
DrawText(canvas, Floor(settings.width/2d - GetTextWidth(settings.title)/2d), Floor(yPadding/3d), settings.title, GetBlack());
/* Draw grid*/
xPixelMin = xPadding;
yPixelMin = yPadding;
xPixelMax = settings.width - xPadding;
yPixelMax = settings.height - yPadding;
xLengthPixels = xPixelMax - xPixelMin;
yLengthPixels = yPixelMax - yPixelMin;
DrawRectangle1px(canvas, xPixelMin, yPixelMin, xLengthPixels, yLengthPixels, settings.gridColor);
gridLabelColor = GetGray(0.5);
xLabels = new StringArrayReference();
xLabelPriorities = new NumberArrayReference();
yLabels = new StringArrayReference();
yLabelPriorities = new NumberArrayReference();
xGridPositions = ComputeGridLinePositions(xMin, xMax, xLabels, xLabelPriorities);
yGridPositions = ComputeGridLinePositions(yMin, yMax, yLabels, yLabelPriorities);
if(settings.showGrid){
/* X-grid*/
for(i = 0d; i < xGridPositions.Length; i = i + 1d){
x = xGridPositions[(int)(i)];
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax);
DrawLine1px(canvas, px, yPixelMin, px, yPixelMax, settings.gridColor);
}
/* Y-grid*/
for(i = 0d; i < yGridPositions.Length; i = i + 1d){
y = yGridPositions[(int)(i)];
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax);
DrawLine1px(canvas, xPixelMin, py, xPixelMax, py, settings.gridColor);
}
}
/* Compute origin information.*/
originYInside = yMin < 0d && yMax > 0d;
originY = 0d;
if(settings.xAxisAuto){
if(originYInside){
originY = 0d;
}else{
originY = yMin;
}
}else{
if(settings.xAxisTop){
originY = yMax;
}
if(settings.xAxisBottom){
originY = yMin;
}
}
originYPixels = MapYCoordinate(originY, yMin, yMax, yPixelMin, yPixelMax);
originXInside = xMin < 0d && xMax > 0d;
originX = 0d;
if(settings.yAxisAuto){
if(originXInside){
originX = 0d;
}else{
originX = xMin;
}
}else{
if(settings.yAxisLeft){
originX = xMin;
}
if(settings.yAxisRight){
originX = xMax;
}
}
originXPixels = MapXCoordinate(originX, xMin, xMax, xPixelMin, xPixelMax);
if(originYInside){
originTextY = 0d;
}else{
originTextY = yMin + yLength/2d;
}
originTextYPixels = MapYCoordinate(originTextY, yMin, yMax, yPixelMin, yPixelMax);
if(originXInside){
originTextX = 0d;
}else{
originTextX = xMin + xLength/2d;
}
originTextXPixels = MapXCoordinate(originTextX, xMin, xMax, xPixelMin, xPixelMax);
/* Labels*/
occupied = new Rectangle [(int)(xLabels.stringArray.Length + yLabels.stringArray.Length)];
for(i = 0d; i < occupied.Length; i = i + 1d){
occupied[(int)(i)] = CreateRectangle(0d, 0d, 0d, 0d);
}
nextRectangle = CreateNumberReference(0d);
/* x labels*/
for(i = 1d; i <= 5d; i = i + 1d){
textOnBottom = true;
if(!settings.xAxisAuto && settings.xAxisTop){
textOnBottom = false;
}
DrawXLabelsForPriority(i, xMin, originYPixels, xMax, xPixelMin, xPixelMax, nextRectangle, gridLabelColor, canvas, xGridPositions, xLabels, xLabelPriorities, occupied, textOnBottom);
}
/* y labels*/
for(i = 1d; i <= 5d; i = i + 1d){
textOnLeft = true;
if(!settings.yAxisAuto && settings.yAxisRight){
textOnLeft = false;
}
DrawYLabelsForPriority(i, yMin, originXPixels, yMax, yPixelMin, yPixelMax, nextRectangle, gridLabelColor, canvas, yGridPositions, yLabels, yLabelPriorities, occupied, textOnLeft);
}
/* Draw origin line axis titles.*/
axisLabelPadding = 20d;
/* x origin line*/
if(originYInside){
DrawLine1px(canvas, Roundx(xPixelMin), Roundx(originYPixels), Roundx(xPixelMax), Roundx(originYPixels), GetBlack());
}
/* y origin line*/
if(originXInside){
DrawLine1px(canvas, Roundx(originXPixels), Roundx(yPixelMin), Roundx(originXPixels), Roundx(yPixelMax), GetBlack());
}
/* Draw origin axis titles.*/
DrawTextUpwards(canvas, 10d, Floor(originTextYPixels - GetTextWidth(settings.yLabel)/2d), settings.yLabel, GetBlack());
DrawText(canvas, Floor(originTextXPixels - GetTextWidth(settings.xLabel)/2d), yPixelMax + axisLabelPadding, settings.xLabel, GetBlack());
/* X-grid-markers*/
for(i = 0d; i < xGridPositions.Length; i = i + 1d){
x = xGridPositions[(int)(i)];
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax);
p = xLabelPriorities.numberArray[(int)(i)];
l = 1d;
if(p == 1d){
l = 8d;
}else if(p == 2d){
l = 3d;
}
side = -1d;
if(!settings.xAxisAuto && settings.xAxisTop){
side = 1d;
}
DrawLine1px(canvas, px, originYPixels, px, originYPixels + side*l, GetBlack());
}
/* Y-grid-markers*/
for(i = 0d; i < yGridPositions.Length; i = i + 1d){
y = yGridPositions[(int)(i)];
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax);
p = yLabelPriorities.numberArray[(int)(i)];
l = 1d;
if(p == 1d){