-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbar_graph.html
155 lines (136 loc) · 3.99 KB
/
bar_graph.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Crete+Round:400,400italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,600' rel='stylesheet' type='text/css'>
<title>Some Data on Mass Shootings</title>
<script type="text/javascript" src="../d3/d3.v3.js"></script>
<style type="text/css">
body {
background: #faf9f0;
}
a:link {
color: #607f9f;
text-decoration: none;
}
a:visited {
color: #43596f;
text-decoration: none;
}
h1 {
text-align: center;
font-family: 'Crete Round', serif;
font-size: 22px;
font-weight: 400;
}
p {
margin-left: 20%;
font-family: 'Source Sans Pro', sans-serif;
font-size: 12px;
font-weight: 400;
}
.axis {
font-family: 'Source Sans Pro', sans-serif;
font-size: 10px;
font-weight: 400;
}
.axis path,
.axis line {
fill: none;
stroke: gray;
stroke-width: .5px;
shape-rendering:crispEdges;
}
#tooltip3 {
text-anchor: middle;
font: 10px sans-serif;
font-weight: regular;
fill: white;
pointer-events: none;
}
.space {
margin-left: 500px;
}
</style>
</head>
<body>
<br>
<br>
<h1>Mass Shootings in the United States: 1982—2015</h1>
<br>
<script type="text/javascript">
//Width and height
var margin = {top: 30, right: 100, bottom: 50, left: 100},
w = 1200 - margin.left - margin.right,
h = 400 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangeRoundBands([0, w], .05);
var y = d3.scale.linear().range([h, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var svg = d3.select("body")
.append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//Load in shootings data
d3.csv("byYear.csv", function(data) {
x.domain(data.map(function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) {return d.value; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -10)
.attr("x",-198)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Number of Mass Shootings");
svg.selectAll("bar")
.data(data)
.enter()
.append("rect")
.attr("x", function(d) { return x(d.date); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) {return h - y(d.value); })
.attr("fill", "#afbfcf")
.on("mouseover", function(d) {
d3.select(this)
.attr("fill", "#cc0000");
//Get this bar's x/y values, then augment for the tooltip
var xPosition = parseFloat(d3.select(this).attr("x")) + x.rangeBand() / 2;
var yPosition = parseFloat(d3.select(this).attr("y")) + 14;
//Create the tooltip label
svg.append("text")
.attr("id", "tooltip3")
.attr("x", xPosition)
.attr("y", yPosition)
.text(d.value);
})
.on("mouseout", function(d) {
d3.select(this)
.transition()
.duration(250)
.attr("fill", "#90a5bc");
//Remove the tooltip
d3.select("#tooltip3").remove()
.transition()
.duration(250);
})
})
</script>
<p>Source: Mass Shootings data from <a href = "http://www.motherjones.com/politics/2012/12/mass-shootings-mother-jones-full-data" target="_blank">Mother Jones Magazine</a><span class=space><a href = "../Mass shootings/index.html">NEXT ></a></span></p>
</body>
</html>