Created
June 7, 2019 19:14
-
-
Save diannepeng/0806f45c8663d67beca3417b97dbd08a to your computer and use it in GitHub Desktop.
Violent Deaths per 50 Years
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> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.bar rect { | |
shape-rendering: crispEdges; | |
} | |
.bar text { | |
fill: #999999; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
// Set the base bar color | |
var color = "blue"; | |
// A comma-separated list of the years, using negative numbers for BCE | |
var years = [-509, -499, -499, -485, -460, | |
-480, | |
-480, | |
-476, | |
-464, | |
-472, | |
-449, | |
-460, | |
-449, | |
-449, | |
-438, | |
-438, | |
-438, | |
-438, | |
-414, | |
-414, | |
-396, | |
-384, | |
-389, | |
-390, | |
-362, | |
-340, | |
-340, | |
-337, | |
-315, | |
-295, | |
-305, | |
-294, | |
-283, | |
-282, | |
-182, | |
-282, | |
-279, | |
-273, | |
-250, | |
-265, | |
-260, | |
-249, | |
-216, | |
-217, | |
-230, | |
-230, | |
-216, | |
-208, | |
-225, | |
-211, | |
-216, | |
-216, | |
-211, | |
-82, | |
-216, | |
-211, | |
-217, | |
-217, | |
-212, | |
-216, | |
-216, | |
-216, | |
-216, | |
-216, | |
-210, | |
-208, | |
-216, | |
-196, | |
-210, | |
-218, | |
-208, | |
-208, | |
-205, | |
-205, | |
-203, | |
-203, | |
-203, | |
-189, | |
-187, | |
-201, | |
-190, | |
-197, | |
-197, | |
-196, | |
-196, | |
-196, | |
-194, | |
-194, | |
-194, | |
-193, | |
-193, | |
-193, | |
-176, | |
-186, | |
-172, | |
-178, | |
-162, | |
-154, | |
-154, | |
-148, | |
-146, | |
-133, | |
-141, | |
-140, | |
-140, | |
-100, | |
-121, | |
-119, | |
-121, | |
-113, | |
-120, | |
-105, | |
-82, | |
-114, | |
-113, | |
-87, | |
-107, | |
-111, | |
-107, | |
-89, | |
-87, | |
-100, | |
-109, | |
-91, | |
-88, | |
-100, | |
-104, | |
-89, | |
-82, | |
-87, | |
-102, | |
-102, | |
-90, | |
-87, | |
-85, | |
-100, | |
-98, | |
-88, | |
-100, | |
-82, | |
-71, | |
-82, | |
-90, | |
-89, | |
-82, | |
-90, | |
-84, | |
-87, | |
-87, | |
-90, | |
-87, | |
-91, | |
-88, | |
-89, | |
-87, | |
-89, | |
-87, | |
-82, | |
-88, | |
-86, | |
-82, | |
-87, | |
-85, | |
-82, | |
-87, | |
-44, | |
-75, | |
-83, | |
-41, | |
-80, | |
-77, | |
-48, | |
-82, | |
-80, | |
-53, | |
-82, | |
-72, | |
-80, | |
-81, | |
-82, | |
-82, | |
-62, | |
-82, | |
-82, | |
-80, | |
-63, | |
-71, | |
-81, | |
-75, | |
-80, | |
-75, | |
-75, | |
-79, | |
-76, | |
-73, | |
-77, | |
-71, | |
-71, | |
-43, | |
-46, | |
-73, | |
-66, | |
-41, | |
-71, | |
-71, | |
-71, | |
-43, | |
-52, | |
-41, | |
-46, | |
-48, | |
-45, | |
-35, | |
-48, | |
-64, | |
-46, | |
-47, | |
-45, | |
-46, | |
-43, | |
-43, | |
-46, | |
-47, | |
-42, | |
-48, | |
-43, | |
-46, | |
-54, | |
-54, | |
-53, | |
-46, | |
-41, | |
-41, | |
-48, | |
-30, | |
-42, | |
-45, | |
-48, | |
-43, | |
-41, | |
-49, | |
-54, | |
-53, | |
-43, | |
-53, | |
-53, | |
-45, | |
-42, | |
-42, | |
-53, | |
-53, | |
-43, | |
-43, | |
-42, | |
-51, | |
-43, | |
-42, | |
-42, | |
-49, | |
-42, | |
-46, | |
-48, | |
-42, | |
-46, | |
-30, | |
-39, | |
-43, | |
-46, | |
-42, | |
-46, | |
-30, | |
-45, | |
-43, | |
-42, | |
-43, | |
-44, | |
-41, | |
-41, | |
-43, | |
-43, | |
-43, | |
-40, | |
-44, | |
-30, | |
-41, | |
-41, | |
-43, | |
-43, | |
-43, | |
-30, | |
-30, | |
-42, | |
-42, | |
-43, | |
-42, | |
-42, | |
-40, | |
-46, | |
-42, | |
-39, | |
-42, | |
-40, | |
-42, | |
-42, | |
-40, | |
-26, | |
-41, | |
-35, | |
-40, | |
-36, | |
-36, | |
-36, | |
-35, | |
-29, | |
-78, | |
-67, | |
-63, | |
-43, | |
-82, | |
-40, | |
-31, | |
-43, | |
-43, | |
-52, | |
-43, | |
-100, | |
-43, | |
-45, | |
-41, | |
-40, | |
-80, | |
-41, | |
-43, | |
-75, | |
-48, | |
-209, | |
-86, | |
-41, | |
-31, | |
-42, | |
-43, | |
-88, | |
-87, | |
-77, | |
-80, | |
-46, | |
-80, | |
-114, | |
-154, | |
-154, | |
-220, | |
-80, | |
-30, | |
-41, | |
-80, | |
-52, | |
-80, | |
-80, | |
-48, | |
-48, | |
-80, | |
-80, | |
-80, | |
-42, | |
-104, | |
-41, | |
-266, | |
-41, | |
-41, | |
-43, | |
-41, | |
-41, | |
-30, | |
-80, | |
-80, | |
-80, | |
-82, | |
-80, | |
-80, | |
-72, | |
-71, | |
-43, | |
-43, | |
-41, | |
-30, | |
-42, | |
-41, | |
-42, | |
-43, | |
-82, | |
-80, | |
-80, | |
-80, | |
-80, | |
-41, | |
-80, | |
-80, | |
-80, | |
-41, | |
-80, | |
-80]; | |
// A formatter for counts. | |
var formatCount = d3.format(",.0f"); | |
// Sets the margins | |
var margin = {top: 40, right: 30, bottom: 60, left: 30}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
// Round the minimum year down to the nearest 50 and the maximum year up to the nearest 50 | |
var max = Math.ceil(d3.max(years) / 50) * 50; | |
var min = Math.floor(d3.min(years) / 50) * 50; | |
var x = d3.scale.linear() | |
.domain([min, max]) | |
.range([0, width]); | |
// Generate a histogram using bins of length 25 years. | |
var data = d3.layout.histogram() | |
.bins(d3.range(min, max, 50)) | |
(years); | |
// Set the bar coloring | |
var yMax = d3.max(data, function(d){return d.length}); | |
var yMin = d3.min(data, function(d){return d.length}); | |
var colorScale = d3.scale.linear() | |
.domain([yMin, yMax]) | |
.range([d3.rgb(color).brighter(), d3.rgb(color).darker()]); | |
var y = d3.scale.linear() | |
.domain([0, yMax]) | |
.range([height, 0]); | |
// Ensure the axis uses 50 year increments | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom") | |
.tickValues(d3.range(min, max, 50)); | |
var svg = d3.select("body").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 bar = svg.selectAll(".bar") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "bar") | |
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; }); | |
bar.append("rect") | |
.attr("x", 1) | |
.attr("width", (x(data[0].dx) - x(0)) - 1) | |
.attr("height", function(d) { return height - y(d.y); }) | |
.attr("fill", function(d) { return colorScale(d.y) }); | |
bar.append("text") | |
.attr("dy", ".75em") | |
.attr("y", -12) | |
.attr("x", (x(data[0].dx) - x(0)) / 2) | |
.attr("text-anchor", "middle") | |
.text(function(d) { return formatCount(d.y); }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
// Graph title | |
svg.append("text") | |
.attr("x", (width / 2)) | |
.attr("y", 0 - (margin.top / 2)) | |
.attr("text-anchor", "middle") | |
.style("font-size", "16px") | |
.text("Violent Deaths per 50 Years"); | |
// Axis label | |
svg.append("text") | |
.attr("class", "x label") | |
.attr("text-anchor", "middle") | |
.attr("x", width / 2) | |
.attr("y", height + 50) | |
.text("Year"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment