Skip to content

Commit 8a55c7b

Browse files
committed
make scatter_hist variables configurable
1 parent d9c2882 commit 8a55c7b

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

_config.yml

Whitespace-only changes.

_plot-prep.qmd

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```{ojs}
2+
//| output: false
3+
4+
Plot = import("https://esm.sh/@observablehq/plot@0.6.17")
5+
d = transpose(data)
6+
7+
yaml = require("js-yaml")
8+
config_file = FileAttachment("_config.yml").text()
9+
config = yaml.load(config_file)
10+
11+
distinct_cutoff = 10
12+
13+
disc_types = (['string', 'boolean'])
14+
disc_filter = (config?.categorical_vars)
15+
? d => config.categorical_vars.includes(d.name)
16+
: d => disc_types.includes(d.type) && d.numDistinct <= distinct_cutoff && d.numDistinct > 1
17+
disc_vars = vars.filter(disc_filter)
18+
disc_opts = new Map([['', null], ...disc_vars.map(d => [d.label ? d.label : d.name, d.name])])
19+
20+
cont_types = (['integer', 'float', 'date', 'datetime', 'time'])
21+
cont_filter = (config?.numerical_vars)
22+
? d => config.numerical_vars.includes(d.name)
23+
: d => cont_types.includes(d.type) && d.numDistinct > distinct_cutoff
24+
cont_vars = vars.filter(cont_filter)
25+
cont_opts = new Map(cont_vars.map(d => [d.label ? d.label : d.name, d.name]))
26+
27+
x_val = config?.defaults?.x || cont_vars[0].name
28+
y_val = config?.defaults?.y || cont_vars[1].name
29+
color_val = config?.defaults?.color || disc_vars[0].name
30+
```

_scatter_hist.qmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
```{ojs}
22
//| panel: input
33
4-
viewof x_var = Inputs.select(cont_opts, {value: Array.from(cont_opts.values())[0], label: "X axis"})
5-
viewof y_var = Inputs.select(cont_opts, {value: Array.from(cont_opts.values())[1], label: "Y axis"})
6-
viewof color_var = Inputs.select(disc_opts, {value: Array.from(disc_opts.values())[1], label: "Color"})
4+
viewof x_var = Inputs.select(cont_opts, {value: x_val, label: "X axis"})
5+
viewof y_var = Inputs.select(cont_opts, {value: y_val, label: "Y axis"})
6+
viewof color_var = Inputs.select(disc_opts, {value: color_val, label: "Color"})
77
```
88

99
```{ojs}
1010
default_color = d3.schemeCategory10[0]
1111
plt_color = color_var || default_color
1212
13-
all_vars = cont_vars.concat(disc_vars)
14-
channels = Object.fromEntries(all_vars.map(k => [k[1], k[1]]))
13+
all_vars = [...cont_vars, ...disc_vars]
14+
channels = Object.fromEntries(all_vars.map(k => [k.name, k.name]))
1515
1616
viewof scatter = Plot.plot({
1717
style: { fontFamily: "var(--sans-serif)" },

index.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ description: |
1313

1414
{{< include _load-data.qmd >}}
1515

16+
{{< include _plot-prep.qmd >}}
17+
1618
{{< include _scatter_hist.qmd >}}
1719

1820
<!-- {{< include _scatter.qmd >}} -->

0 commit comments

Comments
 (0)