/
PlotMorph.class.st
570 lines (510 loc) · 13.6 KB
/
PlotMorph.class.st
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
"
I can draw many series of Points in a XY grid. See the methods testXXX in the class side.
Samples:
PlotMorph test.
PlotMorph test2.
PlotMorph test4.
PlotMorph testWithReferences.
"
Class {
#name : #PlotMorph,
#superclass : #AxesMorph,
#instVars : [
'series',
'cachedMaxPoint',
'cachedMinPoint',
'lens',
'scaledPoints',
'references',
'processMouseDown',
'balloonFormatter'
],
#category : #'CogTools-VMProfiler'
}
{ #category : #'instance creation' }
PlotMorph class >> plotPoints: aPointOrderedCollection [
| plotMorph |
plotMorph := PlotMorph new.
plotMorph color: Color black twiceLighter twiceLighter;
title: 'Colors';
extent: 700 @ 300;
useRoundedCorners;
borderRaised.
plotMorph series: #series color: Color white;
series: #series drawLine: false.
aPointOrderedCollection
do: [:e | ""
plotMorph series: #series addPoint: e].
plotMorph openInWorld.
^ plotMorph
]
{ #category : #'instance creation' }
PlotMorph class >> plotSeries: aPointOrderedCollection [
| plotMorph |
plotMorph := PlotMorph new.
plotMorph color: Color gray lighter;
title: 'Colors';
extent: 700 @ 300;
useRoundedCorners;
borderRaised.
plotMorph series: #series color: Color red;
series: #series drawLine: false.
aPointOrderedCollection
do: [:e | ""
plotMorph series: #series addPoint: e].
plotMorph openInWorld.
^ plotMorph
]
{ #category : #testing }
PlotMorph class >> test [
"
PlotMorph test
"
| pm |
pm := PlotMorph new.
pm
color: (Color
r: 0.0
g: 0.376
b: 0.317);
extent: 320 @ 320;
borderWidth: 2;
useRoundedCorners;
setBorderStyle: #raised;
title: 'Some test functions'.
pm series: #sin color: Color red;
series: #cos color: Color blue;
series: #test color: Color yellow.
pm series: #sin drawArea: true;
series: #cos drawArea: true;
series: #test drawArea: true.
pm series: #sin description: 'sin';
series: #cos description: 'cosin';
series: #test description: 'test'.
pm series: #test type: #stepped.
pm series: #sin width: 2;
series: #sin drawLine: false.
""
pm
yAxisFormatter: [:y | (y roundTo: 0.1) asString].
""
0
to: 360
by: 10
do: [:x |
pm series: #sin addPoint: x @ x degreesToRadians sin.
pm series: #cos addPoint: x @ x degreesToRadians cos.
pm series: #test addPoint: x @ (x degreesToRadians cos + x degreesToRadians sin)].
""
pm openInWorld
]
{ #category : #testing }
PlotMorph class >> test2 [
"
PlotMorph test2
"
| pm sigmoid |
pm := PlotMorph new.
pm title: 'Sigmoid';
extent: 250 @ 250;
color: Color black.
""
pm series: #sigmoid1 color: Color red;
series: #sigmoid1 drawPoints: false;
series: #sigmoid2 color: Color blue;
series: #sigmoid2 drawPoints: false;
series: #sigmoid3 color: Color yellow;
series: #sigmoid3 drawPoints: false;
series: #sigmoid4 color: Color green;
series: #sigmoid4 drawPoints: false;
series: #sigmoid5 color: Color white;
series: #sigmoid5 drawPoints: false.
""
pm
yAxisFormatter: [:y | (y roundTo: 0.1) asString].
sigmoid := [:x :slope | 1 / (1 + (slope * x) negated exp)].
-10
to: 10
by: 0.25
do: [:x |
pm series: #sigmoid1 addPoint: x
@ (sigmoid value: x value: 3).
pm series: #sigmoid2 addPoint: x
@ (sigmoid value: x value: 2).
pm series: #sigmoid3 addPoint: x
@ (sigmoid value: x value: 1).
pm series: #sigmoid4 addPoint: x
@ (sigmoid value: x value: 1 / 2).
pm series: #sigmoid5 addPoint: x
@ (sigmoid value: x value: 1 / 3)].
pm openInWorld
]
{ #category : #testing }
PlotMorph class >> test4 [
"
PlotMorph test4
"
| pm function |
pm := PlotMorph new.
pm
color: (Color blue twiceDarker twiceDarker twiceDarker alpha: 0.3);
extent: 300 @ 300;
useRoundedCorners.
pm
xAxisFormatter: [:x | x rounded asStringWithCommas].
pm
yAxisFormatter: [:y | y rounded asString].
pm title: 'Some funny function'.
pm series: #test2 color: Color red;
series: #test2 drawPoints: false.
function := [:x | x degreesToRadians sin / 5 + ((x / 10) degreesToRadians cos + (x / 10) degreesToRadians sin) * 100].
0
to: 3000
by: 5
do: [:x | pm series: #test2 addPoint: x
@ (function value: x)].
pm openInWorld
]
{ #category : #testing }
PlotMorph class >> testWithReferences [
"
PlotMorph testWithReferences.
"
| pm ref |
ref := AlignmentMorph newColumn.
ref color: Color magenta twiceDarker twiceDarker;
hResizing: #shrinkWrap;
vResizing: #shrinkWrap;
wrapCentering: #center;
cellPositioning: #leftCenter.
""
pm := PlotMorph new.
pm references: ref.
pm color: Color magenta twiceDarker twiceDarker;
extent: 300 @ 300;
borderWidth: 0;
title: 'Some test functions'.
pm series: #sin color: Color red;
series: #cos color: Color blue;
series: #test color: Color yellow.
pm series: #sin drawArea: true;
series: #cos drawArea: true;
series: #test drawArea: true.
pm series: #sin description: 'sin';
series: #cos description: 'cosin';
series: #test description: 'test'.
pm series: #test type: #stepped.
0
to: 360
by: 10
do: [:x |
pm series: #sin addPoint: x @ x degreesToRadians sin.
pm series: #cos addPoint: x @ x degreesToRadians cos.
pm series: #test addPoint: x @ (x degreesToRadians cos + x degreesToRadians sin)].
""
ref openInWorld.
pm openInWorld
]
{ #category : #accessing }
PlotMorph >> balloonFormatter: anObject [
balloonFormatter := anObject
]
{ #category : #'change reporting' }
PlotMorph >> changed [
cachedMaxPoint := nil.
cachedMinPoint := nil.
super changed
]
{ #category : #accessing }
PlotMorph >> clear [
series do:[:each | each clear].
self seriesChanged
]
{ #category : #private }
PlotMorph >> exploreExtrasAt: nearPoint [
| extras |
extras := (self scaledPoints at: nearPoint)
collect: [:each | each extra].
extras := extras
select: [:each | each notNil].
extras isEmpty ifFalse:[
extras explore]
]
{ #category : #private }
PlotMorph >> findNearestPointTo: targetPoint [
| nearestPoint |
nearestPoint := nil.
Cursor wait
showWhile: [""
self scaledPoints
keysDo: [:scaledPoint | ""
(nearestPoint isNil
or: [(targetPoint dist: scaledPoint)
< (targetPoint dist: nearestPoint)])
ifTrue: [nearestPoint := scaledPoint]]].
^ nearestPoint
]
{ #category : #'event handling' }
PlotMorph >> handlesMouseDown: evt [
^ processMouseDown
]
{ #category : #initialization }
PlotMorph >> initialize [
super initialize.
series := Dictionary new.
processMouseDown := true.
lens := nil.
balloonFormatter := [:aCollection | self textForBalloon: aCollection].
self extent: 1 @ 1
]
{ #category : #initialization }
PlotMorph >> initializeCotas [
"Don't put initial limits on the grid range... default is to compute them from series data."
super initializeCotas.
limitMinX := limitMaxX := limitMinY := limitMaxY := nil.
]
{ #category : #drawing }
PlotMorph >> maxPoint [
cachedMaxPoint
ifNil: [""
limitMaxX notNil & limitMaxY notNil
ifTrue: [cachedMaxPoint := limitMaxY @ limitMaxY]
ifFalse: [| maxPoints |
maxPoints := series
collect: [:serie | serie maxPoint]
thenSelect: [:point | point notNil].
cachedMaxPoint := maxPoints isEmpty
ifTrue: [1 @ 1]
ifFalse: [maxPoints max].
limitMaxX notNil
ifTrue: [cachedMaxPoint := limitMaxX @ cachedMaxPoint y].
limitMaxY notNil
ifTrue: [cachedMaxPoint := cachedMaxPoint x @ limitMaxY]]].
^ cachedMaxPoint
]
{ #category : #drawing }
PlotMorph >> minPoint [
cachedMinPoint
ifNil: [""
limitMinX notNil & limitMinY notNil
ifTrue: [cachedMinPoint := limitMinX @ limitMinY]
ifFalse: [| minPoints |
minPoints := series
collect: [:serie | serie minPoint]
thenSelect: [:point | point notNil].
cachedMinPoint := minPoints isEmpty
ifTrue: [0 @ 0]
ifFalse: [minPoints min].
limitMinX notNil
ifTrue: [cachedMinPoint := limitMinX
@ cachedMinPoint y].
limitMinY notNil
ifTrue: [cachedMinPoint := cachedMinPoint x
@ limitMinY]]].
^ cachedMinPoint
]
{ #category : #'event handling' }
PlotMorph >> mouseDown: anEvent [
| nearPoint |
nearPoint := self findNearestPointTo: anEvent position - self topLeft - self borderWidth.
nearPoint
ifNotNil: [anEvent redButtonChanged
ifTrue: [self showLensAt: nearPoint]
ifFalse: [self exploreExtrasAt: nearPoint]]
]
{ #category : #'event handling' }
PlotMorph >> mouseUp: anEvent [
lens isNil ifTrue:[^ self].
""
lens deleteBalloon.
lens delete.
lens := nil
]
{ #category : #accessing }
PlotMorph >> processMouseDown: aBoolean [
processMouseDown := aBoolean
]
{ #category : #accessing }
PlotMorph >> references: aMorphOrNil [
"Specifies a morph (if not nil) that is updated with the names of the plotted series, displayed in the same color as the actual plot."
references := aMorphOrNil
]
{ #category : #drawing }
PlotMorph >> scalePoints [
| |
scaledPoints := nil.
series
do: [:serie | serie
scaleTo: self drawBounds
height: self height - (self borderWidth * 2)
maxPoint: self maxPoint
minPoint: self minPoint]
]
{ #category : #drawing }
PlotMorph >> scaledPoints [
^ scaledPoints
ifNil: [scaledPoints := Dictionary new.
series
do: [:serie | serie points
do: [:point |
| allPoints |
allPoints := scaledPoints
at: point scaledPoint
ifAbsentPut: [OrderedCollection new].
allPoints add: point]].
scaledPoints]
]
{ #category : #accessing }
PlotMorph >> series [
^series
]
{ #category : #series }
PlotMorph >> series: aSeriesOrSymbol [
"If aSeriesOrSymbol is a PlotSeries, simply answer it. Otherwise, it should be a string, and the returned value is the series with that name."
^ aSeriesOrSymbol isString
ifTrue: [| symbol |
symbol := aSeriesOrSymbol asSymbol.
series
at: symbol
ifAbsentPut: [PlotSeries name: symbol]]
ifFalse: [aSeriesOrSymbol]
]
{ #category : #series }
PlotMorph >> series: aSymbol addPoint: aPoint [
"Find the appropriate series and set a property in it."
(self series: aSymbol)
addPoint: aPoint.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol addPoint: aPoint extra: anObject [
"Find the appropriate series and set a property in it."
(self series: aSymbol)
addPoint: aPoint
extra: anObject.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol color: aColor [
"Find the appropriate series and set a property in it."
(self series:aSymbol) color:aColor.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol description: aString [
"Find the appropriate series and set a property in it."
(self series: aSymbol)
description: aString.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol drawArea: aBoolean [
"Find the appropriate series and set a property in it."
(self series: aSymbol)
drawArea: aBoolean.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol drawLine: aBoolean [
"Find the appropriate series and set a property in it."
(self series: aSymbol)
drawLine: aBoolean.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol drawPoints: aBoolean [
"Find the appropriate series and set a property in it."
(self series: aSymbol)
drawPoints: aBoolean.
self changed
]
{ #category : #series }
PlotMorph >> series: seriesSymbol type: lineTypeSymbol [
"Find the appropriate series and set a property in it."
(self series: seriesSymbol)
type: lineTypeSymbol.
self changed
]
{ #category : #series }
PlotMorph >> series: aSymbol width: anInteger [
"Find the appropriate series and set a property in it."
(self series: aSymbol) width: anInteger.
self changed
]
{ #category : #private }
PlotMorph >> seriesChanged [
cachedMaxPoint := nil.
cachedMinPoint := nil.
"If the morphs has no owner, then the morph is not open yet"
owner isNil
ifTrue:[^ self].
""
self changed.
self updateCotas
]
{ #category : #private }
PlotMorph >> showLensAt: nearPoint [
lens := EllipseMorph new.
lens
color: (Color red alpha: 0.5).
lens extent: 7 @ 7.
self addMorph: lens.
lens position: self topLeft + nearPoint - (3 @ 3) + self borderWidth.
lens
showBalloon: (balloonFormatter
value: (self scaledPoints at: nearPoint))
]
{ #category : #private }
PlotMorph >> textForBalloon: aCollection [
| stream point |
point := aCollection anyOne.
stream := String new writeStream.
stream
nextPutAll: (xAxisFormatter value: point x);
nextPutAll: ' ';
nextPutAll: (yAxisFormatter value: point y);
nextPut: Character cr.
aCollection
do: [:each |
stream nextPutAll: each series name.
each extra
ifNotNil: [stream nextPutAll: ': ';
print: each extra]]
separatedBy: [stream nextPut: Character cr].
^ stream contents
]
{ #category : #drawing }
PlotMorph >> updateForm [
"Override superclass implementation to do drawing of data."
| canvas |
self updateReferences.
self updateCotas.
self scalePoints.
canvas := form getCanvas.
grid drawOn: canvas.
(series values
asSortedCollection: [:x :y | x name <= y name])
do: [:serie | serie drawOn: canvas].
]
{ #category : #drawing }
PlotMorph >> updateReferences [
"Update a 'legend' displaying the description of each plotted series in the same color as that series."
| seriesWithDescription sortedSeried |
references isNil
ifTrue: [^ self].
""
references removeAllMorphs.
""
seriesWithDescription := series
reject: [:each | each description isEmpty].
sortedSeried := seriesWithDescription
asSortedCollection: [:x :y | x description asLowercase <= y description asLowercase].
sortedSeried
do: [:serie |
| ref |
ref := StringMorph new.
ref contents: serie description.
ref color: serie color.
references addMorphBack: ref.
serie]
]