-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathpbPlots.py
6463 lines (4811 loc) · 184 KB
/
pbPlots.py
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.
from math import *
class RGBABitmapImageReference:
image = None
class Rectangle:
x1 = None
x2 = None
y1 = None
y2 = None
class ScatterPlotSeries:
linearInterpolation = None
pointType = None
lineType = None
lineThickness = None
xs = None
ys = None
color = None
class ScatterPlotSettings:
scatterPlotSeries = None
autoBoundaries = None
xMax = None
xMin = None
yMax = None
yMin = None
autoPadding = None
xPadding = None
yPadding = None
xLabel = None
yLabel = None
title = None
showGrid = None
gridColor = None
xAxisAuto = None
xAxisTop = None
xAxisBottom = None
yAxisAuto = None
yAxisLeft = None
yAxisRight = None
width = None
height = None
class BarPlotSeries:
ys = None
color = None
class BarPlotSettings:
width = None
height = None
autoBoundaries = None
yMax = None
yMin = None
autoPadding = None
xPadding = None
yPadding = None
title = None
showGrid = None
gridColor = None
barPlotSeries = None
yLabel = None
autoColor = None
grayscaleAutoColor = None
autoSpacing = None
groupSeparation = None
barSeparation = None
autoLabels = None
xLabels = None
barBorder = None
def CropLineWithinBoundary(x1Ref, y1Ref, x2Ref, y2Ref, xMin, xMax, yMin, yMax):
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
elif not p1In and p2In:
dx = x1 - x2
dy = y1 - y2
if dx != 0.0:
f1 = (xMin - x2)/dx
f2 = (xMax - x2)/dx
else:
f1 = 1.0
f2 = 1.0
if dy != 0.0:
f3 = (yMin - y2)/dy
f4 = (yMax - y2)/dy
else:
f3 = 1.0
f4 = 1.0
if f1 < 0.0:
f1 = 1.0
if f2 < 0.0:
f2 = 1.0
if f3 < 0.0:
f3 = 1.0
if f4 < 0.0:
f4 = 1.0
f = min(f1,min(f2,min(f3,f4)))
x1 = x2 + f*dx
y1 = y2 + f*dy
success = True
elif p1In and not p2In :
dx = x2 - x1
dy = y2 - y1
if dx != 0.0:
f1 = (xMin - x1)/dx
f2 = (xMax - x1)/dx
else:
f1 = 1.0
f2 = 1.0
if dy != 0.0:
f3 = (yMin - y1)/dy
f4 = (yMax - y1)/dy
else:
f3 = 1.0
f4 = 1.0
if f1 < 0.0:
f1 = 1.0
if f2 < 0.0:
f2 = 1.0
if f3 < 0.0:
f3 = 1.0
if f4 < 0.0:
f4 = 1.0
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
def IncrementFromCoordinates(x1, y1, x2, y2):
return (x2 - x1)/(y2 - y1)
def InterceptFromCoordinates(x1, y1, x2, y2):
a = IncrementFromCoordinates(x1, y1, x2, y2)
b = y1 - a*x1
return b
def Get8HighContrastColors():
colors = [None]*int(8.0)
colors[int(0.0)] = CreateRGBColor(3.0/256.0, 146.0/256.0, 206.0/256.0)
colors[int(1.0)] = CreateRGBColor(253.0/256.0, 83.0/256.0, 8.0/256.0)
colors[int(2.0)] = CreateRGBColor(102.0/256.0, 176.0/256.0, 50.0/256.0)
colors[int(3.0)] = CreateRGBColor(208.0/256.0, 234.0/256.0, 43.0/256.0)
colors[int(4.0)] = CreateRGBColor(167.0/256.0, 25.0/256.0, 75.0/256.0)
colors[int(5.0)] = CreateRGBColor(254.0/256.0, 254.0/256.0, 51.0/256.0)
colors[int(6.0)] = CreateRGBColor(134.0/256.0, 1.0/256.0, 175.0/256.0)
colors[int(7.0)] = CreateRGBColor(251.0/256.0, 153.0/256.0, 2.0/256.0)
return colors
def DrawFilledRectangleWithBorder(image, x, y, w, h, borderColor, fillColor):
if h > 0.0 and w > 0.0:
DrawFilledRectangle(image, x, y, w, h, fillColor)
DrawRectangle1px(image, x, y, w, h, borderColor)
def CreateRGBABitmapImageReference():
reference = RGBABitmapImageReference()
reference.image = RGBABitmapImage()
reference.image.x = [None]*int(0.0)
return reference
def RectanglesOverlap(r1, r2):
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
def CreateRectangle(x1, y1, x2, y2):
r = Rectangle()
r.x1 = x1
r.y1 = y1
r.x2 = x2
r.y2 = y2
return r
def CopyRectangleValues(rd, rs):
rd.x1 = rs.x1
rd.y1 = rs.y1
rd.x2 = rs.x2
rd.y2 = rs.y2
def DrawXLabelsForPriority(p, xMin, oy, xMax, xPixelMin, xPixelMax, nextRectangle, gridLabelColor, canvas, xGridPositions, xLabels, xLabelPriorities, occupied, textOnBottom):
r = Rectangle()
padding = 10.0
overlap = False
i = 0.0
while i < len(xLabels.stringArray):
if xLabelPriorities.numberArray[int(i)] == p:
x = xGridPositions[int(i)]
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax)
text = xLabels.stringArray[int(i)].string
r.x1 = floor(px - GetTextWidth(text)/2.0)
if textOnBottom:
r.y1 = floor(oy + 5.0)
else:
r.y1 = floor(oy - 20.0)
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.0
while j < nextRectangle.numberValue:
currentOverlaps = currentOverlaps or RectanglesOverlap(r, occupied[int(j)])
j = j + 1.0
if not currentOverlaps and p == 1.0:
DrawText(canvas, r.x1 + padding, r.y1 + padding, text, gridLabelColor)
CopyRectangleValues(occupied[int(nextRectangle.numberValue)], r)
nextRectangle.numberValue = nextRectangle.numberValue + 1.0
overlap = overlap or currentOverlaps
i = i + 1.0
if not overlap and p != 1.0:
i = 0.0
while i < len(xGridPositions):
x = xGridPositions[int(i)]
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax)
if xLabelPriorities.numberArray[int(i)] == p:
text = xLabels.stringArray[int(i)].string
r.x1 = floor(px - GetTextWidth(text)/2.0)
if textOnBottom:
r.y1 = floor(oy + 5.0)
else:
r.y1 = floor(oy - 20.0)
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 + 1.0
i = i + 1.0
def DrawYLabelsForPriority(p, yMin, ox, yMax, yPixelMin, yPixelMax, nextRectangle, gridLabelColor, canvas, yGridPositions, yLabels, yLabelPriorities, occupied, textOnLeft):
r = Rectangle()
padding = 10.0
overlap = False
i = 0.0
while i < len(yLabels.stringArray):
if yLabelPriorities.numberArray[int(i)] == p:
y = yGridPositions[int(i)]
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax)
text = yLabels.stringArray[int(i)].string
if textOnLeft:
r.x1 = floor(ox - GetTextWidth(text) - 10.0)
else:
r.x1 = floor(ox + 10.0)
r.y1 = floor(py - 6.0)
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.0
while j < nextRectangle.numberValue:
currentOverlaps = currentOverlaps or RectanglesOverlap(r, occupied[int(j)])
j = j + 1.0
# Draw labels with priority 1 if they do not overlap anything else.
if not currentOverlaps and p == 1.0:
DrawText(canvas, r.x1 + padding, r.y1 + padding, text, gridLabelColor)
CopyRectangleValues(occupied[int(nextRectangle.numberValue)], r)
nextRectangle.numberValue = nextRectangle.numberValue + 1.0
overlap = overlap or currentOverlaps
i = i + 1.0
if not overlap and p != 1.0:
i = 0.0
while i < len(yGridPositions):
y = yGridPositions[int(i)]
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax)
if yLabelPriorities.numberArray[int(i)] == p:
text = yLabels.stringArray[int(i)].string
if textOnLeft:
r.x1 = floor(ox - GetTextWidth(text) - 10.0)
else:
r.x1 = floor(ox + 10.0)
r.y1 = floor(py - 6.0)
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 + 1.0
i = i + 1.0
def ComputeGridLinePositions(cMin, cMax, labels, priorities):
cLength = cMax - cMin
p = floor(log10(cLength))
pInterval = 10.0**p
# gives 10-1 lines for 100-10 diff
pMin = ceil(cMin/pInterval)*pInterval
pMax = floor(cMax/pInterval)*pInterval
pNum = Round((pMax - pMin)/pInterval + 1.0)
mode = 1.0
if pNum <= 3.0:
p = floor(log10(cLength) - 1.0)
# gives 100-10 lines for 100-10 diff
pInterval = 10.0**p
pMin = ceil(cMin/pInterval)*pInterval
pMax = floor(cMax/pInterval)*pInterval
pNum = Round((pMax - pMin)/pInterval + 1.0)
mode = 4.0
elif pNum <= 6.0:
p = floor(log10(cLength))
pInterval = 10.0**p/4.0
# gives 40-5 lines for 100-10 diff
pMin = ceil(cMin/pInterval)*pInterval
pMax = floor(cMax/pInterval)*pInterval
pNum = Round((pMax - pMin)/pInterval + 1.0)
mode = 3.0
elif pNum <= 10.0:
p = floor(log10(cLength))
pInterval = 10.0**p/2.0
# gives 20-3 lines for 100-10 diff
pMin = ceil(cMin/pInterval)*pInterval
pMax = floor(cMax/pInterval)*pInterval
pNum = Round((pMax - pMin)/pInterval + 1.0)
mode = 2.0
positions = [None]*int(pNum)
labels.stringArray = [None]*int(pNum)
priorities.numberArray = [None]*int(pNum)
i = 0.0
while i < pNum:
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 = 1.0
# Prioritize x.25, x.5 and x.75 lower.
if mode == 2.0 or mode == 3.0:
rem = fabs(round(num/10.0**(p - 2.0))) % 100.0
priority = 1.0
if rem == 50.0:
priority = 2.0
elif rem == 25.0 or rem == 75.0:
priority = 3.0
# Prioritize x.1-x.4 and x.6-x.9 lower
if mode == 4.0:
rem = fabs(Round(num/10.0**p)) % 10.0
priority = 1.0
if rem == 1.0 or rem == 2.0 or rem == 3.0 or rem == 4.0 or rem == 6.0 or rem == 7.0 or rem == 8.0 or rem == 9.0:
priority = 2.0
# 0 has lowest priority.
if EpsilonCompare(num, 0.0, 10.0**(p - 5.0)):
priority = 3.0
priorities.numberArray[int(i)] = priority
# The label itself.
labels.stringArray[int(i)] = StringReference()
if p < 0.0:
if mode == 2.0 or mode == 3.0:
num = RoundToDigits(num, -(p - 1.0))
else:
num = RoundToDigits(num, -p)
labels.stringArray[int(i)].string = CreateStringDecimalFromNumber(num)
i = i + 1.0
return positions
def MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax):
yLength = yMax - yMin
yPixelLength = yPixelMax - yPixelMin
y = y - yMin
y = y*yPixelLength/yLength
y = yPixelLength - y
y = y + yPixelMin
return y
def MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax):
xLength = xMax - xMin
xPixelLength = xPixelMax - xPixelMin
x = x - xMin
x = x*xPixelLength/xLength
x = x + xPixelMin
return x
def MapXCoordinateAutoSettings(x, image, xs):
return MapXCoordinate(x, GetMinimum(xs), GetMaximum(xs), GetDefaultPaddingPercentage()*ImageWidth(image), (1.0 - GetDefaultPaddingPercentage())*ImageWidth(image))
def MapYCoordinateAutoSettings(y, image, ys):
return MapYCoordinate(y, GetMinimum(ys), GetMaximum(ys), GetDefaultPaddingPercentage()*ImageHeight(image), (1.0 - GetDefaultPaddingPercentage())*ImageHeight(image))
def MapXCoordinateBasedOnSettings(x, settings):
boundaries = 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)
def MapYCoordinateBasedOnSettings(y, settings):
boundaries = 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)
def GetDefaultPaddingPercentage():
return 0.10
def DrawText(canvas, x, y, text, color):
charWidth = 8.0
spacing = 2.0
i = 0.0
while i < len(text):
DrawAsciiCharacter(canvas, x + i*(charWidth + spacing), y, text[int(i)], color)
i = i + 1.0
def DrawTextUpwards(canvas, x, y, text, color):
buffer = CreateImage(GetTextWidth(text), GetTextHeight(text), GetTransparent())
DrawText(buffer, 0.0, 0.0, text, color)
rotated = RotateAntiClockwise90Degrees(buffer)
DrawImageOnImage(canvas, rotated, x, y)
DeleteImage(buffer)
DeleteImage(rotated)
def GetDefaultScatterPlotSettings():
settings = ScatterPlotSettings()
settings.autoBoundaries = True
settings.xMax = 0.0
settings.xMin = 0.0
settings.yMax = 0.0
settings.yMin = 0.0
settings.autoPadding = True
settings.xPadding = 0.0
settings.yPadding = 0.0
settings.title = ""
settings.xLabel = ""
settings.yLabel = ""
settings.scatterPlotSeries = [None]*int(0.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
def GetDefaultScatterPlotSeriesSettings():
series = ScatterPlotSeries()
series.linearInterpolation = True
series.pointType = "pixels"
series.lineType = "solid"
series.lineThickness = 1.0
series.xs = [None]*int(0.0)
series.ys = [None]*int(0.0)
series.color = GetBlack()
return series
def DrawScatterPlot(canvasReference, width, height, xs, ys, errorMessage):
settings = GetDefaultScatterPlotSettings()
settings.width = width
settings.height = height
settings.scatterPlotSeries = [None]*int(1.0)
settings.scatterPlotSeries[int(0.0)] = GetDefaultScatterPlotSeriesSettings()
settings.scatterPlotSeries[int(0.0)].xs = None
settings.scatterPlotSeries[int(0.0)].xs = xs
settings.scatterPlotSeries[int(0.0)].ys = None
settings.scatterPlotSeries[int(0.0)].ys = ys
success = DrawScatterPlotFromSettings(canvasReference, settings, errorMessage)
return success
def DrawScatterPlotFromSettings(canvasReference, settings, errorMessage):
canvas = CreateImage(settings.width, settings.height, GetWhite())
patternOffset = CreateNumberReference(0.0)
success = ScatterPlotFromSettingsValid(settings, errorMessage)
if success:
boundaries = Rectangle()
ComputeBoundariesBasedOnSettings(settings, boundaries)
xMin = boundaries.x1
yMin = boundaries.y1
xMax = boundaries.x2
yMax = boundaries.y2
# If zero, set to defaults.
if xMin - xMax == 0.0:
xMin = 0.0
xMax = 10.0
if yMin - yMax == 0.0:
yMin = 0.0
yMax = 10.0
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/2.0 - GetTextWidth(settings.title)/2.0), floor(yPadding/3.0), 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 = StringArrayReference()
xLabelPriorities = NumberArrayReference()
yLabels = StringArrayReference()
yLabelPriorities = NumberArrayReference()
xGridPositions = ComputeGridLinePositions(xMin, xMax, xLabels, xLabelPriorities)
yGridPositions = ComputeGridLinePositions(yMin, yMax, yLabels, yLabelPriorities)
if settings.showGrid:
# X-grid
i = 0.0
while i < len(xGridPositions):
x = xGridPositions[int(i)]
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax)
DrawLine1px(canvas, px, yPixelMin, px, yPixelMax, settings.gridColor)
i = i + 1.0
# Y-grid
i = 0.0
while i < len(yGridPositions):
y = yGridPositions[int(i)]
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax)
DrawLine1px(canvas, xPixelMin, py, xPixelMax, py, settings.gridColor)
i = i + 1.0
# Compute origin information.
originYInside = yMin < 0.0 and yMax > 0.0
originY = 0.0
if settings.xAxisAuto:
if originYInside:
originY = 0.0
else:
originY = yMin
else:
if settings.xAxisTop:
originY = yMax
if settings.xAxisBottom:
originY = yMin
originYPixels = MapYCoordinate(originY, yMin, yMax, yPixelMin, yPixelMax)
originXInside = xMin < 0.0 and xMax > 0.0
originX = 0.0
if settings.yAxisAuto:
if originXInside:
originX = 0.0
else:
originX = xMin
else:
if settings.yAxisLeft:
originX = xMin
if settings.yAxisRight:
originX = xMax
originXPixels = MapXCoordinate(originX, xMin, xMax, xPixelMin, xPixelMax)
if originYInside:
originTextY = 0.0
else:
originTextY = yMin + yLength/2.0
originTextYPixels = MapYCoordinate(originTextY, yMin, yMax, yPixelMin, yPixelMax)
if originXInside:
originTextX = 0.0
else:
originTextX = xMin + xLength/2.0
originTextXPixels = MapXCoordinate(originTextX, xMin, xMax, xPixelMin, xPixelMax)
# Labels
occupied = [None]*int(len(xLabels.stringArray) + len(yLabels.stringArray))
i = 0.0
while i < len(occupied):
occupied[int(i)] = CreateRectangle(0.0, 0.0, 0.0, 0.0)
i = i + 1.0
nextRectangle = CreateNumberReference(0.0)
# x labels
i = 1.0
while i <= 5.0:
textOnBottom = True
if not settings.xAxisAuto and settings.xAxisTop:
textOnBottom = False
DrawXLabelsForPriority(i, xMin, originYPixels, xMax, xPixelMin, xPixelMax, nextRectangle, gridLabelColor, canvas, xGridPositions, xLabels, xLabelPriorities, occupied, textOnBottom)
i = i + 1.0
# y labels
i = 1.0
while i <= 5.0:
textOnLeft = True
if not settings.yAxisAuto and settings.yAxisRight:
textOnLeft = False
DrawYLabelsForPriority(i, yMin, originXPixels, yMax, yPixelMin, yPixelMax, nextRectangle, gridLabelColor, canvas, yGridPositions, yLabels, yLabelPriorities, occupied, textOnLeft)
i = i + 1.0
# Draw origin line axis titles.
axisLabelPadding = 20.0
# x origin line
if originYInside:
DrawLine1px(canvas, Round(xPixelMin), Round(originYPixels), Round(xPixelMax), Round(originYPixels), GetBlack())
# y origin line
if originXInside:
DrawLine1px(canvas, Round(originXPixels), Round(yPixelMin), Round(originXPixels), Round(yPixelMax), GetBlack())
# Draw origin axis titles.
DrawTextUpwards(canvas, 10.0, floor(originTextYPixels - GetTextWidth(settings.yLabel)/2.0), settings.yLabel, GetBlack())
DrawText(canvas, floor(originTextXPixels - GetTextWidth(settings.xLabel)/2.0), yPixelMax + axisLabelPadding, settings.xLabel, GetBlack())
# X-grid-markers
i = 0.0
while i < len(xGridPositions):
x = xGridPositions[int(i)]
px = MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax)
p = xLabelPriorities.numberArray[int(i)]
l = 1.0
if p == 1.0:
l = 8.0
elif p == 2.0:
l = 3.0
side = -1.0
if not settings.xAxisAuto and settings.xAxisTop:
side = 1.0
DrawLine1px(canvas, px, originYPixels, px, originYPixels + side*l, GetBlack())
i = i + 1.0
# Y-grid-markers
i = 0.0
while i < len(yGridPositions):
y = yGridPositions[int(i)]
py = MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax)
p = yLabelPriorities.numberArray[int(i)]
l = 1.0
if p == 1.0:
l = 8.0
elif p == 2.0:
l = 3.0
side = 1.0
if not settings.yAxisAuto and settings.yAxisRight:
side = -1.0
DrawLine1px(canvas, originXPixels, py, originXPixels + side*l, py, GetBlack())
i = i + 1.0
# Draw points
plot = 0.0
while plot < len(settings.scatterPlotSeries):
sp = settings.scatterPlotSeries[int(plot)]
xs = sp.xs
ys = sp.ys
linearInterpolation = sp.linearInterpolation
x1Ref = NumberReference()
y1Ref = NumberReference()
x2Ref = NumberReference()
y2Ref = NumberReference()
if linearInterpolation:
prevSet = False
xPrev = 0.0
yPrev = 0.0
i = 0.0
while i < len(xs):
x = xs[int(i)]
y = ys[int(i)]
if prevSet:
x1Ref.numberValue = xPrev
y1Ref.numberValue = yPrev
x2Ref.numberValue = x
y2Ref.numberValue = y
success = CropLineWithinBoundary(x1Ref, y1Ref, x2Ref, y2Ref, xMin, xMax, yMin, yMax)
if success:
pxPrev = floor(MapXCoordinate(x1Ref.numberValue, xMin, xMax, xPixelMin, xPixelMax))
pyPrev = floor(MapYCoordinate(y1Ref.numberValue, yMin, yMax, yPixelMin, yPixelMax))
px = floor(MapXCoordinate(x2Ref.numberValue, xMin, xMax, xPixelMin, xPixelMax))
py = floor(MapYCoordinate(y2Ref.numberValue, yMin, yMax, yPixelMin, yPixelMax))
if aStringsEqual(sp.lineType, "solid") and sp.lineThickness == 1.0:
DrawLine1px(canvas, pxPrev, pyPrev, px, py, sp.color)
elif aStringsEqual(sp.lineType, "solid"):
DrawLine(canvas, pxPrev, pyPrev, px, py, sp.lineThickness, sp.color)
elif aStringsEqual(sp.lineType, "dashed"):
linePattern = GetLinePattern1()
DrawLineBresenhamsAlgorithmThickPatterned(canvas, pxPrev, pyPrev, px, py, sp.lineThickness, linePattern, patternOffset, sp.color)
elif aStringsEqual(sp.lineType, "dotted"):
linePattern = GetLinePattern2()
DrawLineBresenhamsAlgorithmThickPatterned(canvas, pxPrev, pyPrev, px, py, sp.lineThickness, linePattern, patternOffset, sp.color)
elif aStringsEqual(sp.lineType, "dotdash"):
linePattern = GetLinePattern3()
DrawLineBresenhamsAlgorithmThickPatterned(canvas, pxPrev, pyPrev, px, py, sp.lineThickness, linePattern, patternOffset, sp.color)
elif aStringsEqual(sp.lineType, "longdash"):
linePattern = GetLinePattern4()
DrawLineBresenhamsAlgorithmThickPatterned(canvas, pxPrev, pyPrev, px, py, sp.lineThickness, linePattern, patternOffset, sp.color)
elif aStringsEqual(sp.lineType, "twodash"):
linePattern = GetLinePattern5()
DrawLineBresenhamsAlgorithmThickPatterned(canvas, pxPrev, pyPrev, px, py, sp.lineThickness, linePattern, patternOffset, sp.color)
prevSet = True
xPrev = x
yPrev = y
i = i + 1.0
else:
i = 0.0
while i < len(xs):
x = xs[int(i)]
y = ys[int(i)]
if x > xMin and x < xMax and y > yMin and y < yMax:
x = floor(MapXCoordinate(x, xMin, xMax, xPixelMin, xPixelMax))
y = floor(MapYCoordinate(y, yMin, yMax, yPixelMin, yPixelMax))
if aStringsEqual(sp.pointType, "crosses"):
DrawPixel(canvas, x, y, sp.color)
DrawPixel(canvas, x + 1.0, y, sp.color)
DrawPixel(canvas, x + 2.0, y, sp.color)
DrawPixel(canvas, x - 1.0, y, sp.color)
DrawPixel(canvas, x - 2.0, y, sp.color)
DrawPixel(canvas, x, y + 1.0, sp.color)
DrawPixel(canvas, x, y + 2.0, sp.color)
DrawPixel(canvas, x, y - 1.0, sp.color)
DrawPixel(canvas, x, y - 2.0, sp.color)
elif aStringsEqual(sp.pointType, "circles"):
DrawCircle(canvas, x, y, 3.0, sp.color)
elif aStringsEqual(sp.pointType, "dots"):
DrawFilledCircle(canvas, x, y, 3.0, sp.color)
elif aStringsEqual(sp.pointType, "triangles"):
DrawTriangle(canvas, x, y, 3.0, sp.color)
elif aStringsEqual(sp.pointType, "filled triangles"):
DrawFilledTriangle(canvas, x, y, 3.0, sp.color)
elif aStringsEqual(sp.pointType, "pixels"):
DrawPixel(canvas, x, y, sp.color)
i = i + 1.0
plot = plot + 1.0
canvasReference.image = canvas
return success
def ComputeBoundariesBasedOnSettings(settings, boundaries):
if len(settings.scatterPlotSeries) >= 1.0:
xMin = GetMinimum(settings.scatterPlotSeries[int(0.0)].xs)
xMax = GetMaximum(settings.scatterPlotSeries[int(0.0)].xs)
yMin = GetMinimum(settings.scatterPlotSeries[int(0.0)].ys)
yMax = GetMaximum(settings.scatterPlotSeries[int(0.0)].ys)
else:
xMin = -10.0
xMax = 10.0
yMin = -10.0
yMax = 10.0
if not settings.autoBoundaries :
xMin = settings.xMin
xMax = settings.xMax
yMin = settings.yMin
yMax = settings.yMax
else:
plot = 1.0
while plot < len(settings.scatterPlotSeries):
sp = settings.scatterPlotSeries[int(plot)]
xMin = min(xMin,GetMinimum(sp.xs))
xMax = max(xMax,GetMaximum(sp.xs))
yMin = min(yMin,GetMinimum(sp.ys))
yMax = max(yMax,GetMaximum(sp.ys))
plot = plot + 1.0
boundaries.x1 = xMin
boundaries.y1 = yMin
boundaries.x2 = xMax
boundaries.y2 = yMax
def ScatterPlotFromSettingsValid(settings, errorMessage):
success = True
# Check axis placement.
if not settings.xAxisAuto :
if settings.xAxisTop and settings.xAxisBottom:
success = False
errorMessage.string = "x-axis not automatic and configured to be both on top and on bottom."
if not settings.xAxisTop and not settings.xAxisBottom :
success = False
errorMessage.string = "x-axis not automatic and configured to be neither on top nor on bottom."
if not settings.yAxisAuto :
if settings.yAxisLeft and settings.yAxisRight:
success = False
errorMessage.string = "y-axis not automatic and configured to be both on top and on bottom."
if not settings.yAxisLeft and not settings.yAxisRight :
success = False
errorMessage.string = "y-axis not automatic and configured to be neither on top nor on bottom."
# Check series lengths.
i = 0.0
while i < len(settings.scatterPlotSeries):
series = settings.scatterPlotSeries[int(i)]
if len(series.xs) != len(series.ys):
success = False
errorMessage.string = "x and y series must be of the same length."
if len(series.xs) == 0.0:
success = False
errorMessage.string = "There must be data in the series to be plotted."
if series.linearInterpolation and len(series.xs) == 1.0:
success = False
errorMessage.string = "Linear interpolation requires at least two data points to be plotted."
i = i + 1.0
# Check bounds.
if not settings.autoBoundaries :
if settings.xMin >= settings.xMax:
success = False
errorMessage.string = "x min is higher than or equal to x max."
if settings.yMin >= settings.yMax:
success = False
errorMessage.string = "y min is higher than or equal to y max."
# Check padding.
if not settings.autoPadding :
if 2.0*settings.xPadding >= settings.width:
success = False
errorMessage.string = "The x padding is more then the width."
if 2.0*settings.yPadding >= settings.height:
success = False
errorMessage.string = "The y padding is more then the height."
# Check width and height.
if settings.width < 0.0:
success = False
errorMessage.string = "The width is less than 0."
if settings.height < 0.0:
success = False
errorMessage.string = "The height is less than 0."
# Check point types.
i = 0.0
while i < len(settings.scatterPlotSeries):
series = settings.scatterPlotSeries[int(i)]
if series.lineThickness < 0.0:
success = False
errorMessage.string = "The line thickness is less than 0."
if not series.linearInterpolation :
# Point type.
found = False
if aStringsEqual(series.pointType, "crosses"):
found = True
elif aStringsEqual(series.pointType, "circles"):
found = True