Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@robinhouston
Last active April 15, 2016 10:51
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 robinhouston/8663e60242e88dea64231a6fd014a49f to your computer and use it in GitHub Desktop.
Save robinhouston/8663e60242e88dea64231a6fd014a49f to your computer and use it in GitHub Desktop.
An axis with a date scale
height: 40
border: no

An axis that uses a date scale (d3.scaleTime) and has a specified number of ticks is apparently mislabelled.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>An axis with a date scale</title>
<script src="https://d3js.org/d3.v4.0.0-alpha.29.min.js"></script>
<style>
path.domain, .tick line { fill: none; stroke: black; }
</style>
</head>
<body>
<script>
var W = 600, M = 10;
var scale = d3.scaleTime().domain([ new Date(2012, 0, 1), new Date(2013, 0, 1) ]).range([0, W]),
axis = d3.axisBottom().scale(scale);
// If we do not specify a number of ticks, the axis is labelled with months as expected
axis.ticks(6);
d3.select(document.body)
.append("svg").attr("width", W + 2*M)
.append("g").attr("transform", "translate(" + M + "," + M + ")")
.call(axis);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment