-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
197 lines (164 loc) · 4.5 KB
/
index.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
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Line Chart</title>
<script src="http://d3js.org/d3.v3.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
svg {
font-family: Helvetica;
}
.domain {
fill: none;
stroke: black;
}
.tick line {
stroke: black;
}
.tick {
font-size: 10px;
}
.line {
fill: none;
stroke: blue;
}
.dot {
fill: blue;
stroke: blue;
}
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
font-size: 11px;
text-anchor: start !important;
}
.axis path {
display: none;
}
.y.axis text {
fill: #999;
}
.y.axis .minor line {
stroke: #ccc;
stroke-dasharray: 2,4;
}
.x.axis .minor text{
display: none;
}
.annotationline {
fill:none;
stroke: black;
}
.annotation {
font-family: helvetica;
font-size: 11px;
text-align: center;
}
</style>
</head>
<body>
<h1>Strikeouts on the Rise</h1>
<p>Baseball. America's sport under fire from increasing strikeouts and merciless pitchers. Or just worse batters? Here is some data.</p>
<div class="chart"></div>
<script type="text/javascript">
d3.csv("strikeouts.csv", function(data){
var margin = {top: 20, right: 20, bottom: 20, left: 20};
width = 750 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
data.forEach(function(d) {
d.so = +d.so;
d.year = +d.year;
d.g = +d.g;
//We also want to calculate the strikeouts per game and store it as a new column
d.kpg = d.so / d.g;
});
var seattleData = data.filter(function(d) { return d.franchise === "SEA"; });
var svg = d3.select(".chart").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 + ")");
var xScale = d3.scale.linear()
.domain([1900, 2012])
.range([0, width]);
var yScale = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.kpg; })])
.range([height, 0]);
//creates plot
svg.selectAll(".dot")
.data(seattleData)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 2)
.attr("cx", function(d, i) { return xScale(d.year); })
.attr("cy", function(d) { return yScale(d.kpg); })
//creates line
var line = d3.svg.line()
.x(function(d) { return xScale(d.year); })
.y(function(d) { return yScale(d.kpg); });
svg.append("path")
.attr("d", line(seattleData))
.attr("class", "line");
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickFormat(function(d) { return d; })
.scale(xScale);
svg.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(0, " + height + ")")
.call(xAxis);
var margin = 720;
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("right")
.ticks(10)
.tickPadding(10)
.tickSize(-width - margin.left - margin.right);
svg.append("g")
.attr("class", "y-axis")
.attr("transform", "translate(" + margin + ",0)")
.call(yAxis)
.selectAll("g")
.classed("minor", function(d,i) { return i !== 0; });
svg.append("line")
.attr("class", "annotationline")
.attr("x1", xScale(1986))
.attr("y1", yScale(8))
.attr("x2", xScale(1986))
.attr("y2", yScale(7.2));
svg.selectAll(".annotation")
.data([
"Wikipedia did not explain",
"what happened here.",
])
.enter().append("text")
.classed("annotation", true)
.attr("x", xScale(1977))
.attr("y", yScale(8.5))
.attr("dy", function(d, i) { return i * 1.3 + "em"; })
.text(function(d) { return d; });
svg.append("line")
.attr("class", "annotationline")
.attr("x1", xScale(2007))
.attr("y1", yScale(5))
.attr("x2", xScale(2007))
.attr("y2", yScale(3));
svg.selectAll(".annotation")
.data([
"Mariners go on eight-",
"game winning streak.",
])
.enter().append("text")
.classed("annotation", true)
.attr("x", xScale(1995))
.attr("y", yScale(2.7))
.attr("dy", function(d, i) { return i * 1.3 + "em"; })
.text(function(d) { return d; });
});
</script>
</body>
</html>