Skip to content

Instantly share code, notes, and snippets.

@tgerard
Created September 13, 2015 06:23
Show Gist options
  • Select an option

  • Save tgerard/07ed0c08c406f649408f to your computer and use it in GitHub Desktop.

Select an option

Save tgerard/07ed0c08c406f649408f to your computer and use it in GitHub Desktop.
Tongariro visitors with scales
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tongariro National Park</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<link href="natparks.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>Visitors to Tongariro National Park</h1>
<h3>by calendar year</h3>
<script type="text/javascript">
var w = 700;
var h = 600;
var padding = [ 20, 20, 30, 50 ]; //Top, right, bottom, left
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1);
var xAxis = d3.svg.axis()
.scale(widthScale)
.innerTickSize(-h)
.outerTickSize(0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var thou = d3.format(",")
d3.csv("visitorsToNZNationalParks.csv", function(data) {
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.tongariro;
}) ]);
heightScale.domain(data.map(function(d) { return d.calendarYear; } ));
var tongariro = svg.selectAll("rect")
.data(data)
.enter();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
tongariro.append("rect")
.attr("class", "volcano")
.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.calendarYear);
})
.attr("width", 1)
.attr("height", heightScale.rangeBand())
.append("title")
.text(function(d) {
return d.calendarYear + " – " + thou(d.tongariro) + " visitors";
});
svg.selectAll(".volcano")
.transition()
.attr("width", function(d) {
return widthScale(d.tongariro);
})
.duration(1000)
.delay(function(d, i) {
return i * 50;
});
});
</script>
</script>
</body>
</html>
h1, .label {
font-family: verdana, sans-serif;
font-size: 18px;
margin-bottom: 0;
}
h1, h3 {
color: #56350c;
margin-left: 50px;
}
h3 {
font-family: verdana, sans-serif;
margin-top: 0;
font-size: 14px;
}
.label {
fill: #f2f6cb;
font-size: 10px;
}
.volcano {
fill: #56350c;
opacity: 0.9;
}
.volcano:hover {
fill: #af1e23;
opacity: 1;
}
.axis path, .axis line {
fill: none;
stroke: #ccc;
shape-rendering: crispEdges;
}
.axis text {
font-family: verdana, sans-serif;
fill: #56350c;
font-size: 11px;
}
.y.axis path, .y.axis line {
opacity: 0;
}
calendarYear abelTasman fiordland westland aorakiMtCook tongariro paparoa
1997 28800 196100 205500 154300 32100 11700
1998 31100 234500 207300 147100 41900 30100
1999 31700 205100 233900 135500 45500 33400
2000 54400 250300 278900 158400 48600 40500
2001 48200 239300 271500 143100 51800 40500
2002 57900 273000 280900 158100 55100 44400
2003 93000 308400 342300 157700 80800 71200
2004 94400 393700 362400 209900 96700 87700
2005 95800 409700 386000 201000 100300 98300
2006 96700 438000 372800 192200 95000 116700
2007 110700 439900 376700 172700 97800 97400
2008 119300 441200 379300 201800 83200 121200
2009 106800 385700 341400 170400 102100 130800
2010 120800 392700 357300 186600 110000 165800
2011 110500 371400 312100 146900 141500 127500
2012 95300 338700 288800 155700 114000 114200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment