Skip to content

Commit ccaa681

Browse files
authored
default tip for auto (#1546)
* default tip for auto * use real DOM during hydration * fix unit tests * defer for auto mark * update comment
1 parent fedb13b commit ccaa681

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+111
-21
lines changed

docs/components/PlotRender.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class Element {
6060
removeAttributeNS(namespace, name) {
6161
this.removeAttribute(name);
6262
}
63+
addEventListener() {
64+
// ignored; interaction needs real DOM
65+
}
66+
removeEventListener() {
67+
// ignored; interaction needs real DOM
68+
}
6369
appendChild(child) {
6470
this.children.push(child);
6571
child.parentNode = this;
@@ -127,7 +133,7 @@ export default {
127133
...this.options,
128134
className: "plot"
129135
};
130-
if (this.defer) { // || typeof document !== "undefined") {
136+
if (this.defer) {
131137
const mounted = (el) => {
132138
disconnect(); // remove old listeners
133139
function observed() {
@@ -191,7 +197,6 @@ export default {
191197
]
192198
);
193199
}
194-
options.document = new Document();
195-
return Plot[method](options).toHyperScript();
200+
return Plot[method]({...options, document: new Document()}).toHyperScript();
196201
}
197202
};

docs/marks/auto.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ onMounted(() => {
1919

2020
The magic ✨ **auto mark** automatically selects a mark type that best represents the given dimensions of the data according to some simple heuristics. The auto mark—which powers [Observable’s chart cell](https://observablehq.com/@observablehq/chart-cell)—is intended to support fast exploratory analysis where the goal is to get a useful plot as quickly as possible. For example, two quantitative dimensions make a scatterplot:
2121

22-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-scatterplot
22+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-scatterplot
2323
```js
2424
Plot.auto(penguins, {x: "body_mass_g", y: "flipper_length_mm"}).plot()
2525
```
@@ -36,7 +36,7 @@ Because its heuristics are likely to evolve, they are not explicitly documented;
3636

3737
A monotonically increasing dimension (here *Date*, as the data is ordered chronologically), paired with a numeric column (*Close*), makes a line chart:
3838

39-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-line-chart
39+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-line-chart
4040
```js
4141
Plot.auto(aapl, {x: "Date", y: "Close"}).plot()
4242
```
@@ -50,7 +50,7 @@ Plot.auto(olympians, {x: "weight"}).plot()
5050
```
5151
:::
5252

53-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-ordinal-histogram
53+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-ordinal-histogram
5454
```js
5555
Plot.auto(penguins, {x: "island"}).plot()
5656
```
@@ -60,7 +60,7 @@ This is easier than deciding whether to use bin and rect, or group and bar: the
6060

6161
If you’d like to explicitly avoid grouping the data, you can opt out of the reducer, and get a one-dimensional plot:
6262

63-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-barcode
63+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-barcode
6464
```js
6565
Plot.auto(penguins, {x: "body_mass_g", y: {reduce: null}}).plot()
6666
```
@@ -92,21 +92,21 @@ Plot.auto(olympians, {x: "weight", y: "sex", color: "count"}).plot()
9292

9393
Similarly, with explicit marks and transforms, changing a vertical histogram to a horizontal histogram involves switching [rectY](./rect.md#recty-data-options) to [rectX](./rect.md#rectx-data-options), [binX](../transforms/bin.md#binx-outputs-options) to [binY](../transforms/bin.md#biny-outputs-options), **x** to **y**, and **y** to **x**. With the auto mark, just specify **y** instead of **x**:
9494

95-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-horizontal-histogram
95+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-horizontal-histogram
9696
```js
9797
Plot.auto(penguins, {y: "island"}).plot()
9898
```
9999
:::
100100

101101
For the sake of seamless switching, the auto mark has just one color channel, which it assigns to either **fill** or **stroke** depending on the mark. We can see that clearly by overriding a line chart with the **mark** option to make an area chart:
102102

103-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-color-channel
103+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-color-channel
104104
```js
105105
Plot.auto(industries, {x: "date", y: "unemployed", color: "industry"}).plot()
106106
```
107107
:::
108108

109-
:::plot
109+
:::plot defer
110110
```js
111111
Plot.auto(industries, {x: "date", y: "unemployed", color: "industry", mark: "area"}).plot()
112112
```
@@ -126,15 +126,15 @@ Plot
126126

127127
You can similarly pass a **zero** option to indicate that zero is meaningful for either **x** or **y**. This adds a corresponding rule to the returned mark.
128128

129-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-zero-option
129+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-zero-option
130130
```js
131131
Plot.auto(industries, {x: "date", y: {value: "unemployed", zero: true}, color: "industry"}).plot()
132132
```
133133
:::
134134

135135
The auto mark has a **size** channel, which (currently) always results in a dot. For now, it’s an alias for the dot’s **r** channel; in the future it will also represent a vector’s **length** channel.
136136

137-
:::plot https://observablehq.com/@observablehq/plot-auto-mark-size-channel
137+
:::plot defer https://observablehq.com/@observablehq/plot-auto-mark-size-channel
138138
```js
139139
Plot.auto(aapl, {x: "Date", y: "Close", size: "Volume"}).plot()
140140
```

src/marks/auto.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export function autoSpec(data, options) {
142142
y: Y ?? undefined, // treat null y as undefined for implicit stack
143143
[colorMode]: C ?? colorColor,
144144
z: Z,
145-
r: S ?? undefined // treat null size as undefined for default constant radius
145+
r: S ?? undefined, // treat null size as undefined for default constant radius
146+
tip: true
146147
};
147148
let transformImpl;
148149
let transformOptions = {[colorMode]: colorReduce ?? undefined, r: sizeReduce ?? undefined};

test/marks/auto-test.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ it("Plot.autoSpec makes a histogram from a quantitative dimension", () => {
1919
y: undefined,
2020
fill: undefined,
2121
z: undefined,
22-
r: undefined
22+
r: undefined,
23+
tip: true
2324
},
2425
transformImpl: "binX",
2526
transformOptions: {fill: undefined, r: undefined, y: "count"},
@@ -45,7 +46,8 @@ it("Plot.autoSpec makes a bar chart from an ordinal dimension", () => {
4546
y: undefined,
4647
fill: "blue",
4748
z: undefined,
48-
r: undefined
49+
r: undefined,
50+
tip: true
4951
},
5052
transformImpl: "groupX",
5153
transformOptions: {fill: undefined, r: undefined, y: "count"},
@@ -75,7 +77,8 @@ it("Plot.autoSpec makes a lineY from monotonic x", () => {
7577
y: Object.assign([1, 0, 38], {label: "value"}),
7678
stroke: undefined,
7779
z: undefined,
78-
r: undefined
80+
r: undefined,
81+
tip: true
7982
},
8083
transformImpl: undefined,
8184
transformOptions: {stroke: undefined, r: undefined},
@@ -105,7 +108,8 @@ it("Plot.autoSpec makes a lineY from monotonic x and monotonic y", () => {
105108
y: Object.assign([0, 1, 38], {label: "value"}),
106109
stroke: undefined,
107110
z: undefined,
108-
r: undefined
111+
r: undefined,
112+
tip: true
109113
},
110114
transformImpl: undefined,
111115
transformOptions: {stroke: undefined, r: undefined},
@@ -135,7 +139,8 @@ it("Plot.autoSpec makes a lineX from monotonic y", () => {
135139
y: Object.assign([1, 2, 3], {label: "date"}),
136140
stroke: undefined,
137141
z: undefined,
138-
r: undefined
142+
r: undefined,
143+
tip: true
139144
},
140145
transformImpl: undefined,
141146
transformOptions: {stroke: undefined, r: undefined},
@@ -165,7 +170,8 @@ it("Plot.autoSpec makes a line from non-monotonic x and non-monotonic y", () =>
165170
y: Object.assign([1, 0, 38], {label: "value"}),
166171
stroke: undefined,
167172
z: undefined,
168-
r: undefined
173+
r: undefined,
174+
tip: true
169175
},
170176
transformImpl: undefined,
171177
transformOptions: {stroke: undefined, r: undefined},
@@ -195,7 +201,8 @@ it("Plot.autoSpec makes a dot plot from two quantitative dimensions", () => {
195201
y: Object.assign([0, 3, 2], {label: "y"}),
196202
stroke: undefined,
197203
z: undefined,
198-
r: undefined
204+
r: undefined,
205+
tip: true
199206
},
200207
transformImpl: undefined,
201208
transformOptions: {stroke: undefined, r: undefined},
@@ -228,7 +235,8 @@ it("Plot.autoSpec makes a faceted heatmap", () => {
228235
y: {value: Object.assign([0, 3, 2, 1, 6, 2], {label: "y"})},
229236
fill: undefined,
230237
z: undefined,
231-
r: undefined
238+
r: undefined,
239+
tip: true
232240
},
233241
transformImpl: "bin",
234242
transformOptions: {fill: "count", r: undefined},

test/output/autoArea.svg

Lines changed: 1 addition & 0 deletions
Loading

test/output/autoAreaColor.svg

Lines changed: 1 addition & 0 deletions
Loading

test/output/autoAreaColorColor.svg

Lines changed: 1 addition & 0 deletions
Loading

test/output/autoAreaColorName.svg

Lines changed: 1 addition & 0 deletions
Loading

test/output/autoAreaColorValue.svg

Lines changed: 1 addition & 0 deletions
Loading

test/output/autoAreaStackColor.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)