/
ch6-4.html
192 lines (165 loc) · 4.55 KB
/
ch6-4.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
186
187
188
189
190
191
192
<html>
<head>
<title>Germany World Cup Goals</title>
<meta name="description" content="Practical D3">
<meta name="author" content="Tarek Amr, @gr33ndata">
<style>
.axis path,
.axis line {
fill: none;
stroke: #EEE;
}
.axis text {
font-family: sans-serif;
font-size: 10px;
stroke: #555;
}
.area {
fill: #7F00FF;
stroke: #BBB;
}
.year-labels {
font-family: sans-serif;
font-size: 8px;
stroke: #555;
}
.position-labels {
font-family: sans-serif;
font-size: 10px;
stroke: #555;
}
</style>
</head>
<body>
</body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
germany_cup = [
{
'year': 1990, 'goals_for': 15, 'goals_against': 5, 'position': 1
},
{
'year': 1994, 'goals_for': 9,'goals_against': 7, 'position': 5
},
{
'year': 1998,
'goals_for': 8,
'goals_against': 6,
'position': 7
},
{
'year': 2002,
'goals_for': 14,
'goals_against': 3,
'position': 2
},
{
'year': 2006,
'goals_for': 14,
'goals_against': 6,
'position': 3
},
{
'year': 2010,
'goals_for': 16,
'goals_against': 5,
'position': 3
},
{
'year': 2014,
'goals_for': 18,
'goals_against': 4,
'position': 1
}
];
</script>
<script type="text/javascript">
// Width and Height of our SVG area
w = 400; h = 300;
// Margin around our chart
margin = 20;
// Let the JavaScript append the SVG into the body this time.
var svg_area = d3.select("body")
.append("svg")
.attr('width', w)
.attr('height', w);
var xScale = d3.scale
.linear()
// It is better programmatically calculate
// the domain values from the data.
// But let's do it manually now for simplicity
.domain([1988, 2016])
.range([margin, w-margin]);
var yScale = d3.scale
.linear()
// It is better programmatically calculate
// the domain values from the data.
// But let's do it manually now for simplicity
.domain([0, 20])
.range([h-margin, margin]);
var gen = d3.svg.area()
.x(function(d){
return xScale(d['year']);
})
.y0(function(d){
return yScale(d['goals_for']);
})
.y1(function(d){
return yScale(d['goals_against']);
})
.interpolate('monotone');
svg_area.append("path")
.attr("class", "area")
.attr("d", gen(germany_cup));
svg_area.append("text")
.style("text-anchor", "middle")
.attr("x", w/2).attr("y", 20)
.text("Goals For/Against");
svg_area.append("text")
.style("text-anchor", "middle")
.attr("x", w/2).attr("y", h-10)
.text("Germany World Cup");
svg_area.selectAll(".position-labels")
.data(germany_cup)
.enter()
.append("text")
.attr("class", "position-labels")
.attr("x", function(d){
return xScale(d['year']) - 10;
})
.attr("y", function(d){
return yScale(d['goals_for']) - 20;
})
.text(function(d){
if (d['position'] == 1){
return "1st";
} else if (d['position'] == 2) {
return "2nd";
} else {
return d['position'] + "th"
}
});
svg_area.selectAll(".year-labels")
.data(germany_cup)
.enter()
.append("text")
.attr("class", "year-labels")
.attr("x", function(d){
return xScale(d['year']) - 10;
})
.attr("y", function(d){
return yScale(d['goals_against']) + 20;
})
.text(function(d){
return "" + d['year'] + "";
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("right")
.ticks(10);
svg_area.append("g")
.attr("transform", "translate(" + (w - margin) + ",0)")
.attr("class", "y axis")
.call(yAxis);
</script>
<html>