Skip to content

Instantly share code, notes, and snippets.

@tmcw
Last active December 20, 2015 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/6101423 to your computer and use it in GitHub Desktop.
Save tmcw/6101423 to your computer and use it in GitHub Desktop.
{"0":115411,"2":54341,"4":21096,"6":6606,"8":3120,"10":2394,"12":1480,"14":1089,"16":899,"18":449,"20":203,"22":218,"24":371,"26":178,"28":186,"30":227,"32":838,"34":192,"36":105,"38":90,"-4":68330,"-6":44076,"-2":98821,"-8":27735,"-14":9803,"-12":13012,"-20":4115,"-18":4947,"-10":18875,"-16":7005,"-22":3394,"-30":1996,"-32":2259,"-34":1007,"-36":1989,"-38":1627,"-26":1888,"-24":2039,"-28":3194}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
margin:0;
padding:0;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar.ontime {
fill:#ff7f0e;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 640 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.ticks(5)
.orient("left");
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 + ")");
d3.json("deviation_histogram.json", function(error, list) {
var data = d3.entries(list).map(function(d) {
d.value = +d.value;
d.key = '' + d.key;
return d;
});
// x.domain(data.map(function(d) { return d.key; }));
x.domain(d3.range(-20, 20, 2));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Buses");
console.log(x(50), x(0));
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", 'bar')
.classed('ontime', function(d) {
return d.key == '0';
})
.attr("x", function(d) { return x(d.key); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment