-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar.js
138 lines (118 loc) · 4.36 KB
/
bar.js
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
var margin = {top: 20, right: 50, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%y-%b-%d").parse,
formatPercent = d3.format(".0%");
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category20();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
var line = d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y0(function(d) { return y(d.y); })
.y1(function(d) { return y(d.y); });
function line_to_stacked(t) {
return d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y0(function(d) { return y(t * d.y0 + d.y); })
.y1(function(d) { return y(t * d.y0 + d.y); });
}
function area_to_stacked(t) {
return d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y0(function(d) { return y(d.y0 + (1 - t) * d.y); })
.y1(function(d) { return y(d.y0 + d.y); });
}
var stack = d3.layout.stack()
.values(function(d) { return d.values; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("/data/ukraine_emissions.json", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));
data.forEach(function(d) {
d.date = parseDate(d.date);
});
var browsers = stack(color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, y: d[name] / 100};
})
};
}));
x.domain(d3.extent(data, function(d) { return d.date; }));
var browser = svg.selectAll(".browser")
.data(browsers)
.enter().append("g")
.attr("class", "browser");
browser.append("path")
.attr("class", "area")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); })
.style("fill", function(d) { return color(d.name); })
browser.append("text")
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.y) + ")"; })
.attr("x", 3)
.attr("dy", ".35em")
.style("fill", function(d) { return color(d.name); })
.text(function(d) { return d.name; }); svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
var is_area_plot = false;
function transition() {
var duration = 1000;
var browser = svg.selectAll(".browser")
var transition = browser.transition()
.delay(function(d, i) { return i * 1000; })
.duration(duration);
var postTransition = transition.transition();
if (!is_area_plot) {
transition.selectAll("path")
.attrTween("d", shapeTween(line_to_stacked, 1));
transition.selectAll("text")
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.y0 + d.value.y) + ")"; });
postTransition.selectAll("path")
.attrTween("d", shapeTween(area_to_stacked, 1))
.style("stroke-opacity", 0.0);
postTransition.selectAll("text")
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.y0 + d.value.y / 2) + ")"; });
} else {
transition.selectAll("path")
.style("stroke-opacity", 1.0)
.attrTween("d", shapeTween(area_to_stacked, 0));
transition.selectAll("text")
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.y0 + d.value.y) + ")"; });
postTransition.selectAll("path")
.attrTween("d", shapeTween(line_to_stacked, 0));
postTransition.selectAll("text")
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.y) + ")"; });
}
is_area_plot = !is_area_plot;
}
function shapeTween(shape, direction) {
return function(d, i, a) {
return function(t) {
return shape(direction ? t : 1.0 - t)(d.values);
};
};
}