-
Notifications
You must be signed in to change notification settings - Fork 35
/
28_area_single.html
executable file
·162 lines (127 loc) · 3.17 KB
/
28_area_single.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Area chart</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
circle:hover {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<h1>United States CO2 Emissions</h1>
<p>The United States’ carbon dioxide emissions (kilotons), 1961-2010. Source: <a href="http://data.worldbank.org/indicator/EN.ATM.CO2E.KT?page=6">World Bank</a>, 2014</p>
<script type="text/javascript">
//Width, height, padding
var w = 700;
var h = 400;
var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left
//Set up date format function (years)
var dateFormat = d3.time.format("%Y");
//Define scales with ranges (domains will be set later)
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Define axes
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
//Define area generator
//
//This is what will take our data, and generate
//an SVG <path> element from it.
//
var area = d3.svg.area()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y0(h - padding[2])
.y1(function(d) {
return yScale(d.emissions);
});
//Create the SVG
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in CSV data
//
//This happens asynchronously; callback function fires when done
//
d3.csv("data/co2/usa_co2_emissions.csv", function(data) {
//Now that the data is loaded in, we can check its
//min and max values to set our scales' domains!
xScale.domain([
d3.min(data, function(d) {
return dateFormat.parse(d.year); //Earliest year
}),
d3.max(data, function(d) {
return dateFormat.parse(d.year); //Latest year
})
]);
yScale.domain([
d3.max(data, function(d) {
return +d.emissions; //Max emissions value
}),
0 //Baseline value
]);
//Area
//
//Note data is wrapped in another array, so all of its
//values are bound to a single element (the <path>!)
//
svg.data([ data ])
.append("path")
.attr("class", "area usa")
.attr("d", area)
.attr("fill", "SteelBlue")
.attr("stroke", "none");
//Create axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
});
</script>
</body>
</html>