Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implement a preliminary version of faceting We can spread the data across multiple renders. Current issues: - theme has a problem with external formatting-items (clim bug?) - resizing and moving charts is broken (ditto) - the number of columns is not configurable at the moment - currently we do two full passes over all layers in a facet
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256: eed0b7585b6ea10714460f8a6d7da1555012845792da33904bd5a36443c69c2f
User & Date: jack 2023-05-17 15:43:15
Context
2023-05-17
16:07
Clear separation of trainining passes (resolves fixme) check-in: 10fe2857c0 user: jack tags: master, trunk
15:43
Implement a preliminary version of faceting We can spread the data across multiple renders. Current issues: - theme has a problem with external formatting-items (clim bug?) - resizing and moving charts is broken (ditto) - the number of columns is not configurable at the moment - currently we do two full passes over all layers in a facet check-in: eed0b7585b user: jack tags: master, trunk
06:58
cosmetic changes check-in: 106fc43687 user: jackdaniel tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Code/dataset-and-aesthetics.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
(in-package #:eu.turtleware.polyclot.implementation)

(defgeneric dataset-head (dataset)
  (:method ((dataset list))
    (car dataset)))

(defgeneric map-dataset-points (fun dataset)
  (:method (fun (dataset list))
    (map nil fun (cdr dataset))))

(defgeneric point-values (point)
  (:method ((point sequence)) point))


(defun group-by (column dataset &key (test #'eql) (key #'identity))

  (let ((result (make-hash-table :test test))

        (index (position column (dataset-head dataset) :key key :test test)))









    (do-points (row dataset)

      (push row (gethash (elt row index) result)))))




;;; Aesthetics are a sequence of pairs (a property list):
;;;
;;;   (AES-1 SRC-1 AES-2 SRC-2 ...)
;;;
;;; Aeshetic is a keyword (i.e :x), and the source is one of:
;;;













>
|
>
|
>
|
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>







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
(in-package #:eu.turtleware.polyclot.implementation)

(defgeneric dataset-head (dataset)
  (:method ((dataset list))
    (car dataset)))

(defgeneric map-dataset-points (fun dataset)
  (:method (fun (dataset list))
    (map nil fun (cdr dataset))))

(defgeneric point-values (point)
  (:method ((point sequence)) point))

;;; Creates a hash table of datasets grouped by a sequence of columns.
(defun group-by (columns dataset &key (test #'eql) (key #'identity))
  (let* ((head (dataset-head dataset))
         (result (make-hash-table :test test))
         (index* (loop for col in columns
                       collect (position col head :key key :test test))))
    (when (null index*)
      (setf (gethash nil result) dataset)
      (return-from group-by result))
    (flet ((make-ht-key-fn (row)
             (lambda (index)
               ;; when the column does not exist then the row "goes in".
               (if (null index)
                   nil
                   (elt row index)))))
      (do-points (row dataset)
        (let ((key (mapcar (make-ht-key-fn row) index*)))
          (push row (gethash key result (list head))))))
    (dohash ((key val) result)
      (setf (gethash key result) (nreverse val)))
    result))

;;; Aesthetics are a sequence of pairs (a property list):
;;;
;;;   (AES-1 SRC-1 AES-2 SRC-2 ...)
;;;
;;; Aeshetic is a keyword (i.e :x), and the source is one of:
;;;
︙︙
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
        with cont = (gensym "CONT")
        with raw-point = (gensym "RAW-POINT")
        with point-values = nil
        with position = nil
        for (aesthetic source) on aesthetics by #'cddr
        when (stringp source) do
          ;; Ignore aesthetics that could not be mapped.
          (setf position (position source head :test #'string=))
          (if position
              (unless point-values
                (setf point-values (gensym "POINT-VALUES")))
              (setf source nil))
        unless (null source)
          appending (list aesthetic (typecase source
                                      (string `(elt ,point-values ,position))
                                      (function `(funcall ,source ,raw-point))
                                      (otherwise source)))
            into lambda-list
        finally
           (return
             (let ((fun (if (null point-values)
                            `(lambda (,cont ,raw-point)

                               (funcall ,cont ,@lambda-list))
                            `(lambda (,cont ,raw-point)
                               (let ((,point-values (point-values ,raw-point)))
                                 (funcall ,cont ,@lambda-list))))))
               (values (compile nil fun) fun)))))

;;; MAYBE currently we access dataset-head and then map points and use







|














>







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
        with cont = (gensym "CONT")
        with raw-point = (gensym "RAW-POINT")
        with point-values = nil
        with position = nil
        for (aesthetic source) on aesthetics by #'cddr
        when (stringp source) do
          ;; Ignore aesthetics that could not be mapped.
          (setf position (position source head :test #'equal))
          (if position
              (unless point-values
                (setf point-values (gensym "POINT-VALUES")))
              (setf source nil))
        unless (null source)
          appending (list aesthetic (typecase source
                                      (string `(elt ,point-values ,position))
                                      (function `(funcall ,source ,raw-point))
                                      (otherwise source)))
            into lambda-list
        finally
           (return
             (let ((fun (if (null point-values)
                            `(lambda (,cont ,raw-point)
                               (declare (ignore ,raw-point))
                               (funcall ,cont ,@lambda-list))
                            `(lambda (,cont ,raw-point)
                               (let ((,point-values (point-values ,raw-point)))
                                 (funcall ,cont ,@lambda-list))))))
               (values (compile nil fun) fun)))))

;;; MAYBE currently we access dataset-head and then map points and use
︙︙

Changes to Code/faceting.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






(in-package #:eu.turtleware.polyclot.implementation)




#+ (or)
(define-class <faceting> ()
  ((scales
    :type (member :fixed :free-x :free-y :free)
    :initarg :scales
    :accessor scales)
   (as-table
    :type boolean
    :initarg :as-table
    :accessor as-table))
  (:default-initargs :scales :fixed

                     :as-table t))




(define-class <facet> ()
  (dataset layers scales coords))

;;;

(define-class <faceting> ()
  (facets))


;;; A single plot (no faceting).
(define-class <faceting-null> (<faceting>)
  ())




;;; "Ribbon" (one variable)
(define-class <faceting-wrap> (<faceting>)
  (var nrow ncol direction))

#+ (or)
(dohash ((key val) (group-by (var facet) dataset))
  (funcall cont val))

;;; "Grid" (two variables)
(define-class <faceting-grid> (<faceting>)
  (var1 var2))






#+ (or)
(dohash ((key dataset-1) (group-by (var1 facet) dataset))
  (dohash ((key dataset-2) (group-by (var2 facet)) dataset-1)

































    (funcall cont val)))








>
>
>
|

<
<
<
<
|
<
<
<
|
>
|
>
>

>
|
<

<
>
|
|
>




>
>
>





|
|
<





>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
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
(in-package #:eu.turtleware.polyclot.implementation)

;;; Faceting may encounter multiple source datasets in the single plot because a
;;; layer may have its own dataset. To avoid grouping repeatedly of the same
;;; dataset we cache results with the source dataset being the key.

(define-class <faceting> ()




  (datasets facets)



  (:default-initargs
   ;; key [dataset] val [ht vars datasets]
   :datasets (make-hash-table)
   ;; key [vars] val [facet]
   :facets (make-hash-table :test #'equal)))

(defun facet-count (faceting)
  (hash-table-count (facets faceting)))



(defgeneric compute-datasets (faceting dataset)
  (:method :around ((faceting <faceting>) dataset)
    (ensure-gethash (dataset (datasets faceting))
        (call-next-method))))

;;; A single plot (no faceting).
(define-class <faceting-null> (<faceting>)
  ())

(defmethod compute-datasets ((faceting <faceting-null>) dataset)
  (group-by nil dataset))

;;; "Ribbon" (one variable)
(define-class <faceting-wrap> (<faceting>)
  (var nrow ncol direction))

(defmethod compute-datasets ((faceting <faceting-wrap>) dataset)
  (group-by (var faceting) dataset :test #'equal))


;;; "Grid" (two variables)
(define-class <faceting-grid> (<faceting>)
  (var1 var2))

(defmethod compute-datasets ((faceting <faceting-grid>) dataset)
  (group-by (list (var1 faceting)
                  (var2 faceting))
            dataset
            :test #'equal))



;;; There may be more than one <facet> per panel - each distinct dataset implies
;;; a separate object for the set of variables. That means that we _can_ have
;;; many incongruent scales on the same render (and that's a good thing!).

(define-class <facet> ()
  (vars dataset scales coords
   %trained-data)
  (:default-initargs :%trained-data (make-hash-table :test #'equal)))

;;; FIXME implement the ordering of facets
(defun compute-facets (plot layer)
  (let* ((dataset (layer-dataset plot layer))
         (faceting (faceting plot))
         (facets-ht (facets faceting))
         (layer-datasets (compute-datasets faceting dataset)))
    (dohash ((vars facet-data) layer-datasets)
      (ensure-gethash (vars facets-ht)
          (<facet> :vars vars
                   :dataset facet-data
                   :scales (scales plot)
                   :coords (coords plot))))
    facets-ht))

(defun trained-dataset (facet layer)
  (gethash (list facet layer) (%trained-data facet)))

(defun (setf trained-dataset) (new-value facet layer)
  (setf (gethash (list facet layer) (%trained-data facet)) new-value))

(defun call-with-facets (cont plot)
  (let ((facets (facets (faceting plot))))
    (dohash ((vars facet) facets)
      (declare (ignore vars))
      (funcall cont facet))))

(defmacro do-facets ((facet plot) &body body)
  (let ((cont (gensym)))
    `(flet ((,cont (,facet) ,@body))
       (declare (dynamic-extent (function ,cont)))
       (call-with-facets (function ,cont) ,plot))))

Changes to Code/layer.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
(in-package #:eu.turtleware.polyclot.implementation)

;;; In ggplot2 layers are described as a combination of the data, aesthetic
;;; mapping, a geometric object, a statistical transformation and a position
;;; adjustment. Layers are usually created with the geom constructor, so we'll
;;; just make the layer a superclass of all geometric objects (pfft).

(define-class <layer> ()
  (aesthetics
   dataset
   statistical-transformation
   position-adjustment
   trained-dataset)
  (:default-initargs :statistical-transformation +stat-identity+
                     :position-adjustment +position-identity+))

(defun layer-aesthetics (plot layer)
  (loop with aesthetics = (append (aesthetics layer) (aesthetics plot))
        for (aes src) on aesthetics by #'cddr
        unless (member aes keys)
          collect aes into keys
          and append (list aes src) into result
        finally (return result)))

(defun layer-dataset (plot layer)
  (or (dataset layer) (dataset plot)))

(defun train-layer (plot layer)
  (let ((dataset (layer-dataset plot layer))
        (aesthetics (layer-aesthetics plot layer))
        (scales (scales plot))
        (stat-tr (statistical-transformation layer))
        (pos-adj (position-adjustment layer)))
    (setf dataset (bind-data aesthetics dataset)
          dataset (scale-data scales dataset)
          dataset (stat-data stat-tr dataset)
          dataset (train-scales scales dataset)
          dataset (map-scales scales dataset)
          dataset (adjust-data pos-adj layer dataset)
          (trained-dataset layer) dataset)))

(defgeneric render-geom (layer coord))


;;; ggplot2 makes a distinction between individual and collective geoms. A
;;; collective geom divides the dataset in groups - group is an explicit
;;; aesthetic, however sometimes groups are implicit.
;;;
;;; The geom type distinction is fluid - i.e a line works on row pairs so it
;;; is somewhere between individual and collective geoms.


(define-class <geom-point> (<layer>) ())

(defmethod render-geom ((g <geom-point>) (c <coord-cartesian>))
  (do-aesthetics ((x y ink line-style) (trained-dataset g))
    (c:draw-point* t x y :ink ink :line-style line-style)))


(define-class <geom-line> (<layer>) ())

(defmethod render-geom ((g <geom-line>) (c <coord-cartesian>))
  (do-points* ((d1 d2) (trained-dataset g))
    (with-aes ((x1 x) (y1 y) ink line-style) d1
      (with-aes ((x2 x) (y2 y)) d2
        (c:draw-line* t x1 y1 x2 y2 :ink ink :line-style line-style)))))


(define-class <geom-bar> (<layer>)
  (bin-width)
  (:default-initargs :statistical-transformation +stat-count+
                     :bin-width 1/10))

(defmethod render-geom ((g <geom-bar>) (c <coord-cartesian>))
  (do-aesthetics ((xrange x y ink) (trained-dataset g))
    (if xrange
        (let ((min (min-pos xrange))
              (max (max-pos xrange))
              (num (print-number y nil)))
          (c:draw-rectangle* t min 0 max y :ink ink)
          (c:draw-text* t num x y :align-x :center :align-y :bottom :text-size :tiny))
        (c:draw-line* t x 0 x y :line-thickness 8 :ink ink))))











|
<














|
|










|

|












|
|





|
|










|
|







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
(in-package #:eu.turtleware.polyclot.implementation)

;;; In ggplot2 layers are described as a combination of the data, aesthetic
;;; mapping, a geometric object, a statistical transformation and a position
;;; adjustment. Layers are usually created with the geom constructor, so we'll
;;; just make the layer a superclass of all geometric objects (pfft).

(define-class <layer> ()
  (aesthetics
   dataset
   statistical-transformation
   position-adjustment)

  (:default-initargs :statistical-transformation +stat-identity+
                     :position-adjustment +position-identity+))

(defun layer-aesthetics (plot layer)
  (loop with aesthetics = (append (aesthetics layer) (aesthetics plot))
        for (aes src) on aesthetics by #'cddr
        unless (member aes keys)
          collect aes into keys
          and append (list aes src) into result
        finally (return result)))

(defun layer-dataset (plot layer)
  (or (dataset layer) (dataset plot)))

(defun train-layer (plot facet layer)
  (let ((dataset (dataset facet))
        (aesthetics (layer-aesthetics plot layer))
        (scales (scales plot))
        (stat-tr (statistical-transformation layer))
        (pos-adj (position-adjustment layer)))
    (setf dataset (bind-data aesthetics dataset)
          dataset (scale-data scales dataset)
          dataset (stat-data stat-tr dataset)
          dataset (train-scales scales dataset)
          dataset (map-scales scales dataset)
          dataset (adjust-data pos-adj layer dataset)
          (trained-dataset facet layer) dataset)))

(defgeneric render-geom (layer coord dataset))


;;; ggplot2 makes a distinction between individual and collective geoms. A
;;; collective geom divides the dataset in groups - group is an explicit
;;; aesthetic, however sometimes groups are implicit.
;;;
;;; The geom type distinction is fluid - i.e a line works on row pairs so it
;;; is somewhere between individual and collective geoms.


(define-class <geom-point> (<layer>) ())

(defmethod render-geom ((g <geom-point>) (c <coord-cartesian>) dataset)
  (do-aesthetics ((x y ink line-style) dataset)
    (c:draw-point* t x y :ink ink :line-style line-style)))


(define-class <geom-line> (<layer>) ())

(defmethod render-geom ((g <geom-line>) (c <coord-cartesian>) dataset)
  (do-points* ((d1 d2) dataset)
    (with-aes ((x1 x) (y1 y) ink line-style) d1
      (with-aes ((x2 x) (y2 y)) d2
        (c:draw-line* t x1 y1 x2 y2 :ink ink :line-style line-style)))))


(define-class <geom-bar> (<layer>)
  (bin-width)
  (:default-initargs :statistical-transformation +stat-count+
                     :bin-width 1/10))

(defmethod render-geom ((g <geom-bar>) (c <coord-cartesian>) dataset)
  (do-aesthetics ((xrange x y ink) dataset)
    (if xrange
        (let ((min (min-pos xrange))
              (max (max-pos xrange))
              (num (print-number y nil)))
          (c:draw-rectangle* t min 0 max y :ink ink)
          (c:draw-text* t num x y :align-x :center :align-y :bottom :text-size :tiny))
        (c:draw-line* t x 0 x y :line-thickness 8 :ink ink))))

Changes to Code/plot.lisp.

︙︙
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

(defvar *plot*)

;;; Drawing a plot requires at least two passes over all layers/datasets:
;;; - perform all statistical transformations
;;; - estabilish a drawing context (based on the above) and draw geoms


(defun train-plot (plot)
  (dolist (layer (layers plot))
    #+dont-retrain-layers 
    (unless (trained-dataset layer)
      ;; good for rendering, bad for mutations
      (train-layer plot layer))
    (train-layer plot layer)))











(defmethod print-object ((plot <polyclot>) stream)
  (print-unreadable-object (plot stream :type t :identity nil)
    (format stream "~s" (title plot))))

(c:define-presentation-method c:present ((plot <polyclot>)
                                         (type <polyclot>)
                                         stream
                                         (view <polyclot-view>)
                                         &rest args)
  (declare (ignore args))
  (with-theme (plot)


    (let ((coords (coords plot))
          (scales (scales plot)))




      (with-coords (coords scales)
        (dolist (layer (layers plot))
          (render-geom layer coords)))
      (render-guides plot coords))))

(defun polyclot (*plot* *standard-output*)
  (train-plot *plot*)
  (c:present *plot* <polyclot>
             :view (<polyclot-view>)
             :sensitive nil
             :allow-sensitive-inferiors t))







>








>
>
>
>
>
>
>
>
>
>











>
>
|
|
>
>
>
>
|
|
|
|


|




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

(defvar *plot*)

;;; Drawing a plot requires at least two passes over all layers/datasets:
;;; - perform all statistical transformations
;;; - estabilish a drawing context (based on the above) and draw geoms

#+ (or)
(defun train-plot (plot)
  (dolist (layer (layers plot))
    #+dont-retrain-layers 
    (unless (trained-dataset layer)
      ;; good for rendering, bad for mutations
      (train-layer plot layer))
    (train-layer plot layer)))

(defun train-facets (plot)
  (dolist (layer (layers plot))
    (compute-facets plot layer)
    ;; FIXME first we need to train scales on all factes/layers and _after_ that
    ;; map these scales onto layers. Here we cheat by doing twice as much work.
    (do-facets (facet plot)
      (train-layer plot facet layer))
    (do-facets (facet plot)
      (train-layer plot facet layer))))

(defmethod print-object ((plot <polyclot>) stream)
  (print-unreadable-object (plot stream :type t :identity nil)
    (format stream "~s" (title plot))))

(c:define-presentation-method c:present ((plot <polyclot>)
                                         (type <polyclot>)
                                         stream
                                         (view <polyclot-view>)
                                         &rest args)
  (declare (ignore args))
  (with-theme (plot)
    (c:formatting-item-list (*standard-output* :x-spacing 20 :y-spacing 10 :n-columns 2)
      (do-facets (facet plot)
        (let ((coords (coords facet))
              (scales (scales facet)))
          (c:formatting-cell (*standard-output*)
            (c:draw-rectangle* *standard-output* 0 0 1 1
                               :ink c:+grey+
                               :filled nil)
            (with-coords (coords scales)
              (dolist (layer (layers plot))
                (render-geom layer coords (trained-dataset facet layer))))
            (render-guides plot coords)))))))

(defun polyclot (*plot* *standard-output*)
  (train-facets *plot*)
  (c:present *plot* <polyclot>
             :view (<polyclot-view>)
             :sensitive nil
             :allow-sensitive-inferiors t))

Changes to Code/theme.lisp.

︙︙
43
44
45
46
47
48
49
50
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
;;; box [0 0 1 1] and adds the plot decorations.
(defmethod call-with-theme ((theme <theme>) cont)
  (c:surrounding-output-with-border (t :filled t :ink c:+white+ :outline-ink c:+deep-sky-blue+)
    (when (climi::null-bounding-rectangle-p theme)
      (multiple-value-bind (cx cy) (c:stream-cursor-position *standard-output*)
        (setf (c:rectangle-edges* theme)
              (values cx cy (+ cx (width theme)) (+ cy (height theme))))))
    (c:with-bounding-rectangle* (x0 y0 :width width :height height) theme
      (c:draw-design *standard-output* theme :filled nil :ink c:+grey+)

      
      (c:with-translation (t x0 y0)
        (c:with-scaling (t width height)
          (funcall cont)
          #+simulate-faceting
          (loop for i from 0 upto 15
                for col = (mod i 4)
                for row = (truncate i 4)
                do (c:with-scaling (*standard-output* 1/4 1/4)
                     (c:with-translation (*standard-output* col row)
                       (funcall cont)))))))))

(defmacro with-theme ((theme) &body body)
  `(call-with-theme ,theme (lambda () ,@body)))







|
|
>



<
<
<
<
<
<
<
|



43
44
45
46
47
48
49
50
51
52
53
54
55







56
57
58
59
;;; box [0 0 1 1] and adds the plot decorations.
(defmethod call-with-theme ((theme <theme>) cont)
  (c:surrounding-output-with-border (t :filled t :ink c:+white+ :outline-ink c:+deep-sky-blue+)
    (when (climi::null-bounding-rectangle-p theme)
      (multiple-value-bind (cx cy) (c:stream-cursor-position *standard-output*)
        (setf (c:rectangle-edges* theme)
              (values cx cy (+ cx (width theme)) (+ cy (height theme))))))
    ;; for debugging
    (c:draw-design *standard-output* theme :filled nil :ink c:+deep-pink+ :line-dashes t)
    (c:with-bounding-rectangle* (x0 y0 :width width :height height) theme
      
      (c:with-translation (t x0 y0)
        (c:with-scaling (t width height)







          (funcall cont))))))

(defmacro with-theme ((theme) &body body)
  `(call-with-theme ,theme (lambda () ,@body)))

Changes to Code/utilities.lisp.

︙︙
136
137
138
139
140
141
142








            (lambda (p)
              (shiftf ,@vars p)
              (unless ,last-var
                (return-from ,blk ,result))
              (when ,first-var
                (,fun)))
            ,dataset))))))















>
>
>
>
>
>
>
>
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
            (lambda (p)
              (shiftf ,@vars p)
              (unless ,last-var
                (return-from ,blk ,result))
              (when ,first-var
                (,fun)))
            ,dataset))))))

(defparameter *grid-background*
  (let ((array (make-array '(40 40) :initial-element 0)))
    (dotimes (i 40)
      (setf (aref array 0 i) 1
            (aref array i 0) 1))
    (c:make-rectangular-tile (c:make-pattern array (list c:+white+ c:+black+))
                             40 40)))

Changes to Demo/demo.lisp.

︙︙
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
   :layers (list (<geom-line>)
                 (<geom-point> :aesthetics
                               (list :ink c:+red+
                                     :line-style (make-line-style :thickness 5)))))
  "Three layers illustrating mapping and multiple layers.")

(define-demo-plot *test2*

  (let* ((data1 (loop for x from -200 upto 300 by 10
                      for y from -20 by 20
                      for z from -40 by 10
                      collect (list x y c:+red+)




                      collect (list x (- z) c:+blue+)))
         (data1 (list* '("x" "y" "ink") data1)))

    (<polyclot> :title "points"
                :dataset data1
                :aesthetics (list :x "x" :y "y" :ink "ink"
                                  :line-style (make-line-style :thickness 4
                                                               :cap-shape :round))
                :coords (<coord-cartesian>)
                :layers (list (<geom-point>)))))

(define-demo-plot *test3*
  (let* ((data1 (loop for x from -5 upto 5
                      for y from 5 downto -5
                      collect (list x y c:+red+)
                      collect (list x y c:+blue+)))
         (data1 (list* '("x" "y" "ink") data1)))







>
|
<
<
|
>
>
>
>
|
|
>
|
|
|
|
|
|
|







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
   :layers (list (<geom-line>)
                 (<geom-point> :aesthetics
                               (list :ink c:+red+
                                     :line-style (make-line-style :thickness 5)))))
  "Three layers illustrating mapping and multiple layers.")

(define-demo-plot *test2*
  (<polyclot> :title "points"
              :dataset +data/series+


              :aesthetics (list :x "x" :y "y" :ink "ink"
                                :line-style (make-line-style :thickness 4
                                                             :cap-shape :round))
              :faceting (i::<faceting-wrap> :var '("ink") :ncol 2)
              :coords (<coord-cartesian>)
              :layers (list (<geom-point>))))

(define-demo-plot *test2/no-facet*
  (<polyclot> :title "points (no facet)"
              :dataset +data/series+
              :aesthetics (list :x "x" :y "y" :ink "ink"
                                :line-style (make-line-style :thickness 4
                                                             :cap-shape :round))
              :coords (<coord-cartesian>)
              :layers (list (<geom-point>))))

(define-demo-plot *test3*
  (let* ((data1 (loop for x from -5 upto 5
                      for y from 5 downto -5
                      collect (list x y c:+red+)
                      collect (list x y c:+blue+)))
         (data1 (list* '("x" "y" "ink") data1)))
︙︙
69
70
71
72
73
74
75
76
77
78





79
80
81
82
83
84
85
                            (make-scale :discrete :y))))

(define-demo-plot *test6*
  (<polyclot> :title "points/binned"
              :dataset +data/ages+ :aesthetics '(:x "age")
              :coords (<coord-cartesian> :ylim (make-range 0 nil))
              :layers (list (<geom-bar>))
              :faceting (list (i::<faceting-wrap> :var '("sex") :ncol 2))
              :scales (list (make-scale :binned :x :bins 5)
                            (make-scale :continuous :y))))








(defgeneric display (frame stream)
  (:method (frame stream)
    (print frame stream)))








<


>
>
>
>
>







73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
                            (make-scale :discrete :y))))

(define-demo-plot *test6*
  (<polyclot> :title "points/binned"
              :dataset +data/ages+ :aesthetics '(:x "age")
              :coords (<coord-cartesian> :ylim (make-range 0 nil))
              :layers (list (<geom-bar>))

              :scales (list (make-scale :binned :x :bins 5)
                            (make-scale :continuous :y))))

#+ (or)
(i::dohash ((key val) (i::group-by '("sex" "eye") +data/ages+ :test #'equal))
  (format *debug-io* "~a~%----------------------~%" val)
  )



(defgeneric display (frame stream)
  (:method (frame stream)
    (print frame stream)))

︙︙
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
    (setf (active frame)
          (mapcar #'symbol-value (plots frame)))))

(define-polyclot-demo-frame-command (com-nop :menu t) ())

(c:define-gesture-name :show-dataset :pointer-button-press (:left :meta))

(define-presentation-action act-show-dataset
    (<polyclot> nil my-command-table :gesture :show-dataset)
    (object)
  (dolist (layer (i::layers object))
    (i::show-dataset (i::trained-dataset layer))))

(defmethod display ((frame polyclot-demo-frame) *standard-output*)
  (format-items (mapcar 'symbol-value (plots frame)) :printer #'present*)
  (terpri)
  (let ((active (active frame)))
    (if (null active)
        (format t "(select a plot)")
        (formatting-item-list ()
          (dolist (plot active)
            (formatting-cell ()
              (polyclot plot *standard-output*)))))))

(defparameter *frame*
  (find-application-frame 'polyclot-demo-frame
                          :plots *demo-plots*))







<
<
<
<
<
<






|







124
125
126
127
128
129
130






131
132
133
134
135
136
137
138
139
140
141
142
143
144
    (setf (active frame)
          (mapcar #'symbol-value (plots frame)))))

(define-polyclot-demo-frame-command (com-nop :menu t) ())

(c:define-gesture-name :show-dataset :pointer-button-press (:left :meta))







(defmethod display ((frame polyclot-demo-frame) *standard-output*)
  (format-items (mapcar 'symbol-value (plots frame)) :printer #'present*)
  (terpri)
  (let ((active (active frame)))
    (if (null active)
        (format t "(select a plot)")
        (formatting-item-list (*standard-output* :row-wise nil)
          (dolist (plot active)
            (formatting-cell ()
              (polyclot plot *standard-output*)))))))

(defparameter *frame*
  (find-application-frame 'polyclot-demo-frame
                          :plots *demo-plots*))

Changes to Demo/test-data.lisp.

︙︙
56
57
58
59
60
61
62
63
64
65
66

67
68
69









70
71
72
73
74
75
    ("other" "le lisp")
    ("other" "franz lisp")
    ("other" "interlisp")
    ;; bla bla bla ...
    ))

(defparameter +data/ages+
  `(("age" "sex")
    ,@(loop repeat 40
            for age = (random 100)
            for sex = (if (zerop (random 2)) :male :female)

            collect (list age sex))))












#+ (or)
(i::doseq (elt (i::data-array
                (stat-data (i::<stat-count>)
                           (i::bind-data '(:x "dialect") +data/lisps+))))
  (format *debug-io* "~16a: ~4d~%" (i::x elt) (i::y elt)))







|
|


>
|


>
>
>
>
>
>
>
>
>






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
    ("other" "le lisp")
    ("other" "franz lisp")
    ("other" "interlisp")
    ;; bla bla bla ...
    ))

(defparameter +data/ages+
  `(("age" "sex" "eye")
    ,@(loop repeat 400
            for age = (random 100)
            for sex = (if (zerop (random 2)) :male :female)
            for eye = (elt '(:red :blue :green :brown :black) (random 5))
            collect (list age sex eye))))


(defparameter +data/series+
  (let ((data (loop for x from -200 upto 300 by 10
                    for y from -20 by 20
                    for z from -40 by 10
                    for q from +280 by 5
                    collect (list x y c:+red+)
                    collect (list x (- z) c:+blue+)
                    collect (list x q c:+dark-green+))))
    (list* '("x" "y" "ink") data)))

#+ (or)
(i::doseq (elt (i::data-array
                (stat-data (i::<stat-count>)
                           (i::bind-data '(:x "dialect") +data/lisps+))))
  (format *debug-io* "~16a: ~4d~%" (i::x elt) (i::y elt)))

Changes to README.org.

︙︙
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    element; should this translation be performed even by position-identity?
  - [ ] handle n/a values
- [1/4] position adjustment
  - [X] dodge bars
  - [ ] stack bars
  - [ ] fill bars
  - [ ] parameter refinements
- [-] implementation of faceting (only embronic class hierarchy)
  - [X] <facet-null>
  - [ ] <facet-wrap>
  - [ ] <facet-grid>
- [X] the hierarchy of defaults
  - [X] dataset is inherited from the plot
  - [X] aesthetic mapping is merged with the plot (or inherited?)
- [ ] allow processing elements to declare required aesthetics, dropped
  aesthetics, modified aesthetics and introduced aesthetics; this will help
  with better error reporting and debugging







|

|







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    element; should this translation be performed even by position-identity?
  - [ ] handle n/a values
- [1/4] position adjustment
  - [X] dodge bars
  - [ ] stack bars
  - [ ] fill bars
  - [ ] parameter refinements
- [-] implementation of faceting
  - [X] <facet-null>
  - [X] <facet-wrap>
  - [ ] <facet-grid>
- [X] the hierarchy of defaults
  - [X] dataset is inherited from the plot
  - [X] aesthetic mapping is merged with the plot (or inherited?)
- [ ] allow processing elements to declare required aesthetics, dropped
  aesthetics, modified aesthetics and introduced aesthetics; this will help
  with better error reporting and debugging
︙︙
189
190
191
192
193
194
195

196
197
198
199
200
201
202

News:
- [2022-11-08 Tue] moved the repository fossil.turtleware.eu/polyclot
- [2022-11-16 Wed] implemented binned scale (and fixed scale mapping)
- [2022-12-11 nie] improvements to scales, a simple listener tool (demo)
- [2022-12-13 wto] add colors, initial render of the color scale
- [2022-12-14 śro] position adjustment (dodge)


** Faceting

Facets are being implemented after a single plot abstraction has been defined
and that demands some architectural changes. Let's start with a description:

Faceting is an operation of splitting datasets into a few separate sets that are







>







189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

News:
- [2022-11-08 Tue] moved the repository fossil.turtleware.eu/polyclot
- [2022-11-16 Wed] implemented binned scale (and fixed scale mapping)
- [2022-12-11 nie] improvements to scales, a simple listener tool (demo)
- [2022-12-13 wto] add colors, initial render of the color scale
- [2022-12-14 śro] position adjustment (dodge)
- [2023-05-17 Wed] the architecture has been changed to account for faceting

** Faceting

Facets are being implemented after a single plot abstraction has been defined
and that demands some architectural changes. Let's start with a description:

Faceting is an operation of splitting datasets into a few separate sets that are
︙︙