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