-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawSimpleGraph.pb
216 lines (199 loc) · 8.68 KB
/
DrawSimpleGraph.pb
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
; https://www.purebasic.fr/english/viewtopic.php?f=12&t=67464
EnableExplicit
;{ Simple Graph }
; Yet another stuff for simple 'visuals'
; 2017 (c) Luna Sole
; v1.0.0.4 (+ mouse interaction)
; Draws graph with 2 lines on given image using PB vector library
; hImgOut PB image to draw on it, can be any size [except maybe "extremely low" resolutions ^^]
; GraphData() array with items to visualise, should have at least 1 item [starting from 0]
; OutGeoData() map to receive data required to handle mouse interaction. map key = X coordinate on graph image, value = related index of GraphData()
; Average how many items use to calculate averaged value for current item? 2 is minimum, if less it will be disabled
; GridStepX X grid resolution [vertical lines], measured in GraphData() count. use 0 to disable
; GridStepY Y grid resolution [horizontal lines], measured in GraphData() values. 0 to disable
; FontSize font size of text labels. use 0 to disable labels
; ColorN colors for graph elements (RGB)
; RETURN: none, image modified on success
Procedure DrawSimpleGraph (hImgOut, Array GraphData(1), Map OutGeoData(), Average, GridStepX = 10, GridStepY = 10, FontSize = 9, Color1 = $00DDDD, Color2 = $DDDD00)
ClearMap(OutGeoData()) ; reset mouse data
Protected maxItems = ArraySize(GraphData()) ; count of items (X)
Protected maxValues ; count of values (Y)
Protected highValue, lowValue ; highest and lowest GraphData values
Protected t ; generic temp variable
If maxItems < 0 : ProcedureReturn : EndIf ; quit if GraphData array is invalid
; detect higher/lower values
Protected Dim TData(0)
CopyArray(GraphData(), TData())
SortArray(TData(), #PB_Sort_Ascending)
lowValue = TData(0)
highValue = TData(maxItems)
FreeArray(TData())
; do some stuff to better fit background grid
If GridStepY > 0
If highValue >= 0
highValue + GridStepY - highValue % GridStepY
ElseIf highValue
highValue - highValue % GridStepY
EndIf
If lowValue > 0
lowValue - lowValue % GridStepY
ElseIf lowValue < 0
lowValue - (GridStepY + lowValue % GridStepY)
EndIf
EndIf
maxValues = highValue - lowValue
If maxValues <= 0
maxValues = 1
EndIf
; load font for text labels
Protected Font
If FontSize > 0
Font = LoadFont(#PB_Any, "arial", FontSize)
If Not IsFont(Font) ; and so on
Font = LoadFont(#PB_Any, "tahoma", FontSize)
If Not IsFont(Font) ; and so on.. :)
Font = LoadFont(#PB_Any, "consolas", FontSize)
EndIf
EndIf
EndIf
; draw data to image
If StartVectorDrawing(ImageVectorOutput(hImgOut, #PB_Unit_Pixel))
; [n] - define graph offsets / text labels sizes / etc
Protected.d oX = 1.0, oY = oX
Protected.d mtW = 1.0, mtH = mtW
If IsFont(Font)
VectorFont(FontID(Font), FontSize)
If VectorTextWidth(Str(highValue)) > VectorTextWidth(Str(lowValue))
oX = VectorTextWidth(Str(highValue)) + 2.0
Else
oX = VectorTextWidth(Str(lowValue)) + 2.0
EndIf
oY = VectorTextHeight("0A") + 2.0
If oX > oY
oY = oX
Else
oX = oY
EndIf
mtW = VectorTextWidth(Str(maxItems)) * 1.2
mtH = VectorTextHeight(Str(highValue)) * 0.8
EndIf
; line width (pixels)
;Protected.d LineWidth = 1.0
Protected.d LineWidth = 2.0
; multipliers to scale graph coordinates
Protected.d mX = (VectorOutputWidth() - oX * 2.0) / (maxItems + Bool(maxItems = 0))
Protected.d mY = (VectorOutputHeight() - oY * 2.0) / maxValues
; tmp variables used in drawing
Protected.d cX, cY
; [0] - draw text labels and grid lines
;VectorSourceColor($77FFFFFF)
VectorSourceColor($FF000000+#Black)
Protected.d tLast
; horizontal lines/labels
tLast = oY + maxValues * mY + 5.0
If GridStepY > 0
For t = maxValues To 0 Step -1
If t % GridStepY = 0 Or t = maxValues
cY = oY + t * mY
; draw text label
If IsFont(Font) And cY < tLast
MovePathCursor(oX - (2.0 + VectorTextWidth(Str(highValue - t))), cY - VectorTextHeight(Str(highValue - t)) * 0.5)
DrawVectorText(Str(highValue - t))
tLast = cY - mtH
EndIf
; draw grid line
MovePathCursor(oX, oY + mY * t)
AddPathLine(oX + maxItems * mX, oY + mY * t)
EndIf
Next t
EndIf
; vertical lines/labels
tLast = 0.0
If GridStepX > 0
;GridStepX + 1
For t = 0 To maxItems
If t % GridStepX = 0 Or t = maxItems
cX = oX + t * mX
cY = oY + maxValues * mY
; draw text label
If IsFont(Font) And cX > tLast
MovePathCursor(cX - VectorTextWidth(Str(t)) * 0.5, cY + 2.0)
DrawVectorText(Str(t))
tLast = cX + mtW
EndIf
; draw grid line
MovePathCursor(cX, oY)
AddPathLine(cX, cY)
EndIf
Next t
EndIf
; fin
If GridStepX > 0 Or GridStepY > 0
DashPath(1.0, 3.0)
EndIf
; [1] - draw main line/items and form "geodata"
Protected.d gX = oX
Protected.d gX2
VectorSourceColor(Color1 | $FF000000)
MovePathCursor(oX, oY + mY * (highValue - GraphData(0)))
For t = 0 To maxItems
cX = oX + mX * t
cY = oY + mY * (highValue - GraphData(t))
AddPathLine(cX, cY) ; add line
;AddPathCircle(cX, cY, 2.0) ; add spot at the edge
AddPathCircle(cX, cY, 3.0) ; add spot at the edge
MovePathCursor(cX, cY) ; restore cursor pos
; build that "geodata" used to handle mouse
gX2 = gX + (cX - gX) * 0.5
While gX < gX2
OutGeoData(Str(Int(gX))) = t - 1
gX + 1.0
Wend
While gX < cX
OutGeoData(Str(Int(gX))) = t
gX + 1.0
Wend
gX = cX
Next t
StrokePath(LineWidth)
; [2] - draw "trend"/averaged line
Protected NewList Avg() ; stack to store recent values
Protected.d Avg ; to calculate current result
If Average > maxItems + 1
Average = maxItems + 1
EndIf
If Average > 1 ; it makes sense to draw only if it is greater than 1
While ListSize(Avg()) < Average
AddElement(Avg()) ; 'extrapolate' using first item data, without that it looks worst as for me :3
Avg() = GraphData(0)
Wend
MovePathCursor(oX, oY + (highValue - GraphData(0)) * mY)
For t = 0 To maxItems
FirstElement(Avg()) ; this all works like a stack structure, with max deepth = Average
DeleteElement(Avg())
LastElement(Avg())
AddElement(Avg())
Avg() = GraphData(t) ; push new element
Avg = 0.0 ; calculate averaged value from recent elements
ForEach Avg()
Avg + Avg()
Next
cX = oX + mX * t
cY = oY + (highValue - Avg / Average) * mY
AddPathLine(cX, cY)
AddPathCircle(cX, cY, 1.0) ; add spot at the edge
MovePathCursor(cX, cY) ; restore cursor pos
Next t
VectorSourceColor(Color2 | $FF000000)
StrokePath(LineWidth)
EndIf
; cls
FreeList(Avg())
StopVectorDrawing()
EndIf
; cls
If IsFont(Font)
FreeFont(Font)
EndIf
EndProcedure
;}