Skip to content

Instantly share code, notes, and snippets.

@CDEdata
Last active September 19, 2016 09:32
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 CDEdata/0686981a221e9b32c3bc4b8d1ea84459 to your computer and use it in GitHub Desktop.
Save CDEdata/0686981a221e9b32c3bc4b8d1ea84459 to your computer and use it in GitHub Desktop.
Science Comm 16: Bar chart adding legend

This code example is the result of the Module 3's exercise from the course “Data Visualization and Infographics with D3,” taught by Alberto Cairo and Scott Murray, and offered through the Knight Center for Journalism in the Americas at UT Austin. http://bl.ocks.org/nadeschda/ff6cac1d3d273584b1e3

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<style>
body {
font: 16px sans-serif;
font-weight: bold;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: purple;
}
.axis {
font: 10px sans-serif;
}
.axis path, /* path refers to a line */
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.title {
font: 20px sans-serif;
font-weight: bold;
}
/*css for legend*/
div.legend-container {
position: absolute;
left: 550px;
top: 30px;
background-color: #fff;
}
.legend {
stroke: #d9d9d9;
stroke-width: 2;
fill: none;
}
p {
width: 300px;
font-size: 11px;
color: #7b4173;
margin: 10px;
position: absolute;
top: 70px;
}
a {
text-decoration: none;
color: #969696;
display: block;
margin: 10px 0;
}
</style>
</head>
<body>
<script>
var margin = {
top: 30,
left: 65,
right: 10,
bottom: 50
}
height = 400 - margin.top - margin.bottom
width = 550 - margin.left - margin.right
var svg = d3.select("body").append("svg")
.attr("height", height + margin.top + margin.bottom)
.attr("width", width + margin.left + margin.right)
.append("g") //investigate the effects of removing this
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//xAxis
var xScale = d3.scale.ordinal()
.rangeRoundBands([width,0], .1);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yScale = d3.scale.linear()
.range([height,0])
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
//lEGEND
legendDIV = d3.select("body")
.append("div")
.attr("class", "legend-container");
p = legendDIV.append("p")
.text("Net receipts from transit trading in Switzerland grew tenfold between 2003 and 2011, from CHF 2 billion to CHF 20 billion. In 2010, trading replaced Swiss banks’ financial services as the country’s top services export, and its share of Swiss GDP (over 3%) eclipsed that of tourism.");
p.append("a")
.attr("href", "https://de.wikipedia.org/wiki/Transithandel")
.attr("target", "_blank")
.text("What is transit trade?");
p.append("a")
.attr("href", "http://www.snb.ch/en/iabout/stat/statpub/bop/id/statpub_bop_desc")
.attr("target", "_blank")
.text("Link to data source");
legendSVG = legendDIV.append("svg")
.attr("width", width / 3)
.attr("height", height / 2)
.style("background-color", "transparent")
.append("g");
legendSVG.append("polyline")
.attr("points", "22,14 2,14 2,290")
.attr("class", "legend");
legendSVG.append("text")
.attr("x", 26)
.attr("y", 20)
.text(function(d) {
return "Legend";
})
.attr("font-size", "16");
legendSVG.append("rect")
.attr("class", "bar")
.attr("x", 10)
.attr("y", 40)
.attr("width", 20)
.attr("height", 20);
legendSVG.append("text")
.attr("x", 35)
.attr("y", 56)
.text(function(d) {
return "transit trade";
})
.attr("font-size", "12");
d3.csv("ttrade.csv",type, function(data) {
xScale.domain(data.map(function(d) {return d.year;}));
yScale.domain([0, d3.max(data, function(d) { return +d.ttrade; } )]); //or use yScale.domain([0, 20000]);
//xAxis
svg.append("g")
.attr("class","x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-35)");
//yAxis
svg.append("g")
.attr("class","y axis")
.call(yAxis);
//x axis label
svg.append("text")
.attr("x", width/2)
.attr("y", height + margin.bottom)
.text("Year");
//y axis label
svg.append("text")
.attr("transform", "rotate(-90)")
//NOTE - we actually rotate our reference point by 90 degrees
//http://www.d3noob.org/2012/12/adding-axis-labels-to-d3js-graph.html gives more detail
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Revenue in Mio. CHF");
//title
svg.append("g")
.attr("class","title")
.append("text")
.attr("x", width/2)
.attr("y", (margin.top / 2))
.attr("text-anchor", "middle")
.text("Commodity transit trade")
svg.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d) {return xScale(d.year);})
.attr("y", function(d) {return yScale(d.ttrade); })
.attr("width",xScale.rangeBand())
.attr("height", function (d) {return height - yScale(d.ttrade); })
});
function type(d) {
d.ttrade = d.ttrade;
return d;
}
</script>
</body>
</html>
year ttrade
2011 19766
2010 19827
2009 14422
2008 14445
2007 10716
2006 8811
2005 5862
2004 4552
2003 2444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment