-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
persons.html
97 lines (87 loc) · 2.74 KB
/
persons.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>D3 Weight vs Age</title>
<meta name="description" content="Sample D3 Chart" />
<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></style>
</head>
<body>
<h1>D3 Persons: Weight vs Age</h1>
<script>
const margin = { top: 20, bottom: 40, left: 30, 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})`);
const data = [
{
name: "Steve",
age: 10,
weight: 30,
gender: "male",
},
{
name: "Stan",
age: 15,
weight: 60,
gender: "male",
},
{
name: "Tom",
age: 18,
weight: 70,
gender: "male",
},
{
name: "Marie",
age: 18,
weight: 58,
gender: "female",
},
];
const color = d3.scaleOrdinal().domain(["female", "male"]).range(["red", "blue"]);
const xscale = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d.age)])
.range([0, width]);
const yscale = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d.weight)])
.range([height, 0]);
const xaxis = d3.axisBottom().scale(xscale);
const yaxis = d3.axisLeft().scale(yscale);
g.append("g").classed("x.axis", true).attr("transform", `translate(0,${height})`).call(xaxis);
g.append("g").classed("y.axis", true).call(yaxis);
const group = g.append("g");
const marks = group
.selectAll("circle")
.data(data)
.join(
(enter) => {
const marks_enter = enter.append("circle");
marks_enter.attr("r", 5).append("title");
return marks_enter;
},
(update) => update,
(exit) => exit.remove()
);
marks
.style("fill", (d) => color(d.gender))
.attr("cx", (d) => xscale(d.age))
.attr("cy", (d) => yscale(d.weight));
marks.select("title").text((d) => d.name);
</script>
</body>
</html>