Repository navigation
Expand file tree
/
Copy pathmultiple-charts.html
More file actions
281 lines (247 loc) · 8.27 KB
/
Copy pathmultiple-charts.html
File metadata and controls
281 lines (247 loc) · 8.27 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>D3 Two Charts on One Page</title>
<script src="https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="wrapper">
<h1>Two D3 charts on the same webpage</h1>
<p>
Multiple charts can be placed on the same page. But the constant names
for width, height, margin, svg, x-axis, y-axis (and whatever else you
have, such as legends and tooltips) in each chart need to be unique.
</p>
<div class="chart">
<h2>Bar Chart</h2>
<div id="barChart"></div>
</div>
<div class="chart">
<h2>Line Chart</h2>
<div id="lineChart"></div>
</div>
</div>
<script>
// Bar Chart
// =========
// Sample data: barX_key, barY_val
const barData = [
{ barX_key: "First", barY_val: 10 },
{ barX_key: "Second", barY_val: 15 },
{ barX_key: "Third", barY_val: 8 },
{ barX_key: "Fourth", barY_val: 12 },
];
const barWidth = 768;
const barHeight = 432;
const barMargin = { top: 50, right: 20, bottom: 50, left: 50 };
// Create the SVG graphic
const barSvg = d3
.create("svg")
.attr("width", barWidth)
.attr("height", barHeight)
.attr("viewBox", `0 0 ${barWidth} ${barHeight}`)
.attr("preserveAspectRatio", "xMinYMin meet")
.style("max-width", "100%")
.style("height", "auto");
// Declare the axis scales first, so that the chart can be drawn. Then draw the bars. Then draw the axes after the bars, so that they are in front of the bars
// Declare X scale (categorical)
const x = d3
.scaleBand()
.domain(barData.map((d) => d.barX_key))
.padding(0.2)
.range([barMargin.left, barWidth - barMargin.right]);
// Declare Y scale (linear)
const y = d3
.scaleLinear()
.domain([0, d3.max(barData, (d) => d.barY_val + 1)])
.range([barHeight - barMargin.bottom, barMargin.top]);
// Create tooltip
// (You only need to create this once per webpage)
const tooltip = d3.select("body").append("div").attr("class", "tooltip");
// Add Bars
barSvg
.selectAll(".bar")
.data(barData)
.enter()
.append("rect")
.classed("bar", true)
.attr("fill", "SteelBlue")
.attr("x", (d) => x(d.barX_key))
.attr("y", (d) => y(d.barY_val))
.attr("width", x.bandwidth())
.attr("height", (d) => barHeight - barMargin.bottom - y(d.barY_val))
.on("mouseover", function (event, d) {
tooltip
.style("opacity", 1)
.html(`${d.barX_key}: ${d.barY_val}`)
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 10 + "px");
})
.on("mousemove", function (event) {
tooltip
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 10 + "px");
})
.on("mouseout", function () {
tooltip.style("opacity", 0);
});
// Add X axis
barSvg
.append("g")
.attr("transform", `translate(0, ${barHeight - barMargin.bottom})`)
.call(d3.axisBottom(x))
.attr("font-size", "0.8rem");
// Add X axis label
barSvg
.append("g")
.attr(
"transform",
`translate(${barWidth / 2}, ${barHeight - barMargin.bottom + 40})`,
)
.append("text")
.text("barX_key");
// Add Y axis
barSvg
.append("g")
.attr("transform", `translate(${barMargin.left},0)`)
.call(d3.axisLeft(y))
.attr("font-size", "0.8rem");
// Add Y axis label
barSvg
.append("g")
.attr("transform", `translate(10, ${barHeight / 2}) rotate(-90)`)
.append("text")
.text("barY_val");
// Add title
barSvg
.append("text")
// Centering the title
.attr("x", barWidth / 2)
.attr("style", `transform: translateX(-50%);`)
.attr("y", 30)
.attr("font-weight", "bold")
.text("barY_val per barX_key");
// Append the SVG element
barChart.append(barSvg.node());
// Charts may have separate scripts
</script>
<script>
// Line Chart
// ==========
// Sample data: lineX_key, lineY_val
const lineData = [
{ lineX_key: "1", lineY_val: 9 },
{ lineX_key: "2", lineY_val: 12 },
{ lineX_key: "3", lineY_val: 10 },
{ lineX_key: "4", lineY_val: 15 },
{ lineX_key: "5", lineY_val: 14 },
{ lineX_key: "6", lineY_val: 18 },
];
const lineWidth = 768;
const lineHeight = 432;
const lineMargin = { top: 50, right: 20, bottom: 50, left: 50 };
// Create the SVG graphic
const lineSvg = d3
.create("svg")
.attr("width", lineWidth)
.attr("height", lineHeight)
.attr("viewBox", `0 0 ${lineWidth} ${lineHeight}`)
.attr("preserveAspectRatio", "xMinYMin meet")
.style("max-width", "100%")
.style("height", "auto");
// Declare the axis scales first, so that the chart can be drawn. Then draw the bars. Then draw the axes after the bars, so that they are in front of the bars
// Declare X scale (linear)
const lineX = d3
.scaleLinear()
.domain([0, lineData.length])
.range([lineMargin.left, lineWidth - lineMargin.right]);
// Declare Y scale (linear)
const lineY = d3
.scaleLinear()
.domain([0, d3.max(lineData, (d) => d.lineY_val + 1)])
.range([lineHeight - lineMargin.bottom, lineMargin.top]);
// Add data dots (same as scatter)
lineSvg
.append("g")
.attr("stroke", "none")
.attr("stroke-width", 1.5)
.attr("fill", "orange")
.selectAll("circle")
.data(lineData)
.join("circle")
.attr("cx", (d) => lineX(d.lineX_key))
.attr("cy", (d) => lineY(d.lineY_val))
.attr("r", 5)
.classed("dot", true)
.on("mouseover", function (event, d) {
tooltip
.style("opacity", 1)
.html(`${d.lineX_key}: ${d.lineY_val}`)
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 10 + "px");
})
.on("mousemove", function (event) {
tooltip
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 10 + "px");
})
.on("mouseout", function () {
tooltip.style("opacity", 0);
});
// Set up the line generator
const line = d3
.line()
.x((d) => lineX(d.lineX_key))
.y((d) => lineY(d.lineY_val));
// .curve(d3.curveBasis);
// Add the line path to the SVG
lineSvg
.append("path")
.datum(lineData)
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 2)
.attr("d", line);
// Add X axis
lineSvg
.append("g")
.attr("transform", `translate(0, ${lineHeight - lineMargin.bottom})`)
.call(d3.axisBottom(lineX).ticks(lineData.length)) // without extras (fractions) between
.attr("font-size", "0.8rem");
// Add X axis label
lineSvg
.append("g")
.attr(
"transform",
`translate(${lineWidth / 2}, ${lineHeight - lineMargin.bottom + 40})`,
)
.append("text")
.text("lineX_key");
// Add Y axis
lineSvg
.append("g")
.attr("transform", `translate(${lineMargin.left},0)`)
.call(d3.axisLeft(lineY))
.attr("font-size", "0.8rem");
// Add Y axis label
lineSvg
.append("g")
.attr("transform", `translate(10, ${lineHeight / 2}) rotate(-90)`)
.append("text")
.text("lineY_val");
// Add title
lineSvg
.append("text")
// Centering the title
.attr("x", lineWidth / 2)
.attr("style", `transform: translateX(-50%);`)
.attr("y", 30)
.attr("font-weight", "bold")
.text("lineY_val per lineX_key");
// Append the SVG element
lineChart.append(lineSvg.node());
</script>
</body>
</html>