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