-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplotting.lisp
More file actions
378 lines (351 loc) · 12.6 KB
/
plotting.lisp
File metadata and controls
378 lines (351 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
(in-package :plt)
;; ----------------------------------
(defclass colorpatch (capi:drawn-pinboard-object)
((cp-color :accessor cp-color :initarg :color :initform :black))
(:default-initargs
:display-callback 'draw-colorpatch
:x 0
:y 0
:width 32
:height 16))
(defmethod draw-colorpatch (pane (self colorpatch) x y width height)
(gp:draw-rectangle pane x y width height
:foreground (cp-color self)
:filled t)
(gp:draw-rectangle pane x y width height
:foreground :black))
;; ----------------------------------
(capi:define-interface colorpatch-text-input-pane ()
()
(:panes
(patch colorpatch
:color :black
:accessor patch)
(textpane capi:text-input-pane
:text ":BLACK"
:completion-function 'color-completion-callback
:buttons '(:ok nil
:completion t)
:accessor textpane)
)
(:layouts
(colorpatch-layout capi:pinboard-layout
'(patch)
:visible-min-width 33
:visible-max-width 33
:visible-min-height 17
:visible-max-height 17)
(main-layout capi:row-layout
'(colorpatch-layout
textpane)
:adjust :center))
(:default-initargs
:layout 'main-layout
:title "ColorPatch Selector"))
(defun color-completion-callback (widget data)
(multiple-value-bind (new-color success)
(capi:prompt-for-color
"Choose a color"
:color (read-from-string data nil :black))
(if success
(let ((new-text (format nil "~S" new-color))
(cp (patch (capi:element-interface widget))))
(setf (cp-color cp) new-color)
(capi:redisplay-element (capi:pinboard-object-pinboard cp))
new-text)
data)))
(defmethod set-colorpatch-color ((widget colorpatch-text-input-pane) color)
(setf (cp-color (patch widget)) color
(capi:text-input-pane-text (textpane widget)) (format nil "~S" color)))
(defmethod get-colorpatch-color ((widget colorpatch-text-input-pane))
(values (cp-color (patch widget))
(capi:text-input-pane-text (textpane widget))))
;; ----------------------------------------------
(defvar *plot-style*
'(:thick 2))
(defvar *plot-axis-properties*
'(:plot-fn plt::sinc
:domain (0 1)))
(capi:define-interface axis-property-sheet ()
()
(:panes
(plot-title capi:text-input-pane
:title "Plot Title"
:text "A Plot of Y vs X"
:accessor plot-title)
#|(domain capi:text-input-pane
:title "Domain (min,max)"
:text "(0 1)"
:accessor domain)|#
(plot-fn capi:text-input-pane
:title "Function"
:text "plt:sinc"
:accessor plot-fn)
(x-title capi:text-input-pane
:title "Title"
:text "X Values"
:visible-min-width 200
:accessor x-title)
(y-title capi:text-input-pane
:title "Title"
:text "Y Values"
:visible-min-width 200
:accessor y-title)
(x-log capi:radio-button-panel
:title "Style"
:items '(:linear :log)
:print-function 'string-capitalize
:layout-class 'capi:row-layout
:accessor x-log)
(y-log capi:radio-button-panel
:title "Style"
:items '(:linear :log)
:print-function 'string-capitalize
:layout-class 'capi:row-layout
:accessor y-log)
#|(x-scale capi:text-input-pane
:title "Scaling"
:text "1.0"
:accessor x-scale)|#
(x-min capi:text-input-pane
:title "Min"
:accessor x-min)
(x-max capi:text-input-pane
:title "Max"
:accessor x-max)
(y-min capi:text-input-pane
:title "Min"
:accessor y-min)
(y-max capi:text-input-pane
:title "Max"
:accessor y-max)
#|(y-scale capi:text-input-pane
:title "Scaling"
:text "1.0"
:accessor y-scale)|#
(bg-color colorpatch-text-input-pane
:title "Background"
:accessor bg-color)
(fg-color colorpatch-text-input-pane
:title "Foreground"
:accessor fg-color)
(ok-button capi:push-button
:text "OK"
:default-p t
:visible-min-width 100
:callback 'exit-axis-dialog)
(cancel-button capi:push-button
:text "Cancel"
:visible-min-width 100
:callback (lambda (item intf)
(declare (ignore item))
(capi:destroy intf)))
(style capi:text-input-pane
:title "Plot style"
:text "(:thick 2)"
:accessor style)
(grid capi:check-button
:title "Grid"
:text ""
:accessor grid)
)
(:layouts
(bgfg-row
capi:row-layout
'(bg-color
fg-color)
:background :gray30
)
(x-minmax
capi:row-layout
'(x-min x-max))
(y-minmax
capi:row-layout
'(y-min y-max))
(x-logscale
capi:row-layout
'(x-log #|x-scale|#))
(y-logscale
capi:row-layout
'(y-log #|y-scale|#))
(x-column
capi:column-layout
'(x-title x-logscale x-minmax)
:background :gray30
:title "X Axis")
(y-column
capi:column-layout
'(y-title y-logscale y-minmax)
:background :gray30
:title "Y Axis")
(title-row
capi:column-layout
'(plot-title #|domain|# plot-fn style
grid))
(filler1
capi:column-layout
())
(button-row
capi:row-layout
'(filler1 ok-button cancel-button))
(axis-row
capi:row-layout
'(x-column y-column))
(main-layout
capi:column-layout
'(title-row
bgfg-row
axis-row
button-row))
)
(:default-initargs
:layout 'main-layout
:title "Function Plotting"
))
(defun make-axis-property-dialog (&key
xlog
ylog
xrange
yrange
(xtitle "X Values")
(ytitle "Y Values")
(title "A Plot of Y vs X")
(background :white)
(foreground :black)
(fullgrid t)
(plot-fn 'plt::sinc)
;; (domain '(0 1))
;; continuation
&allow-other-keys)
(let ((intf (make-instance 'axis-property-sheet)))
(setf (capi:choice-selected-item (x-log intf)) (if xlog :log :linear)
(capi:choice-selected-item (y-log intf)) (if ylog :log :linear)
(capi:text-input-pane-text (plot-title intf)) title
(capi:text-input-pane-text (x-title intf)) xtitle
(capi:text-input-pane-text (y-title intf)) ytitle
(capi:text-input-pane-text (x-min intf)) (if xrange
(format nil "~A" (elt xrange 0))
"")
(capi:text-input-pane-text (x-max intf)) (if xrange
(format nil "~A" (elt xrange 1))
"")
(capi:text-input-pane-text (y-min intf)) (if yrange
(format nil "~A" (elt yrange 0))
"")
(capi:text-input-pane-text (y-max intf)) (if yrange
(format nil "~A" (elt yrange 1))
"")
(capi:text-input-pane-text (plot-fn intf)) (format nil "~S" plot-fn)
;; (capi:text-input-pane-text (domain intf)) (format nil "~S" domain)
(capi:text-input-pane-text (style intf)) (format nil "~S" *plot-style*)
(capi:button-selected (grid intf)) fullgrid
)
(set-colorpatch-color (bg-color intf) background)
(set-colorpatch-color (fg-color intf) foreground)
(capi:contain intf)
;; (capi:display-dialog intf :continuation continuation)
))
(defun read-text-pane-text (widget)
(read-from-string (capi:text-input-pane-text widget) nil nil))
(defun eval-text-pane-text (widget)
;; (gdsp::gz (capi:text-input-pane-text widget))
(read-text-pane-text widget))
(defun exit-axis-dialog (item intf)
(declare (ignore item))
(catch :gz-error
(let ((xmin (eval-text-pane-text (x-min intf)))
(xmax (eval-text-pane-text (x-max intf)))
(ymin (eval-text-pane-text (y-min intf)))
(ymax (eval-text-pane-text (y-max intf)))
#|(domain (eval-text-pane-text (domain intf)))|#)
(unless xmin
(setf xmin 0))
(unless xmax
(setf xmax 0))
(unless ymin
(setf ymin 0))
(unless ymax
(setf ymax 0))
#|(unless domain
(setf domain '(0 1)))|#
(cond ((not (numberp xmin))
(capi:display-message "X Min not numeric"))
((not (numberp xmax))
(capi:display-message "X Max not numeric"))
((not (numberp ymin))
(capi:display-message "Y Min not numeric"))
((not (numberp ymax))
(capi:display-message "Y Max not numeric"))
#|((or (not (consp domain))
(null (cdr domain))
(not (numberp (first domain)))
(not (numberp (second domain))))
(capi:display-message "Domain should be (min max) number pair"))|#
(t
(let* ((title (capi:text-input-pane-text (plot-title intf)))
(xtitle (capi:text-input-pane-text (x-title intf)))
(ytitle (capi:text-input-pane-text (y-title intf)))
(xlog (eq :log (capi:choice-selected-item (x-log intf))))
(ylog (eq :log (capi:choice-selected-item (y-log intf))))
(bg (get-colorpatch-color (bg-color intf)))
(fg (get-colorpatch-color (fg-color intf)))
(plot-fn (read-text-pane-text (plot-fn intf)))
(props `(:title ,title
:xtitle ,xtitle
:ytitle ,ytitle
:xlog ,xlog
:ylog ,ylog
:xrange ,(unless (= xmin xmax) `(,xmin ,xmax))
:yrange ,(unless (= ymin ymax) `(,ymin ,ymax))
:fullgrid ,(capi:button-selected (grid intf))
:foreground ,fg
:background ,bg
;; :domain ,domain
:plot-fn ,plot-fn)))
;; (capi:exit-dialog props)
(setf *plot-style* (read-text-pane-text (style intf)))
(helpme-plot props)
))
))))
(defun sinc (x)
(if (zerop x)
1.0
(/ (sin (* pi x)) (* pi x))))
(defun helpme-plot (props)
(let ((bg-prev (getf *plot-axis-properties* :background))
(fg-prev (getf *plot-axis-properties* :foreground)))
(unless (and (equalp bg-prev (getf props :background bg-prev))
(equalp fg-prev (getf props :foreground fg-prev)))
(wclose 'plt)))
(setf *plot-axis-properties* props)
(let ((fn (getf props :plot-fn #'identity)))
(apply #'plt:fplot 'plt
;; (getf *plot-axis-properties* :domain)
(let ((xrange (getf props :xrange '(0 1))))
(if (or (null xrange)
(= (elt xrange 0)
(elt xrange 1)))
'(0 1)
xrange))
(if (consp fn)
(compile nil fn)
fn)
:clear t
(append props
*plot-style*))))
(defun helpme ()
(apply #'make-axis-property-dialog *plot-axis-properties*))
#|
(capi:with-dialog-results (val okp)
(apply 'make-axis-property-dialog
:no-y-style-selector t
'(:title "Thingy"
:xtitle "Time [ms]"
:ytitle "Amplitude [dB]"
:xlog t
:ylog nil))
(when okp
(let ((*print-length* nil))
(print val))
))
|#