5750
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5742
|
3 |
"{ Package: 'stx:libwidg2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
ImageView subclass:#GnuplotGraphView
|
|
8 |
instanceVariableNames:'script data title'
|
|
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
|
|
19 |
|
|
20 |
[author:]
|
|
21 |
cg
|
|
22 |
|
|
23 |
[instance variables:]
|
|
24 |
|
|
25 |
[class variables:]
|
|
26 |
|
|
27 |
[see also:]
|
|
28 |
|
|
29 |
"
|
|
30 |
!
|
|
31 |
|
|
32 |
examples
|
|
33 |
"
|
|
34 |
Notice that everything between [exBegin] and [exEnd] is extracted by the html-doc generator
|
|
35 |
to create nicely formatted and clickable executable examples in the generated html-doc.
|
|
36 |
(see the browsers class-documentation menu items for more)
|
|
37 |
|
|
38 |
trying the widget as standAlone view:
|
|
39 |
[exBegin]
|
|
40 |
GnuplotGraphView new
|
|
41 |
script:(GnuplotGraphView defaultScript);
|
|
42 |
model:(ValueHolder with:(Random new next:100));
|
|
43 |
open
|
|
44 |
[exEnd]
|
|
45 |
|
|
46 |
embedded in another view:
|
|
47 |
[exBegin]
|
|
48 |
|top v|
|
|
49 |
|
|
50 |
top := StandardSystemView new.
|
|
51 |
top extent:300@300.
|
|
52 |
v := GnuplotGraphView new.
|
|
53 |
v origin:10@10 corner:150@150.
|
|
54 |
top add:v.
|
|
55 |
top open
|
|
56 |
[exEnd]
|
|
57 |
"
|
|
58 |
! !
|
|
59 |
|
|
60 |
!GnuplotGraphView class methodsFor:'defaults'!
|
|
61 |
|
|
62 |
defaultScript
|
|
63 |
"return a default initial gnuplot script"
|
|
64 |
|
|
65 |
^ self defaultScriptForHistogram
|
|
66 |
!
|
|
67 |
|
|
68 |
defaultScriptForHistogram
|
|
69 |
"a default initial gnuplot script to show a histogram"
|
|
70 |
|
|
71 |
^ '
|
|
72 |
set term %(outputFormat)
|
|
73 |
set output "%(outputFile)"
|
|
74 |
set title "%(title)"
|
|
75 |
plot [-10:110] ''%(data)'' with histogram
|
|
76 |
'
|
|
77 |
! !
|
|
78 |
|
|
79 |
!GnuplotGraphView methodsFor:'accessing'!
|
|
80 |
|
|
81 |
data
|
|
82 |
^ data
|
|
83 |
!
|
|
84 |
|
|
85 |
data:something
|
|
86 |
data := something.
|
|
87 |
!
|
|
88 |
|
|
89 |
script
|
|
90 |
^ script
|
|
91 |
!
|
|
92 |
|
|
93 |
script:something
|
|
94 |
script := something.
|
|
95 |
!
|
|
96 |
|
|
97 |
title
|
|
98 |
^ title
|
|
99 |
!
|
|
100 |
|
|
101 |
title:something
|
|
102 |
title := something.
|
|
103 |
! !
|
|
104 |
|
|
105 |
!GnuplotGraphView methodsFor:'drawing'!
|
|
106 |
|
|
107 |
generateDataFileIn:aDirectory
|
|
108 |
"the format of data expected by gnuplot depends on the type of plot:
|
|
109 |
regular plot: a number of values (each in a separate line)
|
|
110 |
multiCol plots: a number of lines, each containing a row
|
|
111 |
here, handle common situations;
|
|
112 |
however, if the data does not match, the program should have prepared the
|
|
113 |
data into a string and present it as such.
|
|
114 |
Strings will be sent as-is to the file"
|
|
115 |
|
|
116 |
|dataFilename printRow|
|
|
117 |
|
|
118 |
dataFilename := (Filename newTemporaryIn:aDirectory) pathName.
|
|
119 |
dataFilename := dataFilename asFilename withSuffix:'dat'.
|
|
120 |
|
|
121 |
(data isString or:[data isByteArray]) ifTrue:[
|
|
122 |
dataFilename contents:data.
|
|
123 |
^ dataFilename.
|
|
124 |
].
|
|
125 |
|
|
126 |
data isCollection ifFalse:[
|
|
127 |
dataFilename contents:data printString.
|
|
128 |
^ dataFilename.
|
|
129 |
].
|
|
130 |
|
|
131 |
printRow :=
|
|
132 |
[:s :eachValueOrRow |
|
|
133 |
eachValueOrRow isString ifTrue:[
|
|
134 |
s nextPutAll:eachValueOrRow
|
|
135 |
] ifFalse:[
|
|
136 |
eachValueOrRow isAssociation ifTrue:[
|
|
137 |
eachValueOrRow key printOn:s.
|
|
138 |
s space.
|
|
139 |
eachValueOrRow value printOn:s.
|
|
140 |
] ifFalse:[
|
|
141 |
eachValueOrRow isCollection ifTrue:[
|
|
142 |
eachValueOrRow
|
|
143 |
do:[:element | element printOn:s]
|
|
144 |
separatedBy:[ s space]
|
|
145 |
] ifFalse:[
|
|
146 |
eachValueOrRow printOn:s
|
|
147 |
].
|
|
148 |
].
|
|
149 |
].
|
|
150 |
s cr.
|
|
151 |
].
|
|
152 |
|
|
153 |
dataFilename writingFileDo:[:s |
|
|
154 |
data isSequenceable ifTrue:[
|
|
155 |
data do:[:eachValueOrRow |
|
|
156 |
printRow value:s value:eachValueOrRow.
|
|
157 |
].
|
|
158 |
] ifFalse:[
|
|
159 |
data keysAndValuesDo:[:eachKey :eachValueOrRow |
|
|
160 |
eachKey printOn:s.
|
|
161 |
s space.
|
|
162 |
printRow value:s value:eachValueOrRow.
|
|
163 |
].
|
|
164 |
].
|
|
165 |
].
|
|
166 |
^ dataFilename
|
|
167 |
!
|
|
168 |
|
|
169 |
generateImage
|
|
170 |
"
|
|
171 |
self new
|
|
172 |
script:(GnuplotGraphView defaultScript);
|
|
173 |
data:(RandomGenerator new next:50);
|
|
174 |
open
|
|
175 |
"
|
5750
|
176 |
|scriptUsed command tmpDir outStream errorStream statusCode ok
|
5742
|
177 |
dataFilename scriptFilename outFilename argsDict expandedScript
|
|
178 |
stdout stderr|
|
|
179 |
|
5750
|
180 |
(scriptUsed := script) isNil ifTrue:[
|
|
181 |
scriptUsed := self class defaultScript.
|
|
182 |
].
|
5742
|
183 |
|
|
184 |
command := 'gnuplot "%1"'.
|
|
185 |
argsDict := Dictionary new.
|
|
186 |
|
|
187 |
tmpDir := Filename tempDirectory.
|
|
188 |
|
|
189 |
data notEmptyOrNil ifTrue:[
|
|
190 |
dataFilename := self generateDataFileIn:tmpDir.
|
|
191 |
] ifFalse:[
|
|
192 |
dataFilename := ''.
|
|
193 |
].
|
|
194 |
|
|
195 |
outFilename := (Filename newTemporaryIn:tmpDir) withSuffix:'png'.
|
|
196 |
|
|
197 |
argsDict at:'data' put:dataFilename.
|
|
198 |
argsDict at:'dataSize' put:(data size).
|
|
199 |
argsDict at:'outputFile' put:(outFilename pathName).
|
|
200 |
argsDict at:'outputFormat' put:'png'.
|
|
201 |
argsDict at:'title' put:(title ? '').
|
|
202 |
|
5750
|
203 |
expandedScript := scriptUsed bindWithArguments:argsDict.
|
5742
|
204 |
scriptFilename := (Filename newTemporaryIn:tmpDir).
|
|
205 |
scriptFilename := scriptFilename asFilename withSuffix:'gnuplot'.
|
|
206 |
scriptFilename contents:expandedScript.
|
|
207 |
|
|
208 |
command := command bindWith:(scriptFilename pathName).
|
|
209 |
|
|
210 |
outStream := '' writeStream.
|
|
211 |
errorStream := '' writeStream.
|
|
212 |
|
|
213 |
ok := OperatingSystem
|
|
214 |
executeCommand:command
|
|
215 |
inputFrom:Stdin
|
|
216 |
outputTo:outStream
|
|
217 |
errorTo:errorStream
|
|
218 |
environment:nil
|
|
219 |
inDirectory:tmpDir
|
|
220 |
lineWise:true
|
|
221 |
showWindow:false
|
|
222 |
onError:[:status |
|
|
223 |
statusCode := status code.
|
|
224 |
ok := false.
|
|
225 |
].
|
|
226 |
|
|
227 |
ok ifFalse:[
|
|
228 |
(stderr := errorStream contents) notEmptyOrNil ifTrue:[
|
|
229 |
Transcript showCR:(stderr withColor:Color red).
|
|
230 |
].
|
|
231 |
].
|
|
232 |
(stdout := outStream contents) notEmpty ifTrue:[
|
|
233 |
Transcript showCR:stdout.
|
|
234 |
].
|
|
235 |
|
|
236 |
image := Image fromFile:outFilename.
|
|
237 |
!
|
|
238 |
|
|
239 |
generateMagnifiedImage
|
|
240 |
image isNil ifTrue:[
|
|
241 |
self generateImage
|
|
242 |
].
|
|
243 |
super generateMagnifiedImage.
|
5750
|
244 |
!
|
|
245 |
|
|
246 |
redrawX:x y:y width:w height:h
|
|
247 |
(OperatingSystem canExecuteCommand:'gnuplot') ifFalse:[
|
|
248 |
self clearRectangleX:x y:y width:w height:h.
|
|
249 |
self showWarning.
|
|
250 |
^ self.
|
|
251 |
].
|
|
252 |
super redrawX:x y:y width:w height:h.
|
|
253 |
!
|
|
254 |
|
|
255 |
showWarning
|
|
256 |
|infoMsg wString|
|
|
257 |
|
|
258 |
infoMsg := resources string:'No gnuplot command found'.
|
|
259 |
wString := gc font widthOf:infoMsg.
|
|
260 |
gc displayString:infoMsg x:(self width - wString) // 2 y:(self height // 2).
|
|
261 |
!
|
|
262 |
|
|
263 |
sizeChanged:how
|
|
264 |
super sizeChanged:how.
|
|
265 |
self invalidate.
|
5742
|
266 |
! !
|
|
267 |
|
|
268 |
!GnuplotGraphView class methodsFor:'documentation'!
|
|
269 |
|
|
270 |
version
|
|
271 |
^ '$Header$'
|
|
272 |
!
|
|
273 |
|
|
274 |
version_CVS
|
|
275 |
^ '$Header$'
|
|
276 |
! !
|
|
277 |
|