Skip to content

Instantly share code, notes, and snippets.

@liufly
Created November 12, 2013 14:16
Show Gist options
  • Save liufly/7431511 to your computer and use it in GitHub Desktop.
Save liufly/7431511 to your computer and use it in GitHub Desktop.
Dual-scaled-D3-Bar-Charts
year money number
2005 550 35
2006 600 40
2007 700 45
2008 800 60
2009 900 70
2010 850 65
2011 880 67
2012 900 70
2013 1000 75
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.y.axisRight text {
fill: orange;
}
.y.axisLeft text {
fill: steelblue;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar1 {
fill: steelblue;
}
.bar2 {
fill: orange;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 80, right: 80, bottom: 80, left: 80},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y0 = d3.scale.linear().domain([300, 1100]).range([height, 0]),
y1 = d3.scale.linear().domain([20, 80]).range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
// create left yAxis
var yAxisLeft = d3.svg.axis().scale(y0).ticks(4).orient("left");
// create right yAxis
var yAxisRight = d3.svg.axis().scale(y1).ticks(6).orient("right");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("class", "graph")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("data.tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.year; }));
y0.domain([0, d3.max(data, function(d) { return d.money; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis axisLeft")
.attr("transform", "translate(0,0)")
.call(yAxisLeft)
.append("text")
.attr("y", 6)
.attr("dy", "-2em")
.style("text-anchor", "end")
.style("text-anchor", "end")
.text("Dollars");
svg.append("g")
.attr("class", "y axis axisRight")
.attr("transform", "translate(" + (width) + ",0)")
.call(yAxisRight)
.append("text")
.attr("y", 6)
.attr("dy", "-2em")
.attr("dx", "2em")
.style("text-anchor", "end")
.text("#");
bars = svg.selectAll(".bar").data(data).enter();
bars.append("rect")
.attr("class", "bar1")
.attr("x", function(d) { return x(d.year); })
.attr("width", x.rangeBand()/2)
.attr("y", function(d) { return y0(d.money); })
.attr("height", function(d,i,j) { return height - y0(d.money); });
bars.append("rect")
.attr("class", "bar2")
.attr("x", function(d) { return x(d.year) + x.rangeBand()/2; })
.attr("width", x.rangeBand() / 2)
.attr("y", function(d) { return y1(d.number); })
.attr("height", function(d,i,j) { return height - y1(d.number); });
});
function type(d) {
d.money = +d.money;
return d;
}
</script>
@liufly
Copy link
Author

liufly commented Nov 12, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment