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