-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixiePlot.lua
1190 lines (1097 loc) · 35 KB
/
PixiePlot.lua
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
-----------------------------------------------------------------------------------------------------------------------
-- Plotter for WildStar. Supports line, stem, bar, scatter, polar, and parametric plots.
-- Copyright (c) NCsoft. All rights reserved
-- @author draftomatic
-----------------------------------------------------------------------------------------------------------------------
-- PixiePlot is converted to a constructor object, so call it directly to get a new plotter.
local PixiePlot = {}
-- A couple colors
local clrWhite = {
a = 1,
r = 1,
g = 1,
b = 1
}
local clrGrey = {
a = 1,
r = 0.6,
g = 0.6,
b = 0.6
}
local clrClear = {
a = 0,
r = 1,
g = 1,
b = 1
}
--- Constants
-- Plotting Styles
PixiePlot.LINE = 1
PixiePlot.STEM = 2
PixiePlot.SCATTER = 3
PixiePlot.PIE = 4
-- Fill Styles
PixiePlot.LINEONLY = 1
PixiePlot.FILL = 2
-- Coordinate Systems
PixiePlot.CARTESIAN = 1
PixiePlot.POLAR = 2
-- Bar graph orientations
PixiePlot.HORIZONTAL = 1
PixiePlot.VERTICAL = 2
-- Window Overlay event types
PixiePlot.MOUSE_DOWN = 1
PixiePlot.MOUSE_UP = 2
PixiePlot.MOUSE_ENTER = 3
PixiePlot.MOUSE_EXIT = 4
-- Coordinate System Conversion
local function cartToPol(x, y)
return math.sqrt(math.pow(x,2) + math.pow(y,2)), math.atan2(y,x)
end
local function polToCart(th, r)
return r*math.cos(th), r*math.sin(th)
end
--- Sets all plotting options (replaces current options).
-- @param tOpt Plotting options.
function PixiePlot:SetOptions(tOpt)
self.tOpt = tOpt
end
--- Sets a plotting option by name. For a full list of option names and values, see the main PixiePlot section.
-- @param strName The option name.
-- @param opt The option value.
-- @see PixiePlot
function PixiePlot:SetOption(strName, opt)
self.tOpt[strName] = opt
end
--- Sets the x-distance between dataSet values. Does not apply to scatter plots.
-- @param fInterval The x-distance between dataSet values.
function PixiePlot:SetXInterval(fInterval)
self.fXInterval = fInterval
end
--- Sets the minimum (bottom) X bound for the viewbox.
-- @param fXMin The minimum x-value to display in the viewbox.
function PixiePlot:SetXMin(fXMin)
self.fXMin = fXMin
end
--- Sets the minimum (bottom) X bound for the viewbox.
-- @param fXMax The maximum x-value to display in the viewbox.
function PixiePlot:SetXMax(fXMax)
self.fXMax = fXMax
end
--- Sets the minimum (bottom) Y bound for the viewbox.
-- @param fYMin The minimum y-value to display in the viewbox.
function PixiePlot:SetYMin(fYMin)
self.fYMin = fYMin
end
--- Sets the minimum (bottom) Y bound for the viewbox.
-- @param fYMax The maximum y-value to display in the viewbox.
function PixiePlot:SetYMax(fYMax)
self.fYMax = fYMax
end
--- Redraws graph using current dataSets and settings.
function PixiePlot:Redraw()
-- Reset Pixies
self.wnd:DestroyAllPixies()
if self.nDataSets > 0 then
-- Draw axes
if self.tOpt.bDrawXAxis then
self:DrawXAxis()
end
if self.tOpt.bDrawYAxis then
self:DrawYAxis()
end
-- Draw DataSets
local i = 1
for _,dataSet in pairs(self.dataSets) do
local color = self.tOpt.aPlotColors[i]
self:PlotDataSet(dataSet, color, i-1)
i = i + 1
end
-- Draw labels
if self.tOpt.bDrawXAxisLabel then
self:DrawXAxisLabel()
end
if self.tOpt.bDrawYAxisLabel then
self:DrawYAxisLabel()
end
if self.tOpt.bDrawXValueLabels then
self:DrawXValueLabels()
end
if self.tOpt.bDrawYValueLabels then
if self.tOpt.ePlotStyle == self.BAR and self.tOpt.eBarOrientation == self.HORIZONTAL then
self:DrawXValueLabels(self.fYMax - self.fYMin)
else
self:DrawYValueLabels()
end
end
-- Draw grid lines
if self.tOpt.bDrawXGridLines then
self:DrawXGridLines()
end
if self.tOpt.bDrawYGridLines then
self:DrawYGridLines()
end
end
end
--- Adds a dataset to plot on the next Redraw.
-- @param dataSet An array of data to plot. The general format is as follows:
-- dataSet = {xStart = xStart, values = {}}
-- xStart is the x-value of the first values element. This parameter allows you to shift the x position of the
-- graph (for pieceswise plotting, or for lining up dataSets of different length and start position). Generally
-- xStart will be the same for all dataSets.
--
-- If ePlotStyle equals BAR, LINE, or STEM, values is an array of numbers representing y-values, e.g.:
-- {-4, 5, 0, 3, 8}
-- If ePlotStyle == SCATTER, values is an array of tables with x- and y-values, e.g.:
-- {{x=0,y=0},{x=1,y=2}, ...}
function PixiePlot:AddDataSet(dataSet)
--Print("Adding " .. #dataSet.values .. " values")
self:UpdateMinMaxValues(dataSet)
--Print(self.fXMin .. " " ..self.fXMax .. " " ..self.fYMin .. " " .. self.fYMax)
self.dataSets[self.nDataSets + 1] = dataSet
self.nDataSets = self.nDataSets + 1
return self.nDataSets
end
--- Removes all dataSets
function PixiePlot:RemoveAllDataSets()
self.nDataSets = 0
self.nNumDataSets = 0
self.dataSets = {}
self.fXMin = nil
self.fXMax = nil
self.fYMin = nil
self.fYMax = nil
end
--- Removes a dataSet by id
-- @param nDataSetId The id of the dataSet that was returned from @see AddDataSet().
function PixiePlot:RemoveDataSet(nDataSetId)
self.dataSets[nDataSetId] = nil
end
--- Plots the given dataSet with current options.
-- @param dataSet An array of values. Uses self.xInterval for timestep.
-- @param color The color of the current dataSet.
-- @param nBarIndex The index of the current bar graph (used only for multiple bar plot dataSets)
function PixiePlot:PlotDataSet(dataSet, color, nBarIndex)
local maxWidth = self.wnd:GetWidth() - self.tOpt.fYLabelMargin - self.tOpt.fPlotMargin
local maxHeight = self.wnd:GetHeight() - self.tOpt.fXLabelMargin - self.tOpt.fPlotMargin
local clrSymbol = self.tOpt.clrSymbol
if clrSymbol == nil then
clrSymbol = color
end
local ePlotStyle = self.tOpt.ePlotStyle
-- Line Plot
if ePlotStyle == self.LINE or ePlotStyle == self.STEM then
local fXRange = self.fXMax - self.fXMin
local fYRange = self.fYMax - self.fYMin
if self.tOpt.eCoordinateSystem == self.CARTESIAN then
local xOffset = (dataSet.xStart - self.fXMin) / fXRange * maxWidth
--Print(xOffset)
local xIntervalWidth = maxWidth / (#dataSet.values - 1)
local vPrev
-- Line
for i, v in ipairs(dataSet.values) do
if v == math.huge then v = 0 end
if ePlotStyle == self.LINE then
if i > 1 then
if self.tOpt.eFillStyle == PixiePlot.FILL then
self:DrawFilledLine(
{
x1 = self.tOpt.fYLabelMargin + xOffset + (i - 2) * xIntervalWidth,
y1 = self.tOpt.fXLabelMargin + (vPrev - self.fYMin) / fYRange * maxHeight,
x2 = self.tOpt.fYLabelMargin + xOffset + (i - 1) * xIntervalWidth,
y2 = self.tOpt.fXLabelMargin + (v - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
else
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + xOffset + (i - 2) * xIntervalWidth,
y1 = self.tOpt.fXLabelMargin + (vPrev - self.fYMin) / fYRange * maxHeight,
x2 = self.tOpt.fYLabelMargin + xOffset + (i - 1) * xIntervalWidth,
y2 = self.tOpt.fXLabelMargin + (v - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
end
end
elseif ePlotStyle == self.STEM then
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + xOffset + (i - 1) * xIntervalWidth,
y1 = self.tOpt.fXLabelMargin + (0 - self.fYMin) / (self.fYMax - self.fYMin) * maxHeight,
x2 = self.tOpt.fYLabelMargin + xOffset + (i - 1) * xIntervalWidth,
y2 = self.tOpt.fXLabelMargin + (v - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
end
vPrev = v
end
-- Symbol
if self.tOpt.bDrawSymbol then
for i, v in ipairs(dataSet.values) do
if v == math.huge then v = 0 end
local fSymbolSize = self.tOpt.fSymbolSize
if fSymbolSize == nil then
fSymbolSize = self.tOpt.fLineWidth * 2
end
--Print(v .. "/" .. fYRange .. " out of " .. maxHeight)
local tPos = {
x = self.tOpt.fYLabelMargin + xOffset + (i - 1) * xIntervalWidth,
y = self.tOpt.fXLabelMargin + (v - self.fYMin) / fYRange * maxHeight
}
self:DrawSymbol(
tPos,
fSymbolSize,
self.tOpt.strSymbolSprite,
clrSymbol
)
-- Window Overlay
if self.tOpt.bWndOverlays then
local wnd = self:AddWindowOverlay(tPos, self.tOpt.fWndOverlaySize)
local tData = {
i = i,
x = (i - 1) * self.fXInterval + dataSet.xStart,
y = v
}
wnd:SetData(tData)
if self.tOpt.wndOverlayLoadCallback then
self.tOpt.wndOverlayLoadCallback(tData, wnd)
end
end
end
end
elseif self.tOpt.eCoordinateSystem == self.POLAR then
local vPrev
for i, v in ipairs(dataSet.values) do
if v == math.huge then v = 0 end
local vPol = v
local xActual = dataSet.xStart + (i - 2) * self.fXInterval
xActual, v = polToCart(xActual, v)
if ePlotStyle == self.LINE then
if i > 1 then
local xActualPrev = dataSet.xStart + (i - 3) * self.fXInterval
xActualPrev, vPrev = polToCart(xActualPrev, vPrev)
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + (xActualPrev - self.fXMin) / fXRange * maxWidth,
y1 = self.tOpt.fXLabelMargin + (vPrev - self.fYMin) / fYRange * maxHeight,
x2 = self.tOpt.fYLabelMargin + (xActual - self.fXMin) / fXRange * maxWidth,
y2 = self.tOpt.fXLabelMargin + (v - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
end
elseif ePlotStyle == self.STEM then
--local xActual = dataSet.xStart + (i - 2) * self.fXInterval
--xActual, v = polToCart(xActual, v)
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + (0 - self.fXMin) / (self.fXMax - self.fXMin) * maxWidth,
y1 = self.tOpt.fXLabelMargin + (0 - self.fYMin) / (self.fYMax - self.fYMin) * maxHeight,
x2 = self.tOpt.fYLabelMargin + (xActual - self.fXMin) / fXRange * maxWidth,
y2 = self.tOpt.fXLabelMargin + (v - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
end
vPrev = vPol
end
-- Symbol
if self.tOpt.bDrawSymbol then
for i, v in ipairs(dataSet.values) do
if v == math.huge then v = 0 end
local xActual = dataSet.xStart + (i - 2) * self.fXInterval
local xCart, vCart = polToCart(xActual, v)
local fSymbolSize = self.tOpt.fSymbolSize
if fSymbolSize == nil then
fSymbolSize = self.tOpt.fLineWidth * 2
end
--Print(vCart .. "/" .. fYRange .. " out of " .. maxHeight)
local tPos = {
x = self.tOpt.fYLabelMargin + (xCart - self.fXMin) / fXRange * maxWidth,
y = self.tOpt.fXLabelMargin + (vCart - self.fYMin) / fYRange * maxHeight
}
self:DrawSymbol(
tPos,
fSymbolSize,
self.tOpt.strSymbolSprite,
clrSymbol
)
-- Window Overlay
if self.tOpt.bWndOverlays then
local wnd = self:AddWindowOverlay(tPos, self.tOpt.fWndOverlaySize)
local tData = {
i = i,
x = (i - 1) * self.fXInterval + dataSet.xStart,
y = v
}
wnd:SetData(tData)
if self.tOpt.wndOverlayLoadCallback then
self.tOpt.wndOverlayLoadCallback(tData, wnd)
end
end
end
end
end
elseif ePlotStyle == self.SCATTER then
local fXRange = self.fXMax - self.fXMin
local fYRange = self.fYMax - self.fYMin
if self.tOpt.bScatterLine == true then
local vPrev
for i, v in ipairs(dataSet.values) do
if self.tOpt.eCoordinateSystem == self.POLAR then
v.x, v.y = polToCart(v.x, v.y)
end
if i > 1 then
if self.tOpt.eFillStyle == PixiePlot.FILL then
self:DrawFilledLine(
{
x1 = self.tOpt.fYLabelMargin + (vPrev.x - self.fXMin) / fXRange * maxWidth,
y1 = self.tOpt.fXLabelMargin + (vPrev.y - self.fYMin) / fYRange * maxHeight,
x2 = self.tOpt.fYLabelMargin + (v.x - self.fXMin) / fXRange * maxWidth,
y2 = self.tOpt.fXLabelMargin + (v.y - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
else
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + (vPrev.x - self.fXMin) / fXRange * maxWidth,
y1 = self.tOpt.fXLabelMargin + (vPrev.y - self.fYMin) / fYRange * maxHeight,
x2 = self.tOpt.fYLabelMargin + (v.x - self.fXMin) / fXRange * maxWidth,
y2 = self.tOpt.fXLabelMargin + (v.y - self.fYMin) / fYRange * maxHeight
},
"",
self.tOpt.fLineWidth,
self.tOpt.strLineSprite,
color
)
end
end
vPrev = v
end
end
local fSymbolSize = self.tOpt.fSymbolSize
if fSymbolSize == nil then
fSymbolSize = self.tOpt.fLineWidth * 2
end
for i, v in pairs(dataSet.values) do
--local x = self.tOpt.fYLabelMargin + (v.x - self.fXMin) / fXRange * maxWidth
--local y = self.tOpt.fXLabelMargin + (v.y - self.fYMin) / fYRange * maxHeight
--Print("x: " .. v.x - self.fXMin .. "/" .. fXRange .. " out of " .. maxWidth)
--Print("y: " .. v.y - self.fYMin .. "/" .. fYRange .. " out of " .. maxHeight)
--Print("(" .. x .. ", " .. y .. ") in " .. "(" .. maxWidth .. ", " .. maxHeight .. ")")
local tPos = {
x = self.tOpt.fYLabelMargin + (v.x - self.fXMin) / fXRange * maxWidth,
y = self.tOpt.fXLabelMargin + (v.y - self.fYMin) / fYRange * maxHeight
}
self:DrawSymbol(
tPos,
fSymbolSize,
self.tOpt.strSymbolSprite,
clrSymbol
)
-- Window Overlay
if self.tOpt.bWndOverlays then
local wnd = self:AddWindowOverlay(tPos, self.tOpt.fWndOverlaySize)
local tData = {
i = i,
x = v.x,
y = v.y
}
wnd:SetData(tData)
if self.tOpt.wndOverlayLoadCallback then
self.tOpt.wndOverlayLoadCallback(tData, wnd)
end
end
end
elseif self.tOpt.ePlotStyle == self.BAR then
--Print("drawing " .. #dataSet.values .. " bars")
if self.tOpt.eBarOrientation == self.VERTICAL then
local fXIntervalWidth = (maxWidth - self.tOpt.fBarMargin - ((#dataSet.values-1) * self.tOpt.fBarSpacing)) / #dataSet.values
local fTotalBarWidth = fXIntervalWidth / self:GetTableSize(self.dataSets)
local fActualBarWidth = fTotalBarWidth - 2*self.tOpt.fBarMargin
for i,v in ipairs(dataSet.values) do
--Print(v .. "/" .. self.fYMax .. " out of " .. maxHeight)
local label
if dataSet.labels ~= nil then label = dataSet.labels[i] else label = "" end
self:DrawRectangle({
x1 = self.tOpt.fYLabelMargin + self.tOpt.fBarMargin + (i-1) * self.tOpt.fBarSpacing + (i-1) * fXIntervalWidth + fTotalBarWidth * nBarIndex,
y1 = 1 + self.tOpt.fXLabelMargin,
x2 = self.tOpt.fYLabelMargin + self.tOpt.fBarMargin + (i-1) * self.tOpt.fBarSpacing + (i-1) * fXIntervalWidth + fTotalBarWidth * nBarIndex + self.tOpt.fBarMargin + fActualBarWidth,
y2 = 1 + self.tOpt.fXLabelMargin + (v / self.fYMax) * maxHeight
},
self.tOpt.strBarSprite,
color,
self.tOpt.clrBarLabel,
label)
end
else
local fYIntervalHeight = (maxHeight - self.tOpt.fBarMargin - ((#dataSet.values-1) * self.tOpt.fBarSpacing)) / #dataSet.values
local fTotalBarHeight = fYIntervalHeight / self:GetTableSize(self.dataSets)
local fActualBarHeight = fTotalBarHeight - 2*self.tOpt.fBarMargin
for i,v in ipairs(dataSet.values) do
--Print(v .. "/" .. self.fYMax .. " out of " .. maxWidth)
local label
if dataSet.labels ~= nil then label = dataSet.labels[i] else label = "" end
self:DrawRectangle({
y1 = self.tOpt.fXLabelMargin + self.tOpt.fBarMargin + (i-1) * self.tOpt.fBarSpacing + (i-1) * fYIntervalHeight + fTotalBarHeight * nBarIndex,
x1 = 1 + self.tOpt.fYLabelMargin,
y2 = self.tOpt.fXLabelMargin + self.tOpt.fBarMargin + (i-1) * self.tOpt.fBarSpacing + (i-1) * fYIntervalHeight + fTotalBarHeight * nBarIndex + self.tOpt.fBarMargin + fActualBarHeight,
x2 = 1 + self.tOpt.fYLabelMargin + (v / self.fYMax) * maxWidth
},
self.tOpt.strBarSprite,
color,
self.tOpt.clrBarLabel,
label)
end
end
end
end
--- Draws X axis with current options.
function PixiePlot:DrawXAxis()
if self.tOpt.ePlotStyle == self.BAR then
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin,
y1 = self.tOpt.fXLabelMargin,
x2 = self.wnd:GetWidth() - self.tOpt.fPlotMargin,
y2 = self.tOpt.fXLabelMargin
},
"",
self.tOpt.fYAxisWidth,
nil,
self.tOpt.clrYAxis
)
else
if self.fYMin <= 0 and self.fYMax >= 0 then
local maxHeight = self.wnd:GetHeight() - self.tOpt.fXLabelMargin - self.tOpt.fPlotMargin
local yPos = (0 - self.fYMin) / (self.fYMax - self.fYMin) * maxHeight
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin,
y1 = self.tOpt.fXLabelMargin + yPos,
x2 = self.wnd:GetWidth() - self.tOpt.fPlotMargin,
y2 = self.tOpt.fXLabelMargin + yPos
},
"",
self.tOpt.fXAxisWidth,
nil,
self.tOpt.clrXAxis
)
end
end
end
--- Draws Y axis with current options.
function PixiePlot:DrawYAxis()
if self.tOpt.ePlotStyle == self.BAR then
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin,
y1 = self.tOpt.fXLabelMargin,
x2 = self.tOpt.fYLabelMargin,
y2 = self.wnd:GetHeight() - self.tOpt.fPlotMargin
},
"",
self.tOpt.fYAxisWidth,
nil,
self.tOpt.clrYAxis
)
else
if self.fXMin <= 0 and self.fXMax >= 0 then
local maxWidth = self.wnd:GetWidth() - self.tOpt.fYLabelMargin - self.tOpt.fPlotMargin
local xPos = (0 - self.fXMin) / (self.fXMax - self.fXMin) * maxWidth
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + xPos,
y1 = self.tOpt.fXLabelMargin,
x2 = self.tOpt.fYLabelMargin + xPos,
y2 = self.wnd:GetHeight() - self.tOpt.fPlotMargin
},
"",
self.tOpt.fYAxisWidth,
nil,
self.tOpt.clrYAxis
)
end
end
end
--- Draws X labels with current options.
function PixiePlot:DrawXAxisLabel()
self:DrawLine(
{
x1 = self.wnd:GetWidth() - self.tOpt.fPlotMargin - self.tOpt.fXAxisLabelOffset,
y1 = self.tOpt.fXLabelMargin / 2,
x2 = self.wnd:GetWidth() - self.tOpt.fPlotMargin,
y2 = self.tOpt.fXLabelMargin / 2,
},
self.tOpt.strXLabel,
10,
nil,
self.tOpt.clrXAxisLabel
)
end
--- Draws Y labels with current options.
function PixiePlot:DrawYAxisLabel()
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin / 3,
y1 = self.wnd:GetHeight() - self.tOpt.fPlotMargin - self.tOpt.fYAxisLabelOffset,
x2 = self.tOpt.fYLabelMargin / 3,
y2 = self.wnd:GetHeight() - self.tOpt.fPlotMargin,
},
self.tOpt.strYLabel,
10,
nil,
self.tOpt.clrYAxisLabel
)
end
--- Draws X value labels with current options.
function PixiePlot:DrawXValueLabels(fXRange)
local maxWidth = self.wnd:GetWidth() - self.tOpt.fYLabelMargin - self.tOpt.fPlotMargin
if fXRange == nil then
fXRange = self.fXMax - self.fXMin
end
local nLabels = self.tOpt.nXValueLabels
local fInterval = fXRange / nLabels
for i=0,nLabels do
local fPos = i * fInterval / fXRange * maxWidth
local fValue = self.fXMin + i * fInterval --* self.fXInterval
local strValue
if self.tOpt.xValueFormatter ~= nil then
strValue = self.tOpt.xValueFormatter(fValue)
else
strValue = string.format("%." .. self.tOpt.nXLabelDecimals .. "f", fValue)
if strValue == "-0.0" then strValue = "0.0" end
if strValue == "-0" then strValue = "0" end
end
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + fPos - self.tOpt.fXValueLabelTilt,
y1 = 0,
x2 = self.tOpt.fYLabelMargin + fPos,
y2 = self.tOpt.fXLabelMargin - 10
},
strValue,
20,
nil,
self.tOpt.clrXValueBackground,
self.tOpt.clrXValueLabel
)
end
end
--- Draws Y value labels with current options.
function PixiePlot:DrawYValueLabels(fYRange)
local maxHeight = self.wnd:GetHeight() - self.tOpt.fXLabelMargin - self.tOpt.fPlotMargin
if fYRange == nil then
fYRange = self.fYMax - self.fYMin
end
local nLabels = self.tOpt.nYValueLabels
local fInterval = fYRange / nLabels
for i=0,nLabels do
local fPos = i * fInterval / fYRange * maxHeight
local fValue = self.fYMin + i * fInterval
local strValue
if self.tOpt.yValueFormatter ~= nil then
strValue = self.tOpt.yValueFormatter(fValue)
else
strValue = string.format("%." .. self.tOpt.nYLabelDecimals .. "f", fValue)
if strValue == "-0.0" then strValue = "0.0" end
if strValue == "-0" then strValue = "0" end
end
self:DrawLine(
{
x1 = 0,
y1 = self.tOpt.fXLabelMargin + fPos - self.tOpt.fYValueLabelTilt,
x2 = self.tOpt.fYLabelMargin - 10,
y2 = self.tOpt.fXLabelMargin + fPos
},
strValue,
20,
nil,
self.tOpt.clrYValueBackground,
self.tOpt.clrYValueLabel
)
end
end
--- Draws X (vertical) grid lines with current options.
function PixiePlot:DrawXGridLines()
local nLabels = self.tOpt.nXValueLabels
local maxWidth = self.wnd:GetWidth() - self.tOpt.fYLabelMargin - self.tOpt.fPlotMargin
local maxHeight = self.wnd:GetHeight() - self.tOpt.fXLabelMargin - self.tOpt.fPlotMargin
if self.tOpt.eCoordinateSystem == self.CARTESIAN or self.tOpt.bPolarGridLines == false then
local fXRange = self.fXMax - self.fXMin
local fInterval = fXRange / nLabels
for i=0,nLabels do
local fPos = i * fInterval / fXRange * maxWidth
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin + fPos,
y1 = self.tOpt.fXLabelMargin,
x2 = self.tOpt.fYLabelMargin + fPos,
y2 = self.wnd:GetHeight() - self.tOpt.fPlotMargin
},
"",
self.tOpt.fXGridLineWidth,
nil,
self.tOpt.clrXGridLine
)
end
elseif self.tOpt.eCoordinateSystem == self.POLAR and self.tOpt.bPolarGridLines == true then
local x1 = (0 - self.fXMin) / (self.fXMax - self.fXMin) * maxWidth + self.tOpt.fYLabelMargin
local y1 = (0 - self.fYMin) / (self.fYMax - self.fYMin) * maxHeight + self.tOpt.fXLabelMargin
local fInterval = 2*math.pi / nLabels
for i=0,nLabels-1 do
local angle = fInterval * i
local x2, y2 = polToCart(angle, 99999) -- Hack
self:DrawLine(
{
x1 = x1,
y1 = y1,
x2 = x2,
y2 = y2
},
"",
self.tOpt.fXGridLineWidth,
nil,
self.tOpt.clrXGridLine
)
end
end
end
--- Draws Y (horizontal) grid lines with current options.
function PixiePlot:DrawYGridLines()
local maxHeight = self.wnd:GetHeight() - self.tOpt.fXLabelMargin - self.tOpt.fPlotMargin
local fYRange = self.fYMax - self.fYMin
local nLabels = self.tOpt.nYValueLabels
local fInterval = fYRange / nLabels
for i=0,nLabels do
local fPos = i * fInterval / fYRange * maxHeight
self:DrawLine(
{
x1 = self.tOpt.fYLabelMargin,
y1 = self.tOpt.fXLabelMargin + fPos,
x2 = self.wnd:GetWidth() - self.tOpt.fPlotMargin,
y2 = self.tOpt.fXLabelMargin + fPos
},
"",
self.tOpt.fYGridLineWidth,
nil,
self.tOpt.clrYGridLine
)
end
end
--- Adds a line to the canvas.
-- Flips y-axis.
-- @param line A table containing x1,y1,x2,y2 coordinates for the line.
-- @param width Width of the line.
-- @param sprite Sprite to use.
-- @param color A table with keys a, r, g, b. Values should be numbers 0.0-1.0.
function PixiePlot:DrawLine(line, text, width, sprite, color, clrText)
--Print("Drawing line starting at: " .. line.x1 .. ", " .. line.y1)
local nPixieId = self.wnd:AddPixie({
strText = text,
strFont = self.tOpt.strLabelFont,
bLine = true,
fWidth = width,
strSprite = sprite,
cr = color,
crText = clrText,
loc = {
fPoints = {0,0,0,0},
nOffsets = {
line.x1,
self.wnd:GetHeight() - line.y1,
line.x2,
self.wnd:GetHeight() - line.y2
}
},
flagsText = {
DT_RIGHT = true--,
--DT_VCENTER = true
}
})
end
--- Draws a filled polygon on the canvas, from the line coords to the canvas base.
-- @param line A table containing x1,y1,x2,y2 coordinates for the line.
-- @param width Width of the line.
-- @param sprite Sprite to use.
-- @param color A table with keys a, r, g, b. Values should be numbers 0.0-1.0.
function PixiePlot:DrawFilledLine(line, text, width, sprite, color, clrText)
local curpos = line.x1
local slope = (line.y2 - line.y1) / (line.x2 - line.x1)
while curpos <= line.x2 do
local ypos = slope * (curpos - line.x1) + line.y1
local line = {x1 = curpos, x2 = curpos, y1 = self.tOpt.fXLabelMargin, y2 = ypos}
self:DrawLine(line, '', 3, sprite, color, clrText)
curpos = curpos + 1
end
end
--- Adds a symbol to the canvas.
-- Flips y-axis.
-- @param pos A table containing x1,y1,x2,y2 coordinates for the symbol.
-- @param size Width and height of the symbol.
-- @param sprite Sprite to use.
-- @param color A table with keys a, r, g, b. Values should be numbers 0.0-1.0
function PixiePlot:DrawSymbol(pos, size, sprite, color)
--Print("Drawing symbol at: " .. pos.x .. ", " .. pos.y)
--Print("Pixie color: " ..
local nPixieId = self.wnd:AddPixie({
strText = "",
bLine = false,
--fWidth = width,
strSprite = sprite,
cr = color,
loc = {
fPoints = {0,0,0,0},
nOffsets = {
pos.x - size/2,
self.wnd:GetHeight() - (pos.y + size/2),
pos.x + size/2,
self.wnd:GetHeight() - (pos.y - size/2)
}
}
})
--Print("Symbol pixieid: " .. tostring(nPixieId))
end
--- Adds a data point tooltip.
-- Flips y-axis.
-- @param pos A table containing x1,y1,x2,y2 coordinates for the tooltip.
-- @param size Width and height of the tooltip.
function PixiePlot:AddWindowOverlay(pos, size)
local XmlDocument = Apollo.GetPackage("Drafto:Lib:XmlDocument-1.0").tPackage
if not XmlDocument then return end
local tDoc = XmlDocument.NewForm()
local tForm = tDoc:NewFormNode("PixiePlotOverlay", {
TooltipType = "OnCursor",
AnchorPoints = {0,0,0,0},
AnchorOffsets = {
pos.x - size/2,
self.wnd:GetHeight() - (pos.y + size/2),
pos.x + size/2,
self.wnd:GetHeight() - (pos.y - size/2)
}
})
tDoc:GetRoot():AddChild(tForm)
local wnd = tDoc:LoadForm("PixiePlotOverlay", self.wnd, self)
if not wnd then
return
end
wnd:AddEventHandler("MouseButtonUp", "OnWndMouseUp", self)
wnd:AddEventHandler("MouseButtonDown", "OnWndMouseDown", self)
wnd:AddEventHandler("MouseEnter", "OnWndMouseEnter", self)
wnd:AddEventHandler("MouseExit", "OnWndMouseExit", self)
wnd:Show(true)
return wnd
end
--- Adds a rectangle to the canvas.
-- Flips y-axis.
-- @param pos A table containing x1,y1,x2,y2 coordinates for the rectangle.
-- @param sprite Sprite to use.
-- @param color A table with keys a, r, g, b. Values should be numbers 0.0-1.0
function PixiePlot:DrawRectangle(rect, sprite, color, clrText, text)
--Print("Drawing rect: " .. rect.x1 .. ", " .. rect.y1 .." -> " .. rect.x2 .. ", " .. rect.y2)
-- Draw rectangle
local nPixieId = self.wnd:AddPixie({
bLine = false,
strSprite = sprite,
cr = color,
loc = {
fPoints = {0,0,0,0},
nOffsets = {
rect.x1,
self.wnd:GetHeight() - rect.y2,
rect.x2,
self.wnd:GetHeight() - rect.y1
}
},
flagsText = {
DT_VCENTER = true
},
fRotation = 0
})
-- Draw text as a separate pixie so it doesn't get cut off by short bars
if text ~= nil then
local x1, x2, y1, y2, lineWidth, DT_VCENTER
if self.tOpt.eBarOrientation == self.VERTICAL then
local width = rect.x2 - rect.x1
lineWidth = 1
x1 = rect.x1 + width / 2 - 10
x2 = x1
y2 = rect.y1 + 3
y1 = self.wnd:GetHeight() - self.tOpt.fPlotMargin
DT_VCENTER = true
else
local height = rect.y2 - rect.y1
lineWidth = 1
y1 = rect.y1 + height / 2 + 10
y2 = y1
x1 = rect.x1 + 3
x2 = self.wnd:GetWidth() - self.tOpt.fPlotMargin
DT_VCENTER = false
end
local nPixieId = self.wnd:AddPixie({
strText = text,
strFont = self.tOpt.strBarFont,
bLine = true,
fWidth = lineWidth,
strSprite = "WhiteFill",
cr = clrClear,
crText = clrText,
loc = {
fPoints = {0,0,0,0},
nOffsets = {
x1,
self.wnd:GetHeight() - y2,
x2,
self.wnd:GetHeight() - y1
}
},
flagsText = {
DT_VCENTER = false
},
fRotation = 0
})
end
end
--- Updates max/min viewbox values using the given dataSet.
-- @param dataSet The dataSet to update max/min values with.
function PixiePlot:UpdateMinMaxValues(dataSet)
if self.tOpt.ePlotStyle == self.LINE or self.tOpt.ePlotStyle == self.STEM then
for i,v in ipairs(dataSet.values) do
if v == math.huge then v = 0 end
local x = dataSet.xStart + (i - 1) * self.fXInterval
if self.tOpt.eCoordinateSystem == self.POLAR then
x = dataSet.xStart + (i - 2) * self.fXInterval
x, v = polToCart(x, v)
end
if self.fXMin == nil or x < self.fXMin then self.fXMin = x end
if self.fXMax == nil or x > self.fXMax then self.fXMax = x end
if self.fYMin == nil or v < self.fYMin then self.fYMin = v end
if self.fYMax == nil or v > self.fYMax then self.fYMax = v end
end
elseif self.tOpt.ePlotStyle == self.SCATTER then
for i,v in pairs(dataSet.values) do
if v == math.huge then v = 0 end
if self.fXMin == nil or v.x < self.fXMin then self.fXMin = v.x end
if self.fXMax == nil or v.x > self.fXMax then self.fXMax = v.x end
if self.fYMin == nil or v.y < self.fYMin then self.fYMin = v.y end
if self.fYMax == nil or v.y > self.fYMax then self.fYMax = v.y end
end
elseif self.tOpt.ePlotStyle == self.BAR then
self.fXMin = 0
self.fXMax = 1
self.fYMin = 0
self.nNumDataSets = self.nNumDataSets + 1
for i,v in ipairs(dataSet.values) do
if self.fYMax == nil or v > self.fYMax then self.fYMax = v end
end
end
end
--- Internal event handler for data point events
function PixiePlot:OnWndMouseDown(wndHandler, wndControl)
if wndHandler ~= wndControl then return end
local tData = wndHandler:GetData()
if tData and self.tOpt.wndOverlayMouseEventCallback then
self.tOpt.wndOverlayMouseEventCallback(tData, PixiePlot.MOUSE_DOWN)
end
end
--- Internal event handler for data point events
function PixiePlot:OnWndMouseUp(wndHandler, wndControl)
if wndHandler ~= wndControl then return end
local tData = wndHandler:GetData()
if tData and self.tOpt.wndOverlayMouseEventCallback then
self.tOpt.wndOverlayMouseEventCallback(tData, PixiePlot.MOUSE_UP)
end
end