forked from arnicas/interactive-vis-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacked_area_to_stream.html
185 lines (131 loc) · 4.83 KB
/
stacked_area_to_stream.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
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
<!DOCTYPE html>
<!-- mod of Mike Bostock's block http://bl.ocks.org/mbostock/3020685 -->
<meta charset="utf-8">
<style>
body {
font: 12px sans-serif;
padding: 50px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
button {
background: orange;
margin: 5px;
}
.selected {
background: #ccc;
}
.layer {
fill: white;
}
</style>
<body>
<h2>Stacked Area And Streamgraph Charts of South African Child Deaths from Measles</h2>
<p><a href="http://apps.who.int/gho/data/node.main.ghe100-by-cause?lang=en">WHO data</a>, deaths of children 0-4 years old in selected South African countries.</p>
<button id="stream">Stream It!</button><button id="stacked" class="selected">Stack It!</button>
<div id="chart"></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script>
var originaldata;
var formatDate = d3.time.format("Year %Y");
var fullheight = 500, fullwidth = 960;
var margin = {top: 20, right: 30, bottom: 30, left: 60},
width = fullwidth - margin.left - margin.right,
height = fullheight - margin.top - margin.bottom;
var xScale = d3.time.scale()
.range([0, width]);
var yScale = d3.scale.linear()
.range([height, 0]);
var colorScale = d3.scale.category20b();
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var stack = d3.layout.stack()
.offset("zero") // try "silhouette" next, that's a streamgraph!
//.order("inside-out") // try this and see what you think
.values(function(d) { return d.values; })
.x(function(d) { return formatDate.parse(d.Year);})
.y(function(d) { return +d.Measles; });
// exactly the same except for offset:
var stream = d3.layout.stack()
.offset("silhouette") // try "silhouette" next, that's a streamgraph!
//.order("inside-out") // try this and see what you think
.values(function(d) { return d.values; })
.x(function(d) { return formatDate.parse(d.Year);})
.y(function(d) { return +d.Measles; });
// what's another way we can DRY this out? how about just updating the "offset" when we do the redraw below...
var area = d3.svg.area()
.interpolate("cardinal")
.x(function(d) { return xScale(formatDate.parse(d.Year)); })
.y0(function(d) { return yScale(d.y0); })
.y1(function(d) { return yScale(d.y0 + d.y); });
var svg = d3.select("#chart").append("svg")
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("data/deaths_04yearsold_excerpt.csv", setup);
function setup(error, data) {
if (error) { console.log(error) };
originaldata = data; // assign to a global so i can use it in other function
var dataset = d3.nest()
.key(function(d) {
return d.Country;
})
.sortKeys(d3.descending) // alphabetic order from top layer
.sortValues(function (a, b) { return formatDate.parse(a.Year) - formatDate.parse(b.Year)})
.entries(data);
console.log(dataset);
// set up the UI function:
d3.selectAll("button").on("click", function() {
var method = d3.select(this).attr("id"); // this is stream or stacked
d3.selectAll("button").classed("selected", false);
d3.select(this).classed("selected", true);
redraw(dataset, method);
});
// default first view: create the axes, so you transition them in the redraw.
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
redraw(dataset, "stacked"); // method is either stacked or stream
}
function redraw(dataset, method) {
var layers;
if (method == "stacked") {
layers = stack(dataset); // this operation also modifies the dataset
} else {
layers = stream(dataset); // this operation also modifies the dataset
}
console.log("layers", layers); // it adds a y and y0 to the data values.
// reset these AFTER doing the layer stacking.
xScale.domain(d3.extent(originaldata, function(d) { return formatDate.parse(d.Year); }));
yScale.domain([0, d3.max(originaldata, function(d) { return d.y0 + d.y; })]);
var layers = svg.selectAll(".layer")
.data(layers, function(d) {return d.key;});
layers.enter().append("path")
.attr("class", "layer")
.append("title")
.text(function(d) {
return d.key; // country is the key
});
layers.transition().duration(1000)
.attr("d", function(d) { return area(d.values); })
.style("fill", function(d, i) { return colorScale(i); });
layers.exit().remove();
// transition your axes.
d3.select(".x.axis").transition().call(xAxis);
d3.select(".y.axis").transition().call(yAxis); // there is no visible change actually
}
</script>