5750
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5742
|
3 |
"{ Package: 'stx:libwidg2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
ImageView subclass:#GnuplotGraphView
|
5754
|
8 |
instanceVariableNames:'script data title formatHolder'
|
5742
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Views-Misc'
|
|
12 |
!
|
|
13 |
|
|
14 |
!GnuplotGraphView class methodsFor:'documentation'!
|
|
15 |
|
|
16 |
documentation
|
|
17 |
"
|
|
18 |
displays the graph of the data (in model) using a gnuplot script
|
5754
|
19 |
Now also shown in the collection-inspector tab.
|
5742
|
20 |
|
|
21 |
[author:]
|
|
22 |
cg
|
|
23 |
|
|
24 |
[instance variables:]
|
|
25 |
|
|
26 |
[class variables:]
|
|
27 |
|
|
28 |
[see also:]
|
|
29 |
|
|
30 |
"
|
|
31 |
!
|
|
32 |
|
|
33 |
examples
|
|
34 |
"
|
|
35 |
Notice that everything between [exBegin] and [exEnd] is extracted by the html-doc generator
|
|
36 |
to create nicely formatted and clickable executable examples in the generated html-doc.
|
|
37 |
(see the browsers class-documentation menu items for more)
|
|
38 |
|
5781
|
39 |
trying the widget as standAlone view (with a fix script):
|
5742
|
40 |
[exBegin]
|
|
41 |
GnuplotGraphView new
|
|
42 |
script:(GnuplotGraphView defaultScript);
|
|
43 |
model:(ValueHolder with:(Random new next:100));
|
5781
|
44 |
adjust:#smoothFit;
|
|
45 |
extent:400@300;
|
|
46 |
open
|
|
47 |
[exEnd]
|
|
48 |
|
|
49 |
trying the widget as standAlone view (with no fix script):
|
|
50 |
[exBegin]
|
|
51 |
GnuplotGraphView new
|
|
52 |
model:(ValueHolder with:(Random new next:100));
|
|
53 |
adjust:#smoothFit;
|
|
54 |
extent:300@200;
|
5742
|
55 |
open
|
|
56 |
[exEnd]
|
|
57 |
|
|
58 |
embedded in another view:
|
|
59 |
[exBegin]
|
|
60 |
|top v|
|
|
61 |
|
|
62 |
top := StandardSystemView new.
|
|
63 |
top extent:300@300.
|
5755
|
64 |
v := GnuplotGraphView new.
|
|
65 |
v data:(Random new next:100).
|
|
66 |
v origin:0.0@0.0 corner:1.0@1.0.
|
5742
|
67 |
top add:v.
|
|
68 |
top open
|
|
69 |
[exEnd]
|
5755
|
70 |
use as inspect tab:
|
|
71 |
[exBegin]
|
|
72 |
(Random new next:100) inspect.
|
|
73 |
[exEnd]
|
5742
|
74 |
"
|
|
75 |
! !
|
|
76 |
|
|
77 |
!GnuplotGraphView class methodsFor:'defaults'!
|
|
78 |
|
|
79 |
defaultScript
|
|
80 |
"return a default initial gnuplot script"
|
|
81 |
|
|
82 |
^ self defaultScriptForHistogram
|
|
83 |
!
|
|
84 |
|
5754
|
85 |
defaultScriptForDots
|
|
86 |
"a default initial gnuplot script to show a dots diagram"
|
|
87 |
|
|
88 |
^ '
|
5759
|
89 |
set term %(outputFormat) size %(width),%(height)
|
5754
|
90 |
set output "%(outputFile)"
|
|
91 |
set title "%(title)"
|
5781
|
92 |
set nokey
|
5762
|
93 |
plot [-10:110] ''%(data)''
|
5754
|
94 |
'
|
5781
|
95 |
|
|
96 |
"Modified: / 31-05-2018 / 11:54:43 / Claus Gittinger"
|
5754
|
97 |
!
|
|
98 |
|
5742
|
99 |
defaultScriptForHistogram
|
|
100 |
"a default initial gnuplot script to show a histogram"
|
|
101 |
|
|
102 |
^ '
|
5759
|
103 |
set term %(outputFormat) size %(width),%(height)
|
5742
|
104 |
set output "%(outputFile)"
|
|
105 |
set title "%(title)"
|
5781
|
106 |
set nokey
|
5742
|
107 |
plot [-10:110] ''%(data)'' with histogram
|
|
108 |
'
|
5781
|
109 |
|
|
110 |
"Modified: / 31-05-2018 / 11:54:47 / Claus Gittinger"
|
5754
|
111 |
!
|
|
112 |
|
|
113 |
defaultScriptForLines
|
|
114 |
"a default initial gnuplot script to show a line diagram"
|
|
115 |
|
|
116 |
^ '
|
5759
|
117 |
set term %(outputFormat) size %(width),%(height)
|
5754
|
118 |
set output "%(outputFile)"
|
|
119 |
set title "%(title)"
|
5781
|
120 |
set nokey
|
5754
|
121 |
plot ''%(data)'' with lines
|
|
122 |
'
|
5781
|
123 |
|
|
124 |
"Modified: / 31-05-2018 / 11:54:52 / Claus Gittinger"
|
5754
|
125 |
! !
|
|
126 |
|
|
127 |
!GnuplotGraphView class methodsFor:'menu specs'!
|
|
128 |
|
|
129 |
middleButtonMenuExtraSlice
|
|
130 |
"This resource specification was automatically generated
|
|
131 |
by the MenuEditor of ST/X."
|
|
132 |
|
|
133 |
"Do not manually edit this!! If it is corrupted,
|
|
134 |
the MenuEditor may not be able to read the specification."
|
|
135 |
|
|
136 |
|
|
137 |
"
|
|
138 |
MenuEditor new openOnClass:GnuplotGraphView andSelector:#middleButtonMenuExtraSlice
|
|
139 |
(Menu new fromLiteralArrayEncoding:(GnuplotGraphView middleButtonMenuExtraSlice)) startUp
|
|
140 |
"
|
|
141 |
|
|
142 |
<resource: #menu>
|
|
143 |
|
|
144 |
^
|
|
145 |
#(Menu
|
|
146 |
(
|
|
147 |
(MenuItem
|
|
148 |
label: '-'
|
|
149 |
)
|
|
150 |
(MenuItem
|
5781
|
151 |
enabled: hasNoScript
|
|
152 |
label: 'Histogram'
|
|
153 |
hideMenuOnActivated: false
|
|
154 |
choice: formatHolder
|
|
155 |
choiceValue: histogram
|
|
156 |
)
|
|
157 |
(MenuItem
|
|
158 |
enabled: hasNoScript
|
|
159 |
label: 'Line Graph'
|
|
160 |
hideMenuOnActivated: false
|
|
161 |
choice: formatHolder
|
|
162 |
choiceValue: lines
|
|
163 |
)
|
|
164 |
(MenuItem
|
|
165 |
enabled: hasNoScript
|
|
166 |
label: 'Dots'
|
|
167 |
hideMenuOnActivated: false
|
|
168 |
choice: formatHolder
|
|
169 |
choiceValue: dots
|
5754
|
170 |
)
|
|
171 |
)
|
|
172 |
nil
|
|
173 |
nil
|
|
174 |
)
|
5742
|
175 |
! !
|
|
176 |
|
|
177 |
!GnuplotGraphView methodsFor:'accessing'!
|
|
178 |
|
5759
|
179 |
adjust:layoutSymbol
|
|
180 |
image := nil.
|
|
181 |
super adjust:layoutSymbol.
|
|
182 |
self invalidate
|
|
183 |
!
|
|
184 |
|
5742
|
185 |
data
|
|
186 |
^ data
|
|
187 |
!
|
|
188 |
|
|
189 |
data:something
|
|
190 |
data := something.
|
|
191 |
!
|
|
192 |
|
5762
|
193 |
hasNoScript
|
|
194 |
^ [ script isEmptyOrNil ]
|
|
195 |
!
|
|
196 |
|
|
197 |
hasScript
|
|
198 |
^ [ script notEmptyOrNil ]
|
|
199 |
!
|
|
200 |
|
5759
|
201 |
magnificationFactor:aNumber
|
|
202 |
image := nil.
|
|
203 |
super magnificationFactor:aNumber.
|
|
204 |
self invalidate
|
|
205 |
!
|
|
206 |
|
5742
|
207 |
script
|
|
208 |
^ script
|
|
209 |
!
|
|
210 |
|
|
211 |
script:something
|
|
212 |
script := something.
|
|
213 |
!
|
|
214 |
|
|
215 |
title
|
|
216 |
^ title
|
|
217 |
!
|
|
218 |
|
|
219 |
title:something
|
|
220 |
title := something.
|
|
221 |
! !
|
|
222 |
|
5754
|
223 |
!GnuplotGraphView methodsFor:'aspects'!
|
|
224 |
|
|
225 |
formatHolder
|
|
226 |
formatHolder isNil ifTrue:[
|
|
227 |
formatHolder := #histogram asValue.
|
|
228 |
formatHolder onChangeSend:#formatChanged to:self.
|
|
229 |
].
|
|
230 |
^ formatHolder
|
|
231 |
! !
|
|
232 |
|
|
233 |
!GnuplotGraphView methodsFor:'defaults'!
|
|
234 |
|
|
235 |
defaultScript
|
|
236 |
|format|
|
|
237 |
|
|
238 |
format := self formatHolder value.
|
|
239 |
format == #histogram ifTrue:[
|
|
240 |
^ self class defaultScriptForHistogram
|
|
241 |
].
|
|
242 |
format == #lines ifTrue:[
|
|
243 |
^ self class defaultScriptForLines
|
|
244 |
].
|
|
245 |
format == #dots ifTrue:[
|
|
246 |
^ self class defaultScriptForDots
|
|
247 |
].
|
|
248 |
^ self class defaultScript
|
|
249 |
! !
|
|
250 |
|
5742
|
251 |
!GnuplotGraphView methodsFor:'drawing'!
|
|
252 |
|
|
253 |
generateDataFileIn:aDirectory
|
|
254 |
"the format of data expected by gnuplot depends on the type of plot:
|
|
255 |
regular plot: a number of values (each in a separate line)
|
|
256 |
multiCol plots: a number of lines, each containing a row
|
|
257 |
here, handle common situations;
|
|
258 |
however, if the data does not match, the program should have prepared the
|
|
259 |
data into a string and present it as such.
|
|
260 |
Strings will be sent as-is to the file"
|
|
261 |
|
|
262 |
|dataFilename printRow|
|
|
263 |
|
|
264 |
dataFilename := (Filename newTemporaryIn:aDirectory) pathName.
|
|
265 |
dataFilename := dataFilename asFilename withSuffix:'dat'.
|
|
266 |
|
|
267 |
(data isString or:[data isByteArray]) ifTrue:[
|
|
268 |
dataFilename contents:data.
|
|
269 |
^ dataFilename.
|
|
270 |
].
|
|
271 |
|
|
272 |
data isCollection ifFalse:[
|
|
273 |
dataFilename contents:data printString.
|
|
274 |
^ dataFilename.
|
|
275 |
].
|
|
276 |
|
|
277 |
printRow :=
|
|
278 |
[:s :eachValueOrRow |
|
|
279 |
eachValueOrRow isString ifTrue:[
|
|
280 |
s nextPutAll:eachValueOrRow
|
|
281 |
] ifFalse:[
|
|
282 |
eachValueOrRow isAssociation ifTrue:[
|
|
283 |
eachValueOrRow key printOn:s.
|
|
284 |
s space.
|
|
285 |
eachValueOrRow value printOn:s.
|
|
286 |
] ifFalse:[
|
|
287 |
eachValueOrRow isCollection ifTrue:[
|
|
288 |
eachValueOrRow
|
|
289 |
do:[:element | element printOn:s]
|
|
290 |
separatedBy:[ s space]
|
|
291 |
] ifFalse:[
|
|
292 |
eachValueOrRow printOn:s
|
|
293 |
].
|
|
294 |
].
|
|
295 |
].
|
|
296 |
s cr.
|
|
297 |
].
|
|
298 |
|
|
299 |
dataFilename writingFileDo:[:s |
|
|
300 |
data isSequenceable ifTrue:[
|
|
301 |
data do:[:eachValueOrRow |
|
|
302 |
printRow value:s value:eachValueOrRow.
|
|
303 |
].
|
|
304 |
] ifFalse:[
|
|
305 |
data keysAndValuesDo:[:eachKey :eachValueOrRow |
|
|
306 |
eachKey printOn:s.
|
|
307 |
s space.
|
|
308 |
printRow value:s value:eachValueOrRow.
|
|
309 |
].
|
|
310 |
].
|
|
311 |
].
|
|
312 |
^ dataFilename
|
|
313 |
!
|
|
314 |
|
|
315 |
generateImage
|
5759
|
316 |
"generates the magnifiedImage right away
|
|
317 |
|
5742
|
318 |
self new
|
|
319 |
script:(GnuplotGraphView defaultScript);
|
|
320 |
data:(RandomGenerator new next:50);
|
|
321 |
open
|
|
322 |
"
|
5750
|
323 |
|scriptUsed command tmpDir outStream errorStream statusCode ok
|
5742
|
324 |
dataFilename scriptFilename outFilename argsDict expandedScript
|
|
325 |
stdout stderr|
|
|
326 |
|
5750
|
327 |
(scriptUsed := script) isNil ifTrue:[
|
5754
|
328 |
scriptUsed := self defaultScript.
|
5750
|
329 |
].
|
5742
|
330 |
|
|
331 |
command := 'gnuplot "%1"'.
|
|
332 |
argsDict := Dictionary new.
|
|
333 |
|
|
334 |
tmpDir := Filename tempDirectory.
|
5754
|
335 |
[
|
|
336 |
data notEmptyOrNil ifTrue:[
|
|
337 |
dataFilename := self generateDataFileIn:tmpDir.
|
|
338 |
] ifFalse:[
|
|
339 |
dataFilename := nil.
|
5742
|
340 |
].
|
|
341 |
|
5754
|
342 |
outFilename := (Filename newTemporaryIn:tmpDir) withSuffix:'png'.
|
|
343 |
|
|
344 |
argsDict at:'data' put:(dataFilename isNil ifTrue:[''] ifFalse:[dataFilename baseName]).
|
|
345 |
argsDict at:'dataSize' put:(data size).
|
|
346 |
argsDict at:'outputFile' put:(outFilename baseName).
|
|
347 |
argsDict at:'outputFormat' put:'png'.
|
|
348 |
argsDict at:'title' put:(title ? '').
|
5759
|
349 |
((adjustHolder value ? '') includesString:'fit') not
|
|
350 |
ifTrue:[
|
|
351 |
argsDict at:'width' put:(640 * (self magnificationFactor)).
|
|
352 |
argsDict at:'height' put:(400 * (self magnificationFactor)).
|
|
353 |
] ifFalse:[
|
|
354 |
argsDict at:'width' put:self width.
|
|
355 |
argsDict at:'height' put:self height.
|
|
356 |
].
|
5754
|
357 |
expandedScript := scriptUsed bindWithArguments:argsDict.
|
|
358 |
scriptFilename := (Filename newTemporaryIn:tmpDir).
|
|
359 |
scriptFilename := scriptFilename asFilename withSuffix:'gnuplot'.
|
|
360 |
scriptFilename contents:expandedScript.
|
|
361 |
|
|
362 |
command := command bindWith:(scriptFilename baseName).
|
|
363 |
|
|
364 |
outStream := '' writeStream.
|
|
365 |
errorStream := '' writeStream.
|
|
366 |
|
|
367 |
ok := OperatingSystem
|
|
368 |
executeCommand:command
|
|
369 |
inputFrom:Stdin
|
|
370 |
outputTo:outStream
|
|
371 |
errorTo:errorStream
|
|
372 |
environment:nil
|
|
373 |
inDirectory:tmpDir
|
|
374 |
lineWise:true
|
|
375 |
showWindow:false
|
|
376 |
onError:[:status |
|
|
377 |
statusCode := status code.
|
|
378 |
ok := false.
|
|
379 |
].
|
|
380 |
|
|
381 |
ok ifFalse:[
|
|
382 |
(stderr := errorStream contents) notEmptyOrNil ifTrue:[
|
|
383 |
Transcript showCR:(stderr withColor:Color red).
|
|
384 |
].
|
|
385 |
] ifTrue:[
|
|
386 |
(stdout := outStream contents) notEmpty ifTrue:[
|
|
387 |
Transcript showCR:stdout.
|
|
388 |
].
|
|
389 |
image := Image fromFile:outFilename.
|
5742
|
390 |
].
|
5754
|
391 |
] ensure:[
|
5765
|
392 |
dataFilename notNil ifTrue:[ dataFilename remove ].
|
|
393 |
outFilename notNil ifTrue:[ outFilename remove ].
|
|
394 |
scriptFilename notNil ifTrue:[ scriptFilename remove ].
|
5742
|
395 |
].
|
|
396 |
!
|
|
397 |
|
|
398 |
generateMagnifiedImage
|
|
399 |
image isNil ifTrue:[
|
|
400 |
self generateImage
|
|
401 |
].
|
5759
|
402 |
magnifiedImage := smoothMagnifiedImage := image.
|
|
403 |
"/ super generateMagnifiedImage.
|
5750
|
404 |
!
|
|
405 |
|
|
406 |
redrawX:x y:y width:w height:h
|
|
407 |
(OperatingSystem canExecuteCommand:'gnuplot') ifFalse:[
|
|
408 |
self clearRectangleX:x y:y width:w height:h.
|
|
409 |
self showWarning.
|
|
410 |
^ self.
|
|
411 |
].
|
|
412 |
super redrawX:x y:y width:w height:h.
|
|
413 |
!
|
|
414 |
|
|
415 |
showWarning
|
|
416 |
|infoMsg wString|
|
|
417 |
|
|
418 |
infoMsg := resources string:'No gnuplot command found'.
|
|
419 |
wString := gc font widthOf:infoMsg.
|
|
420 |
gc displayString:infoMsg x:(self width - wString) // 2 y:(self height // 2).
|
|
421 |
!
|
|
422 |
|
|
423 |
sizeChanged:how
|
|
424 |
super sizeChanged:how.
|
|
425 |
self invalidate.
|
5759
|
426 |
!
|
|
427 |
|
|
428 |
updateImageAfterSizeChange
|
|
429 |
image := nil.
|
|
430 |
super updateImageAfterSizeChange
|
|
431 |
! !
|
|
432 |
|
|
433 |
!GnuplotGraphView methodsFor:'menu'!
|
|
434 |
|
|
435 |
smoothFitBigMenuItemVisible
|
|
436 |
^ false
|
|
437 |
!
|
|
438 |
|
|
439 |
smoothingMenuItemVisible
|
|
440 |
^ false
|
5742
|
441 |
! !
|
|
442 |
|
5754
|
443 |
!GnuplotGraphView methodsFor:'menu actions'!
|
|
444 |
|
|
445 |
formatChanged
|
|
446 |
image := magnifiedImage := smoothMagnifiedImage := nil.
|
|
447 |
self invalidate
|
|
448 |
! !
|
|
449 |
|
5755
|
450 |
!GnuplotGraphView methodsFor:'mvc'!
|
|
451 |
|
|
452 |
updateFromModel
|
|
453 |
"the model changes, set my image"
|
|
454 |
|
|
455 |
self data:model value.
|
|
456 |
! !
|
|
457 |
|
5742
|
458 |
!GnuplotGraphView class methodsFor:'documentation'!
|
|
459 |
|
|
460 |
version
|
|
461 |
^ '$Header$'
|
|
462 |
!
|
|
463 |
|
|
464 |
version_CVS
|
|
465 |
^ '$Header$'
|
|
466 |
! !
|
|
467 |
|