-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathpointer.ts
57 lines (53 loc) · 2.16 KB
/
pointer.ts
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
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import {html} from "htl";
export async function pointerRenderCompose() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.dot(
penguins,
Plot.pointer({
x: "culmen_length_mm",
y: "culmen_depth_mm",
r: 8,
fill: "red",
render(index, scales, values, dimensions, context, next) {
const node = next(index, scales, values, dimensions, context);
node.setAttribute("fill", "blue");
return node;
}
})
),
Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm"})
]
});
}
export async function pointerViewof() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
const plot = Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm", tip: true}).plot();
const textarea = html`<textarea rows=10 style="width: 640px; resize: none;">`;
const oninput = () => (textarea.value = JSON.stringify(plot.value, null, 2));
oninput(); // initialize the textarea to the initial value
plot.oninput = oninput; // update during interaction
return html`<figure>${plot}${textarea}</figure>`;
}
export async function pointerViewofTitle() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
const plot = Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm", tip: true}).plot({title: "Penguins"});
const textarea = html`<textarea rows=10 style="width: 640px; resize: none;">`;
const oninput = () => (textarea.value = JSON.stringify(plot.value, null, 2));
oninput(); // initialize the textarea to the initial value
plot.oninput = oninput; // update during interaction
return html`<figure>${plot}${textarea}</figure>`;
}
export async function pointerNonFaceted() {
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.lineY(aapl, {x: "Date", y: "Close", fy: (d) => d.Close % 2 === 0}),
Plot.ruleX(aapl, {x: "Date", strokeOpacity: 0.1}),
Plot.ruleX(aapl, Plot.pointerX({x: "Date", stroke: "red"}))
]
});
}