-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
188 lines (155 loc) · 5.41 KB
/
index.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<head>
<style>
.plot {
border: 1px solid gray;
margin: 10px;
;
}
</style>
</head>
<body>
<div>
<p>
<a href="https://observablehq.com/">ObservableHQ</a>:
<ul>
<li>
<a href="https://observablehq.com/@observablehq/plot-gallery">Gallery</a>
</li>
<li>
<a href="https://observablehq.com/plot/api">API Index</a>
</li>
</ul>
</p>
</div>
<div>
<div id="plot-1a"></div>
<div id="plot-1b"></div>
</div>
<div id="plot-2" class="plot"></div>
<div id="plot-3" class="plot"></div>
<script type="module">
import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/+esm";
import * as d3 from "https://cdn.skypack.dev/d3@7";
///////////////////////////
///////////////////////////
// data
const loadData = async (filename_1) => {
const res = await fetch(filename_1);
const data = await res.json();
return data
// return data.slice(0,10)
}
const penguins = await loadData('penguins.json');
window.penguins = penguins;
console.log(penguins);
const population = await loadData('population.json');
window.population = population;
console.log(population);
let data = null;
///////////////////////////
///////////////////////////
// plot 1
// https://observablehq.com/d/98fa0c34fb79cfae
const sl = d3.scaleLinear();
data = penguins;
const arr = [];
arr.push(
Plot.dot(data, {
x: "flipper_length_mm",
y: "body_mass_g",
fill: "sex",
clip: true,
tip: { lineHeight: 1.3, maxRadius: 5 }
}),
Plot.density(data, {
x: "flipper_length_mm",
y: "body_mass_g",
stroke: "sex",
thresholds: 10,
clip: true
})
);
const renderPlot = (xDomain, yDomain) =>
Plot.plot({
width: 740,
height: 500,
x: { domain: xDomain },
y: { domain: yDomain },
inset: 10,
grid: true,
//color: { legend: false },
// figure: true,
marks: arr,
});
const insertPlot = (xDomain = [150, 250], yDomain = [2000, 6500]) => {
const chart = renderPlot(xDomain, yDomain);
console.log(chart);
d3.select('#plot-1a').selectAll('*').remove();
d3.select('#plot-1a').append(() => chart);
// IMPORTANT - legend below - required for zoom alignment
// d3.select(chart).select("div").raise();
// else remove legend
// d3.select(chart).select("div").remove();
// or manage legend separately
return [
chart.scale("x"),
chart.scale("y"),
chart.legend("color"),
// chart.title("my title"),
// chart.subtitle("my subtitle"),
// chart.caption("my caption"),
];
};
const zoomed = (e) => {
console.log(e.transform);
const x = sl.domain(xScale.domain).range(xScale.range);
const xDomain = e.transform.rescaleX(x).domain();
const y = sl.domain(yScale.domain).range(yScale.range);
const yDomain = e.transform.rescaleY(y).domain();
console.log({ xDomain, yDomain })
insertPlot(xDomain, yDomain);
};
const [xScale, yScale, legend] = insertPlot();
const zoom = d3.zoom().on("zoom", zoomed);
d3.select('#plot-1a').call(zoom);
d3.select('#plot-1b').append(() => legend);
///////////////////////////
///////////////////////////
// plot 2
data = penguins;
const plot_2 = Plot.plot({
grid: true,
color: { legend: true },
marks: [
Plot.dot(data, { x: "culmen_length_mm", y: "culmen_depth_mm", fill: "species" }),
Plot.linearRegressionY(data, { x: "culmen_length_mm", y: "culmen_depth_mm", stroke: "species" }),
Plot.linearRegressionY(data, { x: "culmen_length_mm", y: "culmen_depth_mm" })
]
})
const div_2 = document.querySelector("#plot-2");
// div_2.append(plot_2);
///////////////////////////
///////////////////////////
// plot 3
// https://observablehq.com/@observablehq/plot-grouped-bar-chart
data = population;
const plot_3 = Plot.plot({
x: { axis: null },
y: { tickFormat: "s", grid: true },
color: { scheme: "spectral", legend: true },
marks: [
Plot.barY(population, {
x: "key",
y: "population",
fill: "key",
fx: "state",
sort: { x: null, color: null, fx: { value: "-y", reduce: "sum" } }
}),
Plot.ruleY([0])
]
})
const div_3 = document.querySelector("#plot-3");
// div_3.append(plot_3);
</script>
</body>