Skip to content

Commit 7ef209e

Browse files
committed
tableplot param flow - wip
1 parent 0956db2 commit 7ef209e

File tree

1 file changed

+62
-23
lines changed

1 file changed

+62
-23
lines changed

src/data_visualization/tableplot_parameter_flow.clj

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
;;
3030
;; This tutorial is a brief intro to this feature.
3131

32-
;; ## An example problem
32+
;; ## The Challenge: Customizing Grid Colors
3333
;;
34-
;; Let's start with a basic dataset and plot:
34+
;; Let's start with a basic dataset and plot.
3535

3636
;; Assume we have some data.
3737

@@ -44,20 +44,23 @@ sample-data
4444
;; In this tutorial, we'll use Tableplot's [Plotly.js](https://plotly.com/javascript/) API,
4545
;; which generates interactive Plotly.js visualizations. Tableplot also supports other backends
4646
;; like Vega-Lite and an experimental transpilation API.
47+
4748
;; We can make a basic line plot. This is really easy, because the
4849
;; `:x` and `:y` columns are used by default for the plot's axes.
4950

5051
(-> sample-data
5152
plotly/layer-line)
5253

53-
;; Assume that now wish to to colour the grid lines: vertical by green,
54+
;; Assume that we now wish to colour the grid lines: vertical by green,
5455
;; horizontal by red. After all, what would be a
5556
;; better way to teach Tufte's [data-ink ratio](https://infovis-wiki.net/wiki/Data-Ink_Ratio) principle than doing exactly
5657
;; what it asks us to avoid, by adding some chartjunk?
5758

5859
;; Here are a few ways we can do it.
5960

6061
;; ### A brief look inside
62+
;;
63+
;; *(can skip on first read)*
6164

6265
;; By default, when used in [Kindly](https://scicloj.github.io/kindly-noted/)-compatible
6366
;; tools like [Clay](https://scicloj.github.io/clay/) and in Clojure Civitas posts,
@@ -70,18 +73,22 @@ sample-data
7073
plotly/layer-line
7174
kind/pprint)
7275

73-
;; You see, what the API functions such as `plotly/layer-line`
74-
;; generate certain maps called
75-
;; [templats](https://github.com/jsa-aerial/hanami?tab=readme-ov-file#templates-substitution-keys-and-transformations),
76-
;; a brilliant notion coming from
77-
;; the [Hanami](https://github.com/jsa-aerial/hanami) library.
76+
;; You see, what API functions such as `plotly/layer-line` generate are
77+
;; certain maps called
78+
;; [templates](https://github.com/jsa-aerial/hanami?tab=readme-ov-file#templates-substitution-keys-and-transformations),
79+
;; a brilliant concept from the [Hanami](https://github.com/jsa-aerial/hanami) library.
7880

7981
;; This is not the resulting Plotly.js specification yet.
80-
;; It is a potential for it, specifiying lots of partial intermediate
81-
;; values, called susbsitution keys.
82-
;; By Tableplot's convetion, substitution keys will be keywords beginning with `=`,
83-
;; such as `:=layout` or `:=mark-color`.
84-
;; They can have default values, which can also be functions computing
82+
;; It is a potential for it, specifying lots of partial intermediate
83+
;; values, called substitution keys.
84+
;; By Tableplot's convention, substitution keys are keywords beginning with `=`,
85+
;; such as `:=layout` or `:=mark-color`.
86+
;;
87+
;; **Why templates?** They separate *what you want* (data mappings, colors, sizes)
88+
;; from *how to render it* (the actual Plotly.js specification). This gives you
89+
;; flexibility: you can override specific details or let defaults handle everything.
90+
;;
91+
;; Substitution keys can have default values, which can also be functions computing
8592
;; them from the values defined by other keys. On the user side,
8693
;; we may override any of these, as we'll see below.
8794

@@ -96,7 +103,7 @@ sample-data
96103
;; ## Using the relevant substitution keys
97104

98105
;; Sometimes, what we need can be precisely specified in Tableplot.
99-
;; You may find the following in Tabelplot's
106+
;; You may find the following in Tableplot's
100107
;; [Plotly API reference](https://scicloj.github.io/tableplot/tableplot_book.plotly_reference.html#yaxis-gridcolor):
101108

102109
;; - [`:=xaxis-gridcolor`](https://scicloj.github.io/tableplot/tableplot_book.plotly_reference.html#xaxis-gridcolor) - The color for the x axis grid lines
@@ -111,6 +118,8 @@ sample-data
111118
plotly/layer-line)
112119

113120
;; ### A brief look inside
121+
;;
122+
;; *(can skip on first read)*
114123

115124
;; Let us see what actually has changed in the
116125
;; resulting specification:
@@ -124,8 +133,8 @@ sample-data
124133

125134
;; ## Overriding a broader-scope key
126135

127-
;; Sometimes, you will not find exactly what you need in Tabeplot's
128-
;; prarameter system. Plotly.js itself will always be richer and more
136+
;; Sometimes, you will not find exactly what you need in Tableplot's
137+
;; parameter system. Plotly.js itself will always be richer and more
129138
;; flexible.
130139

131140
;; Imagine that the above `:=xaxis-gridcolor` & `:=yaxis-gridcolor` would
@@ -142,12 +151,6 @@ sample-data
142151

143152
;; - [`:=layout`](https://scicloj.github.io/tableplot/tableplot_book.plotly_reference.html#layout) - The layout part of the resulting Plotly.js specification
144153

145-
;; By the way, if you read further in that link to the docs, you will see
146-
;; that `:=layout` depends on `:=xaxis-gridcolor` and `:=yaxis-gridcolor`,
147-
;; among other things. When we specified those narrow-scope keys
148-
;; in our previous example, we actually went through affecting the
149-
;; broad-scope key, `:=layout`.
150-
151154
(-> sample-data
152155
(plotly/base {:=layout {:xaxis {:gridcolor "green"}
153156
:yaxis {:gridcolor "red"}}})
@@ -161,6 +164,8 @@ sample-data
161164
;; your use case.
162165

163166
;; ### A brief look inside
167+
;;
168+
;; *(can skip on first read)*
164169

165170
;; Let us see what happens:
166171

@@ -174,7 +179,13 @@ sample-data
174179
;; As expected this time, the layout is small and simple,
175180
;; just what you specified.
176181

177-
;; ### Overriding the target `:=layuot`
182+
;; By the way, if you read further in that link to the docs, you will see
183+
;; that `:=layout` depends on `:=xaxis-gridcolor` and `:=yaxis-gridcolor`,
184+
;; among other things. When we specified those narrow-scope keys
185+
;; in our previous example, we actually went through affecting the
186+
;; broad-scope key, `:=layout`.
187+
188+
;; ## Direct Manipulation After `plotly/plot`
178189

179190
;; Sometimes, you wish to work with Plotly.js high-level notions,
180191
;; but in a more refined way, preserving most of what we have.
@@ -195,6 +206,8 @@ sample-data
195206
(assoc-in [:layout :yaxis :gridcolor] "red"))
196207

197208
;; ### A brief look inside
209+
;;
210+
;; *(can skip on first read)*
198211

199212
;; You already know what to expect here:
200213

@@ -205,3 +218,29 @@ sample-data
205218
(assoc-in [:layout :yaxis :gridcolor] "red")
206219
kind/pprint)
207220

221+
;; ## Summary
222+
;;
223+
;; Tableplot's parameter substitution system gives you three levels of control:
224+
;;
225+
;; 1. **Specific substitution keys** (`:=xaxis-gridcolor`, `:=yaxis-gridcolor`)
226+
;; - ✅ Most convenient and discoverable
227+
;; - ✅ Preserves all other defaults
228+
;; - ❌ Limited to what Tableplot explicitly supports
229+
;;
230+
;; 2. **Broad-scope keys** (`:=layout`)
231+
;; - ✅ Full Plotly.js flexibility
232+
;; - ✅ Declarative, within Tableplot's API
233+
;; - ❌ Overrides ALL defaults for that scope
234+
;;
235+
;; 3. **Direct data manipulation** (`assoc-in` after `plotly/plot`)
236+
;; - ✅ Complete control
237+
;; - ✅ Surgical precision - only change what you want
238+
;; - ❌ More verbose
239+
;; - ❌ Leaves Tableplot's template system
240+
;;
241+
;; The key insight: **it's all just data**. Templates with substitution keys give you
242+
;; flexibility without magic. You can always drop down to plain Clojure data manipulation
243+
;; when needed.
244+
;;
245+
;; For more examples and the complete API reference, see the
246+
;; [Tableplot documentation](https://scicloj.github.io/tableplot/).

0 commit comments

Comments
 (0)