-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
barchart02_title.html
77 lines (68 loc) · 2.17 KB
/
barchart02_title.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Student's First Barchart</title>
<meta name="description" content="Student's First Barchart" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
<script src="../web_modules/d3/dist/d3.js"></script>
<link href="./style.css" rel="stylesheet" />
<style>
rect {
fill: steelblue;
fill-opacity: 0.8;
}
rect:hover {
fill-opacity: 1;
}
.axis {
font-size: smaller;
}
</style>
</head>
<body>
<h1>Student's First Barchart</h1>
<script>
const margin = { top: 40, bottom: 10, left: 120, right: 20 };
const width = 800 - margin.left - margin.right;
const height = 600 - margin.top - margin.bottom;
// Creates sources <svg> element
const svg = d3
.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
// Group used to enforce margin
const g = svg.append("g").attr("transform", `translate(${margin.left},${margin.top})`);
// Global variable for all data
const data = [66.38, 21.51, 23.37, 34.17, 36.21];
const bar_height = 50;
/////////////////////////
// TODO create a nested title element that shows the value as tooltip
// Render the chart with new data
// DATA JOIN
const rect = g
.selectAll("rect")
.data(data)
.join(
// ENTER
// new elements
(enter) => enter.append("rect").attr("x", 0),
// UPDATE
// update existing elements
(update) => update,
// EXIT
// elements that aren't associated with data
(exit) => exit.remove()
);
// ENTER + UPDATE
// both old and new elements
rect
.attr("height", bar_height)
.attr("width", (d) => d * 7)
.attr("y", (d, i) => i * (bar_height + 5));
</script>
</body>
</html>