-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathanimated_lines.html
More file actions
188 lines (147 loc) · 5.03 KB
/
animated_lines.html
File metadata and controls
188 lines (147 loc) · 5.03 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
<!-- code from http://big-elephants.com/2014-06/unrolling-line-charts-d3js/ -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated Lines and Line Sections</title>
<style>
.chart {
min-height: 200;
min-width: 600;
}
svg {
background-color: #eee;
}
</style>
</head>
<body>
<h2>Animating Lines and Line Sections</h2>
<p>Data shown are Algerian fertility rates over time (1960-2013), from UNICEF/World Bank. Note: I suggest making any highlight or animated segment have a thicker stroke-width than the base line.</p>
<br>
<button id="button2">Click for a smooth animation.</button>
<div class="chart" id="chart2"></div>
<button id="button3">Click to highlight a section.</button>
<div class="chart" id="chart3"></div>
<button id="button4">Click to animate a hightlighted section.</button>
<div class="chart" id="chart4"></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script>
var width = 700,
height = 200;
var dateFormat = d3.time.format("%Y").parse;
var x_scale, y_scale; // making these global so they can be seen in ex3 after def in ex2; could have just set the domain there.
// this is shared by both, so make it global
var lineFunction = d3.svg.line()
.x(function(d) {return x_scale(dateFormat(d.Year));})
.y(function(d) {return y_scale(+d.Fertility); });
d3.csv("data/fertility_algeria.csv", doLines);
function doLines(error, data) {
ex2(data);
ex3(data);
ex4(data);
}
// the full animation one
function ex2(data) {
x_scale = d3.scale.linear().range([0,width]).domain(d3.extent(data, function(d) {return dateFormat(d.Year)}));
// just adding a little manual padding here
y_scale = d3.scale.linear().range([height-5,10]).domain(d3.extent(data, function(d) {return +d.Fertility;}));
var example2 = d3.select("#chart2")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "exampleTwo");
var path = example2
.append("path")
.attr("id", "Series2")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("d", lineFunction(data))
.style("display", "none");
// you need a path to be created before you can get the length.
var totalLength = path.node().getTotalLength();
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.style("display", null) // now we can display it
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0); // we animate the offset down to 0.
// this is repetitive with the above, but it shows how you would repeat the drawing...
d3.select("#button2").on("click", function() {
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.style("display", null)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
});
}
// the colored highlight section
function ex3(data) {
var example3 = d3.select("#chart3")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "exampleThree");
example3
.datum(data)
.append("path")
.attr("id", "Series3")
.attr("d", lineFunction)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2);
d3.select("#button3")
.on("click", function() {
example3.datum(data.slice(20,44))
.append("path")
.attr("d", lineFunction)
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 3); // make it thicker so it totally covers the base line
});
}
function ex4(data) {
var example4 = d3.select("#chart4")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "exampleFour");
example4
.datum(data)
.append("path")
.attr("id", "Series3")
.attr("d", lineFunction)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2);
d3.select("#button4")
.on("click", function() {
var pathoverlay = example4
.append("path")
.attr("id", "Series4")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("d", lineFunction(data.slice(20,44)))
.style("display", "none");
// you need a path to be created before you can get the length.
var totalLength = pathoverlay.node().getTotalLength();
pathoverlay
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.style("display", null) // now we can display it
.attr("stroke", "orange")
.transition()
.duration(1000)
.ease("linear")
.attr("stroke-dashoffset", 0) // we animate the offset down to 0
.attr("stroke-width", 3); // make it thicker so it totally covers the base line
});
}
</script>
</body>
</html>