Repository navigation
Expand file tree
/
Copy pathempty_load.html
More file actions
157 lines (128 loc) · 4.66 KB
/
Copy pathempty_load.html
File metadata and controls
157 lines (128 loc) · 4.66 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis--y path {
display: none;
}
.cities {
fill: none;
stroke: #aaa;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 1.5px;
}
.city--hover {
stroke: #000;
}
.focus text {
text-anchor: middle;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
.voronoi path {
fill: none;
pointer-events: all;
}
.voronoi--show path {
stroke: red;
stroke-opacity: 0.2;
}
#form {
position: absolute;
top: 20px;
right: 30px;
}
</style>
<svg width="960" height="500"></svg>
<label id="form" for="show-voronoi">
Show Voronoi
<input type="checkbox" id="show-voronoi" disabled>
</label>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var xs = [];
var svg = d3.select("svg"),
margin = { top: 20, right: 30, bottom: 30, left: 40 },
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear()
.domain([0, 200])
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var voronoi = d3.voronoi()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.value); })
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]]);
var line = d3.line()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.value); });
d3.csv("unemployment.csv").get(function (error, input_data) {
var data = [];
input_data.columns.slice(1)
.forEach(e => xs.push(+e));
input_data.forEach(function (d) {
var c = { name: d.name, values: null };
c.values = [];
xs.forEach(function (k, i) {
c.values.push(
{ city: c, date: xs[i], value: +d[k] }
);
});
data.push(c);
});
x.domain(d3.extent(xs));
y.domain([0, d3.max(data, function (c) { return d3.max(c.values, function (d) { return d.value; }); })]).nice();
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10))
.append("text")
.attr("x", 4)
.attr("y", 0.5)
.attr("dy", "0.32em")
.style("text-anchor", "start")
.style("fill", "#000")
.style("font-weight", "bold")
.text("Unemployment Rate");
g.append("g")
.attr("class", "cities")
.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", function (d) { d.line = this; return line(d.values); });
var focus = g.append("g")
.attr("transform", "translate(-100,-100)")
.attr("class", "focus");
focus.append("circle")
.attr("r", 3.5);
focus.append("text")
.attr("y", -10);
var voronoiGroup = g.append("g")
.attr("class", "voronoi");
voronoiGroup.selectAll("path")
.data(voronoi.polygons(d3.merge(data.map(function (d) { return d.values; }))))
.enter().append("path")
.attr("d", function (d) { return d ? "M" + d.join("L") + "Z" : null; })
.on("mouseover", mouseover)
.on("mouseout", mouseout);
d3.select("#show-voronoi")
.property("disabled", false)
.on("change", function () { voronoiGroup.classed("voronoi--show", this.checked); });
function mouseover(d) {
console.log("d", d);
console.log("d.data", d.data);
d3.select(d.data.city.line).classed("city--hover", true);
d.data.city.line.parentNode.appendChild(d.data.city.line);
focus.attr("transform", "translate(" + x(d.data.date) + "," + y(d.data.value) + ")");
focus.select("text").text(d.data.city.name);
}
function mouseout(d) {
d3.select(d.data.city.line).classed("city--hover", false);
focus.attr("transform", "translate(-100,-100)");
}
});
</script>