forked from dbmcclain/LispPlotter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotter-drawing.lisp
692 lines (673 loc) · 26.8 KB
/
plotter-drawing.lisp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
(in-package :plotter)
(defun draw-path (port &rest positions)
(gp:draw-polygon
port (mapcan #'append positions)))
#| IMPLEMENT -or- DELETE
(defun zip (&rest seqs)
(apply #'map 'list #'list seqs))
(defun staircase (xv yv)
(let ((pairs (zip xv yv)))
(um:foldl
(lambda (ans pair)
(destructuring-bind (x y) pair
(destructuring-bind (xprev yprev &rest _) ans
(declare (ignore _))
(let ((xmid (* 0.5 (+ x xprev))))
(nconc (list x y xmid y xmid yprev) ans)
))))
(first pairs)
(rest pairs)
)))
(defun make-bars (xv yv)
(let ((pairs (zip xv yv)))
(um:foldl
(lambda (ans pair)
(destructuring-bind (x y) pair
(destructuring-bind (xprev &rest _) ans
(declare (ignore _))
(let ((xmid (* 0.5 (+ x xprev))))
(nconc (list x y xmid y) ans)
))))
(first pairs)
(rest pairs)
)))
(defun interleave (&rest seqs)
(mapcan #'nconc (apply #'zip seqs)))
|#
(defmethod draw-vertical-bars (port (bars <pair-scanner>))
(let* (xprev
yprev
last-x
;; default if only one data point
(wd (* 0.1 (gp:port-width port)))
(wd/2 (* 0.5 wd)))
(loop
for pair = (next-item bars)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(when xprev
(setf
wd (abs (- x xprev))
wd/2 (* 0.5 wd))
(unless (= y yprev)
(let ((next-x (+ xprev wd/2))
(prev-x (or last-x (- xprev wd/2))))
(gp:draw-rectangle
port prev-x 0 (- next-x prev-x) yprev :filled t)
(setf last-x next-x))))
(setf xprev x yprev y))
finally
;; use the last known width
(when xprev
(let ((next-x (+ xprev wd/2))
(prev-x (or last-x (- xprev wd/2))))
(gp:draw-rectangle
port prev-x 0 (- next-x prev-x) yprev :filled t))))))
(defmethod draw-horizontal-bars (port (bars <pair-scanner>))
(let* (xprev
yprev
last-y
;; default if only one data point
(wd (* 0.1 (gp:port-height port)))
(wd/2 (* 0.5 wd)))
(loop
for pair = (next-item bars)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(when yprev
(setf
wd (abs (- y yprev))
wd/2 (* 0.5 wd))
(unless (= x xprev)
(let ((next-y (+ yprev wd/2))
(prev-y (or last-y (- yprev wd/2))))
(gp:draw-rectangle
port 0 prev-y xprev (- next-y prev-y) :filled t)
(setf last-y next-y))))
(setf
xprev x
yprev y))
finally
;; use the last known width
(when xprev
(let ((next-y (+ yprev wd/2))
(prev-y (or last-y (- yprev wd/2))))
(gp:draw-rectangle
port 0 prev-y xprev (- next-y prev-y) :filled t))))))
(defmethod draw-staircase (port (pairs <pair-scanner>))
(let* (xprev
yprev
last-x
;; default for only one data point
(wd (* 0.1 (gp:port-width port)))
(wd/2 (* 0.5 wd)))
(loop
for pair = (next-item pairs)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(when xprev
(setf
wd (abs (- x xprev))
wd/2 (* 0.5 wd))
(unless (= y yprev)
(let ((next-x (- x wd/2)))
(gp:draw-polygon
port
(list
(or last-x (- xprev wd/2))
yprev next-x yprev next-x y)
:closed nil)
(setf last-x next-x))))
(setf
xprev x
yprev y))
finally
(when xprev
(gp:draw-line
port
(or last-x (- xprev wd/2)) yprev
(+ xprev wd/2) yprev)))))
(defmethod draw-polyline (port (pairs <pair-scanner>))
(let (xprev yprev)
(loop
for pair = (next-item pairs)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(when (and (simple-real-number x) (simple-real-number y))
(when (and xprev (or (/= x xprev) (/= y yprev)))
(gp:draw-line port xprev yprev x y))
(setf
xprev x
yprev y))))))
(defun get-symbol-plotfn (port sf symbol-style)
(labels ((draw-symbol (fn)
#+:COCOA
(with-color (port
(or
(when (fill-color symbol-style)
(adjust-color
port
(fill-color symbol-style)
(fill-alpha symbol-style)))
#.(color:make-gray 1.0 0.25)))
(funcall fn t))
#+:WIN32
(when (fill-color symbol-style)
(with-color (port (adjust-color
port
(fill-color symbol-style)
(fill-alpha symbol-style)))
(funcall fn t)))
(gp:with-graphics-state (port
:thickness
(adjust-linewidth
(* sf (border-thick symbol-style)))
:foreground
(adjust-color
port
(border-color symbol-style)
(border-alpha symbol-style)))
(funcall fn))))
(ecase (plot-symbol symbol-style)
(:cross
(lambda (x y)
(gp:with-graphics-state (port
:thickness
(adjust-linewidth
(* sf (border-thick symbol-style)))
:foreground
(adjust-color
port
(border-color symbol-style)
(border-alpha symbol-style)))
(gp:draw-line port (- x 3) y (+ x 3) y)
(gp:draw-line port x (- y 3) x (+ y 3)))))
(:x
(lambda (x y)
(gp:with-graphics-state (port
:thickness
(adjust-linewidth
(* sf (border-thick symbol-style)))
:foreground
(adjust-color
port
(border-color symbol-style)
(border-alpha symbol-style)))
(gp:draw-line port (- x 3) (- y 3) (+ x 3) (+ y 3))
(gp:draw-line port (+ x 3) (- y 3) (- x 3) (+ y 3)))))
((:circle :sampled-data)
(lambda (x y)
(labels ((draw-circle (&optional filled)
(gp:draw-circle
port x #+:COCOA (- y 0.5) #+:WIN32 y 3
:filled filled)))
(draw-symbol #'draw-circle))))
((:box :square)
(lambda (x y)
(labels ((draw-rectangle (&optional filled)
(gp:draw-rectangle
port (- x 3) (- y 3) 6 6 :filled filled)))
(draw-symbol #'draw-rectangle))))
((:triangle :up-triangle)
(lambda (x y)
(labels ((draw-triangle (&optional filled)
(gp:draw-polygon
port
(list (- x 3) (+ y 3)
x (- y 4)
(+ x 3) (+ y 3))
:closed t
:filled filled)))
(draw-symbol #'draw-triangle))))
(:down-triangle
(lambda (x y)
(labels ((draw-triangle (&optional filled)
(gp:draw-polygon
port
(list (- x 3) (- y 3)
x (+ y 4)
(+ x 3) (- y 3))
:closed t
:filled filled)))
(draw-symbol #'draw-triangle))))
(:right-triangle
(lambda (x y)
(labels ((draw-triangle (&optional filled)
(gp:draw-polygon
port
(list (- x 3) (- y 3)
(+ x 4) y
(- x 3) (+ y 3))
:closed t
:filled filled)))
(draw-symbol #'draw-triangle))))
(:left-triangle
(lambda (x y)
(labels ((draw-triangle (&optional filled)
(gp:draw-polygon
port
(list (+ x 3) (- y 3)
(- x 4) y
(+ x 3) (+ y 3))
:closed t
:filled filled)))
(draw-symbol #'draw-triangle))))
(:dot
(lambda (x y)
(with-color (port (adjust-color
port
(border-color symbol-style)
(border-alpha symbol-style)))
(gp:draw-circle port x (1- y) 0.5)))))))
(defmethod unsafe-pw-plot-xv-yv ((cpw <plotter-mixin>) port xvector yvector
&key
;; (color #.(color:make-rgb 0.0 0.5 0.0))
;; alpha
;; thick
;; (linewidth (or thick 1))
;; linedashing
;; symbol
;; plot-joined
;; legend
legend-x
legend-y
legend-anchor
;; (border-color color)
;; symbol-filled
;; (fill-color color)
;; (border-thick linewidth)
;; barwidth
;; bar-offset
plot-style
&allow-other-keys)
;; this is the base plotting routine
;; called only from within the pane process
(let* ((sf (plotter-sf cpw))
(box
(let ((box (plotter-box cpw)))
(adjust-box
(list (1+ (* sf (box-left box)))
(* sf (box-top box))
(1- (* sf (box-width box)))
(* sf (box-height box))))))
(xform (plotter-xform cpw))
;; (color (adjust-color cpw color alpha))
;; (linewidth (adjust-linewidth (* sf linewidth)))
(line-style (line-style plot-style))
(symbol-style (symbol-style plot-style))
(nel
(if xvector
(min (length-of xvector) (length-of yvector))
(length-of yvector)))
(xs
(let ((scanner (make-scanner (or xvector nel))))
(if (plotter-xlog cpw)
(make-transformer scanner #'log10)
scanner)))
(ys
(let ((scanner (make-scanner yvector)))
(if (plotter-ylog cpw)
(make-transformer scanner #'log10)
scanner)))
(pairs (make-pair-scanner xs ys)))
(when (legend plot-style)
(append-legend cpw plot-style))
(when legend-x
(setf (plotter-legend-x cpw) legend-x))
(when legend-y
(setf (plotter-legend-y cpw) legend-y))
(when legend-anchor
(setf (plotter-legend-anchor cpw) legend-anchor))
(gp:with-graphics-state (port
;; :thickness linewidth
;; :dashed (not (null linedashing))
;; :dash (mapcar (expanded-curry (v) #'* sf) linedashing)
;; :foreground color
:line-end-style :butt
:line-joint-style :miter
:mask box)
(labels ((draw-lines ()
(gp:with-graphics-state (port
:thickness
(adjust-linewidth
(* sf (line-thick line-style)))
:foreground
(adjust-color
port
(line-color line-style)
(line-alpha line-style))
:dashed
(line-dashing line-style)
:dash
(mapcar
(expanded-curry (v) #'* sf)
(line-dashing line-style)))
(gp:with-graphics-scale (port sf sf)
(gp:with-graphics-transform (port xform)
(draw-polyline port pairs))))))
(cond
(symbol-style
(case (plot-symbol symbol-style)
(:vbars
(gp:with-graphics-state (port
:foreground
(adjust-color
port
(fill-color symbol-style)
(fill-alpha symbol-style)))
(gp:with-graphics-scale (port sf sf)
(if (bar-width symbol-style)
(let* ((wd (get-x-width cpw (bar-width symbol-style)))
(wd/2 (* 0.5 wd))
(off
(if (bar-offset symbol-style)
(get-x-width cpw (bar-offset symbol-style))
0)))
(loop
for pair = (next-item pairs)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(multiple-value-bind (xx yy)
(gp:transform-point xform x y)
(multiple-value-bind (_ yy0)
(gp:transform-point xform x 0)
(declare (ignore _))
(gp:draw-rectangle
port
(+ off (- xx wd/2)) yy0 wd (- yy yy0)
:filled t))))))
(gp:with-graphics-transform (port xform)
(draw-vertical-bars port pairs))))))
(:hbars
(gp:with-graphics-state (port
:foreground
(adjust-color
port
(fill-color symbol-style)
(fill-alpha symbol-style)))
(gp:with-graphics-scale (port sf sf)
(if (bar-width symbol-style)
(let* ((wd (get-y-width cpw (bar-width symbol-style)))
(wd/2 (* 0.5 wd))
(off
(if (bar-offset symbol-style)
(get-y-width cpw (bar-offset symbol-style))
0)))
(loop
for pair = (next-item pairs)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(multiple-value-bind (xx yy)
(gp:transform-point xform x y)
(multiple-value-bind (xx0 _)
(gp:transform-point xform 0 y)
(declare (ignore _))
(gp:draw-rectangle
port
xx0 (+ off (- yy wd/2))
(- xx xx0) wd
:filled t))))))
(gp:with-graphics-transform (port xform)
(draw-horizontal-bars port pairs))))))
(:sampled-data
(gp:with-graphics-state (port
:foreground
(adjust-color
port
(or
(and line-style (line-color line-style))
:black)
(or
(and line-style (line-alpha line-style))
1))
:thickness
(adjust-linewidth
(* sf (or (and line-style (line-thick line-style)) 1))))
(gp:with-graphics-scale (port sf sf)
(let ((dotfn (get-symbol-plotfn port sf (symbol-style plot-style))))
(loop
for pair = (next-item pairs)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(multiple-value-bind (xx yy)
(gp:transform-point xform x y)
(multiple-value-bind (_ yy0)
(gp:transform-point xform x 0)
(declare (ignore _))
(gp:draw-line port xx yy0 xx yy)
(funcall dotfn xx yy)))))))))
(otherwise
(when line-style
(draw-lines)
(reset-scanner pairs))
(gp:with-graphics-scale (port sf sf)
(let ((plotfn (get-symbol-plotfn port sf symbol-style)))
(loop
for pair = (next-item pairs)
while pair do
(let ((x (aref pair 0))
(y (aref pair 1)))
(multiple-value-bind (xx yy)
(gp:transform-point xform x y)
(funcall plotfn xx yy)))))))))
(line-style
(case (line-type line-style)
(:stepped
(gp:with-graphics-state (port
:thickness
(adjust-linewidth
(* sf (line-thick line-style)))
:foreground
(adjust-color
port
(line-color line-style)
(line-alpha line-style)))
(gp:with-graphics-scale (port sf sf)
(gp:with-graphics-transform (port xform)
(draw-staircase port pairs)))))
;; :interpolated
(otherwise (draw-lines)))))))))
(defmethod pw-plot-xv-yv ((cpw <plotter-mixin>) port xvector yvector &rest args)
(ignore-errors
(apply #'unsafe-pw-plot-xv-yv cpw port xvector yvector args)))
;;; bear in mind that the y values at this point are absolute screen
;;; coords and are inverted with respect to data ordering
(defun get-bar-symbol-plotfn (port symbol color neg-color bar-width testfn)
(ecase symbol
(:sigma
(lambda (x ys)
(let ((ymin (aref ys 0))
(ymax (aref ys 1)))
(gp:draw-line port x ymin x ymax)
(gp:draw-line port (- x (/ bar-width 2)) ymin (+ x (/ bar-width 2)) ymin)
(gp:draw-line port (- x (/ bar-width 2)) ymax (+ x (/ bar-width 2)) ymax))))
(:hl-bar
(lambda (x ys)
(let ((ymin (aref ys 0))
(ymax (aref ys 1)))
(gp:draw-line port x ymin x ymax))))
(:hlc-bar
(lambda (x ys)
(let ((h (aref ys 0))
(l (aref ys 1))
(c (aref ys 2)))
(gp:draw-line port x l x h)
(gp:draw-line port x c (+ x (/ bar-width 2)) c))))
(:ohlc-bar
(lambda (x ys)
(let ((o (aref ys 0))
(h (aref ys 1))
(l (aref ys 2))
(c (aref ys 3)))
(with-color (port (if (funcall testfn c o) neg-color color))
(gp:draw-line port x l x h)
(gp:draw-line port (- x (/ bar-width 2)) o x o)
(gp:draw-line port x c (+ x (/ bar-width 2)) c)))))
(:candlestick
(lambda (x ys)
(let ((o (aref ys 0))
(h (aref ys 1))
(l (aref ys 2))
(c (aref ys 3)))
(if (funcall testfn c o)
(with-color (port neg-color)
(gp:draw-line port x l x h)
(gp:draw-rectangle port (- x (/ bar-width 2)) o bar-width (- c o)
:filled t))
(progn
(with-color (port :black)
(gp:draw-line port x l x h))
(with-color (port color)
(gp:draw-rectangle
port (- x (/ bar-width 2)) o bar-width (- c o)
:filled t))
(with-color (port :black)
(gp:draw-rectangle
port (- x (/ bar-width 2)) o bar-width (- c o))))))))))
(defmethod unsafe-pw-plot-bars-xv-yv ((cpw <plotter-mixin>) port xvector yvectors
&key
(color #.(color:make-rgb 0.0 0.5 0.0))
(neg-color color)
alpha
thick
(linewidth (or thick 1))
(bar-width 6)
(symbol (ecase (length yvectors)
(2 :sigma)
(3 :hlc-bar)
(4 :ohlc-bar)))
&allow-other-keys)
;; this is the base bar-plotting routine
;; called only from within the pane process
(let* ((sf (plotter-sf cpw))
(box
(let ((box (plotter-box cpw)))
(adjust-box
(list (1+ (* sf (box-left box)))
(* sf (box-top box))
(1- (* sf (box-width box)))
(* sf (box-height box))))))
(xform (plotter-xform cpw))
(color (adjust-color cpw color alpha))
(neg-color (adjust-color cpw neg-color alpha))
(linewidth (adjust-linewidth (* sf linewidth)))
(nel
(let ((nely (reduce #'min (mapcar #'length-of yvectors))))
(if xvector
(min (length-of xvector) nely)
nely)))
(xs
(let* ((xform
(lambda (x)
(gp:transform-point xform x 0)))
(scanner
(make-scanner (or xvector nel) :max-items nel)))
(make-transformer
scanner
(if (plotter-xlog cpw)
(compose xform #'log10)
xform))))
(xform-y
(lambda (y)
(second
(multiple-value-list (gp:transform-point xform 0 y)))))
(ys
(let* ((scanners (mapcar #'make-scanner yvectors)))
(mapcar
(rcurry
#'make-transformer
(if (plotter-ylog cpw)
(compose xform-y #'log10)
xform-y))
scanners)))
(c<o-testfn
(let ((y1 (funcall xform-y 0))
(y2 (funcall xform-y 1)))
(if (< y2 y1) #'> #'<)))
(plotfn
(get-bar-symbol-plotfn
port symbol color neg-color bar-width c<o-testfn))
(tmp (make-array (length ys))))
(gp:with-graphics-state (port
:thickness linewidth
:foreground color
:line-end-style :butt
:line-joint-style :miter
:mask box)
(gp:with-graphics-scale (port sf sf)
(loop
for x = (next-item xs)
while x do
(map-into tmp #'next-item ys)
(funcall plotfn x tmp))))))
(defmethod pw-plot-bars-xv-yv ((cpw <plotter-mixin>) port xvector yvectors &rest args)
(ignore-errors
(apply #'unsafe-pw-plot-bars-xv-yv cpw port xvector yvectors args)))
(defun plt-draw-shape (pane port shape x0 y0 x1 y1
&key
color alpha filled
border-thick border-color border-alpha
start-angle sweep-angle)
;; for rectangles: shape = :rect, (x0,y0) and (x1,y1) are opposite corners
;; for ellipses: shape = :ellipse, (x0,y0) is ctr (x1,y1) are radii
(let* ((x0 (get-x-for-location pane (get-x-location pane x0)))
(y0 (get-y-for-location pane (get-y-location pane y0)))
(x1 (get-x-for-location pane (get-x-location pane x1)))
(y1 (get-y-for-location pane (get-y-location pane y1)))
(x0 (if (plotter-xlog pane) (log10 x0) x0))
(x1 (if (plotter-xlog pane) (log10 x1) x1))
(y0 (if (plotter-ylog pane) (log10 y0) y0))
(y1 (if (plotter-ylog pane) (log10 y1) y1))
(wd (- x1 x0))
(ht (- y1 y0))
(sf (plotter-sf pane))
(box
(let ((box (plotter-box pane)))
(adjust-box
(list (1+ (* sf (box-left box)))
(* sf (box-top box))
(1- (* sf (box-width box)))
(* sf (box-height box))))))
(xform (plotter-xform pane))
(color (adjust-color pane color alpha))
(bcolor (adjust-color pane border-color border-alpha))
(linewidth (adjust-linewidth (* sf (or border-thick 0)))))
(gp:with-graphics-state (port
:thickness linewidth
:foreground color
:line-end-style :butt
:line-joint-style :miter
:mask box)
(gp:with-graphics-scale (port sf sf)
(gp:with-graphics-transform (port xform)
(when filled
(ecase shape
(:rect
(gp:draw-rectangle
port x0 y0 wd ht :filled t))
(:ellipse
(gp:draw-ellipse
port x0 y0 x1 y1 :filled t))
(:arc
(gp:draw-arc
port x0 y0 wd ht start-angle sweep-angle :filled t))))
(when border-thick
(with-color (port bcolor)
(case shape
(:rect
(gp:draw-rectangle
port x0 y0 wd ht :filled nil))
(:ellipse
(gp:draw-ellipse
port x0 y0 x1 y1 :filled nil))
(:arc
(gp:draw-arc
port x0 y0 wd ht start-angle sweep-angle
:filled nil))))))))))