-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlotArea.xojo_code
More file actions
583 lines (489 loc) · 14.3 KB
/
Copy pathPlotArea.xojo_code
File metadata and controls
583 lines (489 loc) · 14.3 KB
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
#tag Class
Protected Class PlotArea
Inherits Canvas
#tag Event
Sub Paint(g As Graphics, areas() As REALbasic.Rect)
PlotGraph(g)
End Sub
#tag EndEvent
#tag Method, Flags = &h0
Sub ClearData()
redim data(-1)
redim BarColors(-1)
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub drawBars(g as Graphics)
if data <> nil and data.Ubound <> -1 then
// Take the length of the line
dim w as Double = g.Width - leftMargin - rightMargin
dim h as double = g.Height - topMargin - bottomMargin
//divide the line into nbins.
Dim nbins as integer = data.Ubound+1 // The number of bins
dim binWidth as double = w / nbins
dim gap as double = (binWidth *.1) / 2 // make a small gap between bars.
dim scaledWidth as double = binWidth * .9 // .05 + .9 + .05 = 1
dim maxDataValue as double = Max(data)
dim minDataValue as double = Min(data)
dim barYScale as double = h / (maxDataValue-minDataValue)
dim y as double = g.height - bottomMargin
For bin As Integer = 0 To nbins - 1
Dim x As double = leftMargin + (bin * w / nbins) ' Adjust the starting point to consider the padding
dim v as double = data(bin)
Dim barHeight As double = (v-minDataValue)*barYScale
g.ForeColor = BarColors(bin)
g.FillRect(x+gap, y, scaledWidth, -barHeight) ' Adjust startX for the position of the current major tick
g.ForeColor = color.DarkGray
g.drawRect(x+gap, y, scaledWidth, -barHeight) ' Adjust startX for the position of the current major tick
next
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub drawHorizontalAxis(g as Graphics)
' Start x-axis from leftMargin pixels from the left edge
g.ForeColor = AxisColor
g.DrawLine(leftMargin, g.Height - bottomMargin, g.Width - rightMargin, g.Height - bottomMargin)
// Take the length of the line
dim w as Double = g.Width - leftMargin - rightMargin
//divide the line into nbins.
Dim nbins as integer = data.Ubound+1 // The number of bins
Dim labelWidth as Integer
dim halfBinWidth as double = w / nbins / 2
if PType <> PlotType.Bar then
halfBinWidth = 0
end if
If ShowTicks Then
For bin As Integer = 0 To nbins - 1
Dim x As double = leftMargin + (bin * w / nbins) ' Adjust the starting point to consider the padding
labelWidth = StringWidth(g, Str(bin))
g.DrawString(Str(bin), x - labelWidth / 2 + halfBinWidth, g.Height - bottomMargin + g.TextHeight)
g.DrawLine(x, g.Height - bottomMargin+tickSize, x, g.Height-bottomMargin)
next
End If
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub drawLines(g as Graphics)
if data <> nil and data.Ubound <> -1 then
// Take the length of the line
dim w as Double = g.Width - leftMargin - rightMargin
dim h as double = g.Height - topMargin - bottomMargin
//divide the line into nbins.
Dim nbins as integer = data.Ubound+1 // The number of bins
dim maxDataValue as double = Max(data)
dim minDataValue as double = 0 //Min(data)
dim YScale as double = h / (maxDataValue-minDataValue+1)
For bin As Integer = 0 To nbins - 2
Dim x1 As double = leftMargin + (bin * w / nbins) ' Adjust the starting point to consider the padding
Dim y1 As double = (data(bin)-minDataValue)*YScale
Dim x2 As double = leftMargin + ((bin+1) * w / nbins) ' Adjust the starting point to consider the padding
Dim y2 As double = (data(bin+1)-minDataValue)*YScale
y1 = h - y1 + topMargin
y2 = h - y2 + topMargin
g.ForeColor = BarColors(bin)
PlotPointCircle(g,x1,y1,3)
g.DrawLine(x1,y1,x2,y2)
PlotPointCircle(g,x2,y2,3)
next
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub drawPoints(g as Graphics)
if data <> nil and data.Ubound <> -1 then
//Points in the data are organized in pairs of x,y
Dim barCount As Integer = Data.Ubound + 1
barCount = barCount \ 2 // Therefore should also be even.
// Take the length of the line
dim w as Double = g.Width - leftMargin - rightMargin
dim h as double = g.Height - topMargin - bottomMargin
//divide the line into nbins.
dim minX, maxX, minY, maxY as double
// Because data is in x,y format can't use the min max methods.
minX = data(0)
minY = data(1)
maxX = minX
maxY = minY
for p as integer = 0 to data.Ubound step 2
if data(p) < minX then
minX = data(p)
end if
if data(p) > maxX then
maxX = data(p)
end if
if data(p+1) < minY then
minY = data(p+1)
end if
if data(p+1) > maxY then
maxY = data(p+1)
end if
next
dim XScale, YScale as double
XScale = w / (maxX-minX+1)
YScale = h / (maxY-minY+1)
dim npairs as integer = (data.Ubound+1)/2
for p as integer = 0 to npairs-1
dim x as double = data(p*2)
dim y as double = data(p*2+1)
x = x - minX
y = y - MinY
x = x * XScale
y = y * YScale
x = x + leftMargin
y = y - topMargin
y = h - y // because 0 is at the top not the bottom.
dim c as Integer = p mod BarColors.Ubound
g.ForeColor = BarColors(c) // Caveat wrap around
PlotPointCircle(g,x,y,3)
next
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub drawVerticalAxis(g as Graphics)
' Start y-axis from bottomMargin pixels from the bottom edge
g.ForeColor = AxisColor
g.DrawLine(leftMargin, g.Height - bottomMargin, leftMargin, topMargin)
' Take the height of the axis
Dim h As Double = g.Height - topMargin - bottomMargin
' Determine the maximum value in the data array
Dim maxValue As Double = ceil(Max(data))
dim minValue as Double = floor(Min(data))
dim numTicks as Integer = 10
' Calculate the number of ticks based on a desired interval
Dim tickInterval As Double = (maxValue-minValue+1) / numTicks
Dim tickValue As Double = minValue
Dim labelPadding as double = 2
' Draw ticks and labels
For i As Integer = 0 To numTicks
Dim y As Double = g.Height - bottomMargin - (i * h / numTicks)
' Draw tick mark
g.DrawLine(leftMargin - tickSize, y, leftMargin, y)
' Draw label
dim vstr as string = Format(tickValue, "+0.0")
g.DrawString(vstr, leftMargin - tickSize - labelPadding - StringWidth(g, vstr), y + g.TextHeight /4)
' Increment tick value
tickValue = tickValue + tickInterval
Next
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Function Max(data() as integer) As integer
if data<> nil and data.Ubound <> -1 then
dim m as integer
m = data(0)
for i as integer = 0 to data.Ubound
if data(i) > m then m = data(i)
next
return m
else
Raise New OutOfBoundsException
end if
//Not reached
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function Min(data() as integer) As integer
if data<> nil and data.Ubound <> -1 then
dim m as integer
m = data(0)
for i as integer = 0 to data.Ubound
if data(i) < m then m = data(i)
next
return m
else
Raise New OutOfBoundsException
end if
//Not reached
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub Plot(userData() as integer, AxisColor as color, newBarColors() as color, ptype as PlotType)
for i as integer = 0 to userData.Ubound
data.Append(userData(i))
next
for i as integer = 0 to newBarColors.Ubound
BarColors.Append(newBarColors(i))
next
me.AxisColor = AxisColor
me.PType = ptype
me.Refresh
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub PlotGraph(g as graphics)
If Data <> Nil And Data.Ubound <> -1 Then
If ShowAxis Then
drawHorizontalAxis(g)
drawVerticalAxis(g)
end if
select case PType
case PlotType.Bar
drawBars(g)
case PlotType.Line
drawLines(g)
case PlotType.Scatter
drawPoints(g)
end select
End If
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub PlotPointCircle(g as Graphics, x as double, y as Double, r as Double)
//g.ForeColor = color.red
g.FillOval(x-r,y-2, 2*r, 2*r)
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Function StringWidth(g as Graphics, text as String) As integer
Dim tempBitmap As New Picture(1, 1)
Dim tempGraphics As Graphics = tempBitmap.Graphics
Dim width As Integer = tempGraphics.StringWidth(text)
tempGraphics = Nil
tempBitmap = Nil
Return width
End Function
#tag EndMethod
#tag Property, Flags = &h0
AxisColor As Color = color.red
#tag EndProperty
#tag Property, Flags = &h0
BarColors(-1) As Color
#tag EndProperty
#tag Property, Flags = &h21
Private data() As Integer
#tag EndProperty
#tag Property, Flags = &h0
PType As PlotType = PlotType.Line
#tag EndProperty
#tag Property, Flags = &h0
ShowAxis As Boolean = True
#tag EndProperty
#tag Property, Flags = &h0
ShowTicks As Boolean = True
#tag EndProperty
#tag Constant, Name = bottomMargin, Type = Double, Dynamic = False, Default = \"20", Scope = Public
#tag EndConstant
#tag Constant, Name = leftMargin, Type = Integer, Dynamic = False, Default = \"40", Scope = Public
#tag EndConstant
#tag Constant, Name = rightMargin, Type = Double, Dynamic = False, Default = \"20", Scope = Public
#tag EndConstant
#tag Constant, Name = tickSize, Type = Double, Dynamic = False, Default = \"5", Scope = Public
#tag EndConstant
#tag Constant, Name = topMargin, Type = Double, Dynamic = False, Default = \"10", Scope = Public
#tag EndConstant
#tag Enum, Name = PlotType, Type = Integer, Flags = &h0
Bar
Line
Scatter
#tag EndEnum
#tag ViewBehavior
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
Type="String"
EditorType="String"
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
Type="Integer"
EditorType="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
Type="String"
EditorType="String"
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Width"
Visible=true
Group="Position"
InitialValue="100"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Height"
Visible=true
Group="Position"
InitialValue="100"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="LockLeft"
Visible=true
Group="Position"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="LockTop"
Visible=true
Group="Position"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="LockRight"
Visible=true
Group="Position"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="LockBottom"
Visible=true
Group="Position"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="TabIndex"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="TabStop"
Visible=true
Group="Position"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="AutoDeactivate"
Visible=true
Group="Appearance"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="Backdrop"
Visible=true
Group="Appearance"
Type="Picture"
EditorType="Picture"
#tag EndViewProperty
#tag ViewProperty
Name="Enabled"
Visible=true
Group="Appearance"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="HelpTag"
Visible=true
Group="Appearance"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="UseFocusRing"
Visible=true
Group="Appearance"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="Visible"
Visible=true
Group="Appearance"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="AcceptFocus"
Visible=true
Group="Behavior"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="AcceptTabs"
Visible=true
Group="Behavior"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="DoubleBuffer"
Visible=true
Group="Behavior"
InitialValue="False"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="Transparent"
Visible=true
Group="Behavior"
InitialValue="True"
Type="Boolean"
EditorType="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="ShowAxis"
Visible=true
Group="Behavior"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="ShowTicks"
Visible=true
Group="Behavior"
InitialValue="True"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="AxisColor"
Visible=true
Group="Behavior"
InitialValue="color.blue"
Type="Color"
#tag EndViewProperty
#tag ViewProperty
Name="PType"
Visible=true
Group="Behavior"
InitialValue="PlotType.Line"
Type="PlotType"
EditorType="Enum"
#tag EnumValues
"0 - Bar"
"1 - Line"
"2 - Scatter"
#tag EndEnumValues
#tag EndViewProperty
#tag ViewProperty
Name="TabPanelIndex"
Group="Position"
InitialValue="0"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="EraseBackground"
Group="Behavior"
InitialValue="True"
Type="Boolean"
EditorType="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="InitialParent"
Type="String"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass