Built with blockbuilder.org
Last active
December 8, 2015 12:23
-
-
Save quizzicol/6e2e9e8f3df7786d4f48 to your computer and use it in GitHub Desktop.
simple bar chart
This file contains hidden or 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
name | latitude | longitude | population | |
---|---|---|---|---|
Shanghai | 31.22222 | 121.45806 | 22315474 | |
Buenos Aires | -34.61315 | -58.37723 | 13076300 | |
Mumbai | 19.07283 | 72.88261 | 12691836 |
This file contains hidden or 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> | |
<head> | |
<meta charset="utf-8"> | |
<title>D3 Example</title> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<link href='http://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'> | |
<style> | |
.axis text { | |
font-family: 'Poiret One', cursive; | |
font-size: 16pt; | |
} | |
.axis .label { | |
font-size: 26pt; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.y.axis path, .y.axis line { | |
stroke: none; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
var outerWidth = 500; | |
var outerHeight = 250; | |
var margin = { left: 130, top: 0, right: 0, bottom: 60 }; | |
var barPadding = 0.2; | |
var xColumn = "population"; | |
var yColumn = "name"; | |
var xAxisLabelText = "Population"; | |
var xAxisLabelOffset = 55; | |
var innerWidth = outerWidth - margin.left - margin.right; | |
var innerHeight = outerHeight - margin.top - margin.bottom; | |
var svg = d3.select("body").append("svg") | |
.attr("width", outerWidth) | |
.attr("height", outerHeight); | |
var g = svg.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var xAxisG = g.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + innerHeight + ")") | |
var xAxisLabel = xAxisG.append("text") | |
.style("text-anchor", "middle") | |
.attr("x", innerWidth / 2) | |
.attr("y", xAxisLabelOffset) | |
.attr("class", "label") | |
.text(xAxisLabelText); | |
var yAxisG = g.append("g") | |
.attr("class", "y axis"); | |
var xScale = d3.scale.linear().range( [0, innerWidth]); | |
var yScale = d3.scale.ordinal().rangeBands([0, innerHeight], barPadding); | |
var xAxis = d3.svg.axis().scale(xScale).orient("bottom") | |
.ticks(5) // Use approximately 5 ticks marks. | |
.tickFormat(d3.format("s")) // Use intelligent abbreviations, e.g. 5M for 5 Million | |
.outerTickSize(0); // Turn off the marks at the end of the axis. | |
var yAxis = d3.svg.axis().scale(yScale).orient("left") | |
.outerTickSize(0); // Turn off the marks at the end of the axis. | |
function render(data){ | |
xScale.domain([0, d3.max(data, function (d){ return d[xColumn]; })]); | |
yScale.domain( data.map( function (d){ return d[yColumn]; })); | |
xAxisG.call(xAxis); | |
yAxisG.call(yAxis); | |
var bars = g.selectAll("rect").data(data); | |
bars.enter().append("rect") | |
.attr("height", yScale.rangeBand()); | |
bars | |
.attr("x", 0) | |
.attr("y", function (d){ return yScale(d[yColumn]); }) | |
.attr("width", function (d){ return xScale(d[xColumn]); }); | |
bars.exit().remove(); | |
} | |
function type(d){ | |
d.population = +d.population; | |
return d; | |
} | |
d3.csv("geonames_cities_top3.csv", type, render); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment