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