Last active
November 20, 2017 15:47
-
-
Save JulienAssouline/2034acfab9ae910121195aa180b31f9a to your computer and use it in GitHub Desktop.
Bar chart transition with text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>D3: Loading TopoJSON data and generating SVG paths</title> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="//d3js.org/topojson.v1.min.js"></script> | |
<style type="text/css"> | |
/* No style rules here yet */ | |
body,html{ | |
margin: 0; | |
padding: 0; | |
font-family: "Arial", sans-serif; | |
font-size: 11px; | |
text-align: center; | |
} | |
#chart{ | |
background-color: white; | |
stroke-width: white; | |
} | |
.axisx path { | |
fill: none; | |
stroke: white; | |
stroke-width: 2px; | |
shape-rendering: crispEdges; | |
} | |
.axisy path { | |
fill: none; | |
stroke: white; | |
} | |
.axisy line{ | |
fill: none; | |
stroke: #dcd9d3; | |
stroke-width: 2px; | |
shape-rendering: crispEdges; | |
opacity: 0.7; | |
stroke-dasharray: 3,3; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
var w = 700 | |
var h = 400 | |
var margin = { | |
right: 20, | |
left: 40, | |
top: 70, | |
bottom: 20 | |
} | |
var width = w - margin.right - margin.left; | |
var height = h - margin.top - margin.bottom; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("id", "chart") | |
.attr("width", w) | |
.attr("height", h) | |
.append("g") | |
.attr("transform", "translate(0" + margin.left + "," + margin.top + ")"); | |
var parseTime = d3.timeParse("%Y-%m-%d") | |
var xtickValues = [0, 10, 20, 30, 40, 50, 60]; | |
var xtickFormat = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October"] | |
xScale = d3.scaleTime() | |
.range([0, width]) | |
yScale = d3.scaleLinear() | |
.range([height, 0]) | |
var xAxis = d3.axisBottom() | |
.scale(xScale) | |
.tickFormat(function(d,i){ return xtickFormat[i] }) | |
.tickSizeOuter(0) | |
var yAxis = d3.axisLeft() | |
.scale(yScale) | |
.tickValues(xtickValues) | |
.tickSize(-width,0,0) | |
.tickSizeOuter(0) | |
// var controls = d3.select("body") | |
// .append("div") | |
// .attr("id", "controls"); | |
// var button = controls.append("button") | |
// .html("View Chart") | |
// .attr("state", 0); | |
d3.csv("US_mass_shootings_2017_by_date.csv", function(error, data){ | |
data.forEach(function(d){ | |
d.Killed = +d.Killed | |
d.Injured = +d.Injured | |
d.Date = parseTime(d.Date) | |
}) | |
xScale.domain(d3.extent(data, function(d){ return d.Date })) | |
yScale.domain([0, d3.max(data, function(d){ return d.Killed; })]) | |
svg.append("g") | |
.classed("axisx", true) | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
svg.append("g") | |
.classed("axisy", true) | |
.attr("transform", "translate(0,0)") | |
.call(yAxis) | |
.selectAll("text") | |
.attr("transform", "translate("+ -10 + ",0)") | |
// Bar Chart | |
var myChart = svg.selectAll(".rect") | |
.data(data) | |
.enter() | |
.append("rect") | |
.classed("bar", true) | |
.attr("x", function(d){ | |
return xScale(d.Date) | |
}) | |
.attr("y", height) | |
.attr("width", 5) | |
.attr("height", 0) | |
.style("fill", "darkred") | |
myChart.transition() | |
.attr("height", function(d){ | |
return height - yScale(d.Killed) | |
}) | |
.attr("y", function(d){ | |
return yScale(d.Killed) | |
}) | |
.duration(500) | |
.delay(function(d, i){ | |
return i * 30; | |
}) | |
.style("opacity", 1) | |
// Text label | |
var myText = svg.selectAll(".label") | |
.data(data) | |
.enter() | |
.append("text") | |
.attr("class", "label") | |
.attr("x", function(d){ | |
return xScale(d.Date) - 155 | |
}) | |
.attr("y", function(d){ | |
return yScale(d.Killed) + 30 | |
}) | |
.text(function(d){ | |
if(d.Killed == 62){ | |
return "Las Vegas, 62 deaths" | |
} | |
}) | |
.style("opacity", 0) | |
.style("font-size", 15) | |
myText.transition() | |
.attr("x", function(d){ | |
return xScale(d.Date) - 155 | |
}) | |
.attr("y", function(d){ | |
return yScale(d.Killed) + 30 | |
}) | |
.duration(500) | |
.delay(function(d, i){ | |
return i * 32; | |
}) | |
.style("opacity", 1) | |
}) | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date | Killed | Injured | ||
---|---|---|---|---|
2 | 2017-01-01 | 1 | 15 | |
15 | 2017-01-03 | 0 | 4 | |
22 | 2017-01-04 | 3 | 1 | |
35 | 2017-01-06 | 7 | 8 | |
42 | 2017-01-07 | 1 | 3 | |
63 | 2017-01-11 | 2 | 6 | |
67 | 2017-01-12 | 5 | 4 | |
77 | 2017-01-15 | 4 | 5 | |
83 | 2017-01-16 | 0 | 12 | |
99 | 2017-01-20 | 1 | 8 | |
105 | 2017-01-21 | 2 | 13 | |
109 | 2017-01-22 | 6 | 7 | |
121 | 2017-01-25 | 0 | 6 | |
126 | 2017-01-26 | 2 | 3 | |
134 | 2017-01-27 | 0 | 10 | |
146 | 2017-01-29 | 1 | 4 | |
150 | 2017-01-30 | 0 | 4 | |
156 | 2017-01-31 | 0 | 8 | |
34 | 2017-02-06 | 4 | 0 | |
41 | 2017-02-07 | 1 | 8 | |
47 | 2017-02-08 | 1 | 4 | |
51 | 2017-02-09 | 4 | 0 | |
57 | 2017-02-10 | 1 | 5 | |
62 | 2017-02-11 | 1 | 7 | |
66 | 2017-02-12 | 2 | 14 | |
76 | 2017-02-15 | 3 | 6 | |
88 | 2017-02-18 | 1 | 7 | |
93 | 2017-02-19 | 1 | 3 | |
98 | 2017-02-20 | 0 | 5 | |
104 | 2017-02-21 | 4 | 4 | |
108 | 2017-02-22 | 0 | 4 | |
120 | 2017-02-25 | 3 | 7 | |
125 | 2017-02-26 | 1 | 4 | |
133 | 2017-02-27 | 0 | 4 | |
141 | 2017-02-28 | 0 | 6 | |
18 | 2017-03-03 | 0 | 9 | |
24 | 2017-03-04 | 1 | 8 | |
30 | 2017-03-05 | 0 | 4 | |
43 | 2017-03-07 | 0 | 4 | |
59 | 2017-03-10 | 5 | 3 | |
80 | 2017-03-15 | 3 | 1 | |
91 | 2017-03-18 | 3 | 2 | |
95 | 2017-03-19 | 1 | 5 | |
112 | 2017-03-22 | 5 | 0 | |
117 | 2017-03-24 | 2 | 10 | |
123 | 2017-03-25 | 4 | 9 | |
128 | 2017-03-26 | 6 | 21 | |
137 | 2017-03-27 | 2 | 4 | |
153 | 2017-03-30 | 4 | 0 | |
0 | 2017-04-01 | 3 | 12 | |
7 | 2017-04-02 | 2 | 9 | |
13 | 2017-04-03 | 3 | 8 | |
32 | 2017-04-06 | 1 | 3 | |
40 | 2017-04-07 | 13 | 7 | |
50 | 2017-04-09 | 2 | 6 | |
68 | 2017-04-13 | 1 | 3 | |
75 | 2017-04-15 | 0 | 26 | |
81 | 2017-04-16 | 0 | 17 | |
96 | 2017-04-20 | 0 | 4 | |
102 | 2017-04-21 | 2 | 2 | |
124 | 2017-04-26 | 0 | 4 | |
131 | 2017-04-27 | 1 | 3 | |
139 | 2017-04-28 | 0 | 4 | |
145 | 2017-04-29 | 3 | 24 | |
149 | 2017-04-30 | 7 | 17 | |
5 | 2017-05-01 | 2 | 3 | |
11 | 2017-05-02 | 1 | 3 | |
25 | 2017-05-04 | 1 | 3 | |
44 | 2017-05-07 | 2 | 8 | |
54 | 2017-05-09 | 3 | 1 | |
60 | 2017-05-10 | 1 | 3 | |
74 | 2017-05-14 | 1 | 20 | |
84 | 2017-05-16 | 3 | 1 | |
100 | 2017-05-20 | 0 | 10 | |
106 | 2017-05-21 | 1 | 7 | |
129 | 2017-05-26 | 0 | 4 | |
138 | 2017-05-27 | 9 | 12 | |
143 | 2017-05-28 | 1 | 22 | |
148 | 2017-05-29 | 1 | 3 | |
154 | 2017-05-30 | 1 | 3 | |
4 | 2017-06-01 | 3 | 1 | |
10 | 2017-06-02 | 3 | 6 | |
17 | 2017-06-03 | 5 | 4 | |
29 | 2017-06-05 | 6 | 0 | |
37 | 2017-06-06 | 3 | 2 | |
49 | 2017-06-08 | 0 | 6 | |
53 | 2017-06-09 | 2 | 5 | |
58 | 2017-06-10 | 0 | 4 | |
64 | 2017-06-11 | 0 | 17 | |
70 | 2017-06-13 | 2 | 10 | |
73 | 2017-06-14 | 5 | 11 | |
79 | 2017-06-15 | 6 | 3 | |
86 | 2017-06-17 | 1 | 8 | |
90 | 2017-06-18 | 3 | 17 | |
111 | 2017-06-22 | 1 | 6 | |
116 | 2017-06-24 | 1 | 13 | |
122 | 2017-06-25 | 1 | 7 | |
136 | 2017-06-27 | 0 | 5 | |
152 | 2017-06-30 | 2 | 6 | |
3 | 2017-07-01 | 2 | 35 | |
9 | 2017-07-02 | 0 | 8 | |
16 | 2017-07-03 | 4 | 4 | |
23 | 2017-07-04 | 2 | 11 | |
28 | 2017-07-05 | 6 | 7 | |
36 | 2017-07-06 | 0 | 4 | |
48 | 2017-07-08 | 1 | 12 | |
52 | 2017-07-09 | 0 | 7 | |
78 | 2017-07-15 | 1 | 7 | |
89 | 2017-07-18 | 1 | 11 | |
94 | 2017-07-19 | 1 | 8 | |
110 | 2017-07-22 | 0 | 5 | |
113 | 2017-07-23 | 1 | 7 | |
127 | 2017-07-26 | 1 | 6 | |
135 | 2017-07-27 | 1 | 4 | |
142 | 2017-07-28 | 0 | 4 | |
147 | 2017-07-29 | 3 | 7 | |
151 | 2017-07-30 | 2 | 8 | |
157 | 2017-07-31 | 0 | 4 | |
1 | 2017-08-01 | 0 | 17 | |
8 | 2017-08-02 | 0 | 4 | |
14 | 2017-08-03 | 1 | 3 | |
21 | 2017-08-04 | 3 | 2 | |
27 | 2017-08-05 | 2 | 10 | |
33 | 2017-08-06 | 0 | 17 | |
46 | 2017-08-08 | 1 | 6 | |
65 | 2017-08-12 | 1 | 3 | |
69 | 2017-08-13 | 0 | 13 | |
72 | 2017-08-14 | 2 | 2 | |
82 | 2017-08-16 | 0 | 4 | |
92 | 2017-08-19 | 1 | 4 | |
97 | 2017-08-20 | 2 | 9 | |
103 | 2017-08-21 | 6 | 14 | |
107 | 2017-08-22 | 0 | 4 | |
115 | 2017-08-24 | 4 | 0 | |
119 | 2017-08-25 | 0 | 4 | |
132 | 2017-08-27 | 2 | 7 | |
140 | 2017-08-28 | 2 | 4 | |
12 | 2017-09-02 | 1 | 7 | |
20 | 2017-09-03 | 2 | 3 | |
26 | 2017-09-04 | 0 | 4 | |
39 | 2017-09-06 | 0 | 4 | |
56 | 2017-09-09 | 0 | 4 | |
61 | 2017-09-10 | 9 | 5 | |
71 | 2017-09-13 | 2 | 16 | |
85 | 2017-09-16 | 0 | 8 | |
87 | 2017-09-17 | 2 | 7 | |
101 | 2017-09-20 | 0 | 5 | |
114 | 2017-09-23 | 1 | 9 | |
118 | 2017-09-24 | 2 | 19 | |
130 | 2017-09-26 | 3 | 9 | |
144 | 2017-09-28 | 1 | 3 | |
155 | 2017-09-30 | 1 | 3 | |
6 | 2017-10-01 | 62 | 243 | |
19 | 2017-10-03 | 0 | 4 | |
31 | 2017-10-05 | 4 | 0 | |
38 | 2017-10-06 | 0 | 5 | |
45 | 2017-10-07 | 0 | 4 | |
55 | 2017-10-09 | 0 | 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment