-
Notifications
You must be signed in to change notification settings - Fork 65
/
AxesMorph.class.st
422 lines (352 loc) · 10.3 KB
/
AxesMorph.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
"
I am responsible for drawing a pair of axes, a grid, and various labels. I am not responsible for the display of any data, or for handling user input in any special way.
"
Class {
#name : #AxesMorph,
#superclass : #RectangleMorph,
#instVars : [
'form',
'limitMaxX',
'limitMinX',
'limitMaxY',
'limitMinY',
'title',
'xmax',
'xmid',
'xmin',
'ymax',
'ymid',
'ymin',
'xAxisFormatter',
'yAxisFormatter',
'margin',
'grid',
'drawCotas'
],
#category : #'CogTools-VMProfiler'
}
{ #category : #accessing }
AxesMorph >> baseColor [
"Answer the base color to calculate other colors from"
| baseColor current |
baseColor := self color.
current := self.
[current notNil & (baseColor = Color transparent)]
whileTrue: [""
baseColor := current color.
current := current owner].
^ baseColor
]
{ #category : #'change reporting' }
AxesMorph >> changed [
super changed.
form := nil.
]
{ #category : #accessing }
AxesMorph >> color: aColor [
super color: aColor.
self updateCotas
]
{ #category : #drawing }
AxesMorph >> cotaColor [
| baseColor lighter darker |
baseColor := self baseColor asNontranslucentColor.
baseColor = Color white
ifTrue: [^ Color black].
""
lighter := baseColor muchLighter.
darker := baseColor muchDarker.
""
^ (lighter diff: baseColor) > (darker diff: baseColor)
ifTrue: [lighter]
ifFalse: [darker]
]
{ #category : #utility }
AxesMorph >> dataPointToGridPoint: aPoint [
"Compute the pixel coordinates wrt the grid origin of the given data point."
| drawBounds |
drawBounds := self drawBounds.
^ (aPoint - self minPoint) * (drawBounds width @ drawBounds height negated).
]
{ #category : #utility }
AxesMorph >> dataPointToWorldPoint: aPoint [
"Compute the pixel coordinates wrt the World origin of the given data point."
^ (self dataPointToGridPoint: aPoint) + self gridOrigin
]
{ #category : #geometry }
AxesMorph >> drawBounds [
"answer the rectangle inside the morph where the plot is drawn"
^ (0 @ 0 rect: self width @ self height - (self borderWidth * 2))
insetBy: margin
]
{ #category : #drawing }
AxesMorph >> drawGridOn: aCanvas [
| gridColor right bottom width height lighter darker baseColor |
baseColor := self baseColor.
lighter := baseColor twiceLighter.
darker := baseColor twiceDarker.
gridColor := (lighter diff: baseColor)
> (darker diff: baseColor) ifTrue: [lighter] ifFalse: [darker].
""
right := self bounds width - margin.
width := self bounds width - (margin * 2).
bottom := self bounds height - margin.
height := self bounds height - (margin * 2).
(margin to: right by: width / 10) do:
[:x | | xRounded |
xRounded := x rounded.
aCanvas
line: xRounded @ margin
to: xRounded @ bottom
color: gridColor].
(margin to: bottom by: height / 10) do:
[:y | | yRounded |
yRounded := y rounded.
aCanvas
line: margin @ yRounded
to: right @ yRounded
color: gridColor]
]
{ #category : #drawing }
AxesMorph >> drawOn: aCanvas [
super drawOn: aCanvas.
aCanvas
image: self form
at: self topLeft + self borderWidth
rule: Form blend
]
{ #category : #geometry }
AxesMorph >> extent: aPoint [
super
extent: (aPoint max: self minExtent)
]
{ #category : #naming }
AxesMorph >> externalName [
^ super externalName, (title contents isEmpty ifTrue:[''] ifFalse:[' - ', title contents])
]
{ #category : #drawing }
AxesMorph >> form [
form ifNil: [
Cursor wait showWhile: [
form := Form
extent: (self bounds insetBy: self borderWidth) extent
depth: Display depth.
form fillColor: self color.
self updateForm]].
^ form
]
{ #category : #accessing }
AxesMorph >> graphBounds: aRectangle [
"Sets the axes and then draws."
Transcript cr; print: aRectangle; flush.
limitMinX := aRectangle left.
limitMinY := aRectangle bottom.
limitMaxX := aRectangle right.
limitMaxY := aRectangle top.
self changed
]
{ #category : #accessing }
AxesMorph >> gridOrigin [
"Answer the intersection of the two axes (lower left corner of the grid)"
| inset |
inset := self borderWidth + margin.
^ self bottomLeft + (inset @ inset negated)
]
{ #category : #utility }
AxesMorph >> gridPointToDataPoint: aPoint [
"Compute the coordinates of the data point corresponding to the given grid point (given in pixel coordinates wrt the grid origin)."
| drawBounds |
drawBounds := self drawBounds.
^ (aPoint x @ aPoint y negated) / (drawBounds extent) + self minPoint
]
{ #category : #initialization }
AxesMorph >> initialize [
super initialize.
self color: Color gray.
grid := PlotMorphGrid on: self.
xAxisFormatter := [:x | x printString].
yAxisFormatter := [:y | y printString].
self initializeCotas.
margin := 15 max: (title height + 2).
form := nil.
self extent: 1@1.
]
{ #category : #initialization }
AxesMorph >> initializeCotas [
drawCotas := true.
""
title := StringMorph contents: '' font: TextStyle defaultFont emphasis: 1.
xmax := StringMorph contents: ''.
xmid := StringMorph contents: ''.
xmin := StringMorph contents: ''.
ymax := StringMorph contents: ''.
ymid := StringMorph contents: ''.
ymin := StringMorph contents: ''.
""
self addMorph: title.
self addMorph: xmax.
self addMorph: xmid.
self addMorph: xmin.
self addMorph: ymax.
self addMorph: ymid.
self addMorph: ymin.
""
limitMinX := 0.
limitMaxX := 1.0.
limitMinY := 0.
limitMaxY := 1.0.
]
{ #category : #accessing }
AxesMorph >> limitMaxX: aNumberOrNil [
"Set the maximum value along the X axis. If nil, this value will be computed from the data points to be displayed (subclass responsibility, since AxesMorph doesn't know anything about data)."
limitMaxX := aNumberOrNil.
self changed.
]
{ #category : #accessing }
AxesMorph >> limitMaxY: aNumberOrNil [
"Set the maximum value along the X axis. If nil, this value will be computed from the data points to be displayed (subclass responsibility, since AxesMorph doesn't know anything about data)."
limitMaxY := aNumberOrNil.
self changed
]
{ #category : #accessing }
AxesMorph >> limitMinX: aNumberOrNil [
"Set the maximum value along the X axis. If nil, this value will be computed from the data points to be displayed (subclass responsibility, since AxesMorph doesn't know anything about data)."
limitMinX := aNumberOrNil.
self changed
]
{ #category : #accessing }
AxesMorph >> limitMinX: minNumberOrNil limitMaxX: maxNumberOrNil [
"Set the minimum and maximum values along the X axis. If nil, these
values will be computed from the data points to be displayed (subclass
responsibility, since AxesMorph doesn't know anything about data)."
limitMinX := minNumberOrNil.
limitMaxX := maxNumberOrNil.
self changed
]
{ #category : #accessing }
AxesMorph >> limitMinY: aNumberOrNil [
"Set the maximum value along the X axis. If nil, this value will be computed from the data points to be displayed (subclass responsibility, since AxesMorph doesn't know anything about data)."
limitMinY := aNumberOrNil.
self changed
]
{ #category : #accessing }
AxesMorph >> margin [
"Answer the width of the margin surrounding the grid."
^margin
]
{ #category : #accessing }
AxesMorph >> margin: anInteger [
"Set the size of the margin surrounding the grid."
margin := anInteger.
self changed
]
{ #category : #drawing }
AxesMorph >> maxPoint [
"Limit values must be non-nil"
^ limitMaxX @ limitMaxY
]
{ #category : #geometry }
AxesMorph >> minExtent [
^ 125 @ 125 + margin
]
{ #category : #drawing }
AxesMorph >> minPoint [
"Limit values must be non-nil"
^ limitMinX @ limitMinY
]
{ #category : #accessing }
AxesMorph >> referenceColor [
"This name is confusing because it sounds like it has something to do with PlotMorphs 'references' instance variable."
self deprecatedExplanation: 'use #baseColor instead'.
^ self baseColor
]
{ #category : #accessing }
AxesMorph >> shouldDrawAxis: aBoolean [
grid drawAxis: aBoolean.
self changed
]
{ #category : #accessing }
AxesMorph >> shouldDrawCotas: aBoolean [
aBoolean = drawCotas ifTrue: [^self].
""
drawCotas := aBoolean.
title visible: aBoolean.
xmax visible: aBoolean.
xmid visible: aBoolean.
xmin visible: aBoolean.
ymax visible: aBoolean.
ymid visible: aBoolean.
ymin visible: aBoolean.
""
self changed
]
{ #category : #accessing }
AxesMorph >> shouldDrawGrid: aBoolean [
grid drawGrid: aBoolean.
self changed
]
{ #category : #accessing }
AxesMorph >> title:aString [
title contents: aString
]
{ #category : #drawing }
AxesMorph >> updateCotas [
| cotaColor |
xmax isNil
ifTrue: [^ self].
""
cotaColor := self cotaColor.
title color: cotaColor.
xmax color: cotaColor.
xmid color: cotaColor.
xmin color: cotaColor.
ymax color: cotaColor.
ymid color: cotaColor.
ymin color: cotaColor.
""
xmax
contents: (xAxisFormatter value: self maxPoint x).
xmid
contents: (xAxisFormatter value: self maxPoint x + self minPoint x / 2).
xmin
contents: (xAxisFormatter value: self minPoint x).
ymax
contents: (yAxisFormatter value: self maxPoint y).
ymid
contents: (yAxisFormatter value: self maxPoint y + self minPoint y / 2).
ymin
contents: (yAxisFormatter value: self minPoint y).
""
title position: self topLeft + ((self width - title width / 2) rounded @ 0) + (0 @ self borderWidth).
""
xmax position: self topLeft + (self width - xmax width @ (self height - xmax height)) - (margin @ self borderWidth).
xmid position: self topLeft + ((self width - xmid width / 2) rounded @ (self height - xmid height)) - (0 @ self borderWidth).
xmin position: self topLeft + (0 @ (self height - xmin height)) + (margin @ 0) - (0 @ self borderWidth).
""
ymax position: self topLeft + ((0 - ymax width max: 0)
@ 0) + (self borderWidth @ margin).
ymid position: self topLeft + ((15 - ymid width max: 0)
@ (self height - ymid height / 2) rounded) + (self borderWidth @ 0).
ymin position: self topLeft + ((0 - ymin width max: 0)
@ (self height - ymin height)) - (0 @ margin) + (self borderWidth @ 0)
]
{ #category : #drawing }
AxesMorph >> updateForm [
self updateCotas.
grid drawOn: form getCanvas.
]
{ #category : #utility }
AxesMorph >> worldPointToDataPoint: aPoint [
"Compute the pixel coordinates of the given data point wrt the World origin."
^ self gridPointToDataPoint: aPoint - self gridOrigin
]
{ #category : #accessing }
AxesMorph >> xAxisFormatter: aFormatterBlock [
xAxisFormatter := aFormatterBlock.
self updateCotas
]
{ #category : #accessing }
AxesMorph >> yAxisFormatter: aFormatterBlock [
yAxisFormatter := aFormatterBlock.
self updateCotas
]