Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbostock/1071269 to your computer and use it in GitHub Desktop.
Save mbostock/1071269 to your computer and use it in GitHub Desktop.
Date Ticks
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis text {
font: 10px sans-serif;
}
.axis line,
.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var duration = 2500,
delay = 500;
var width = 960,
height = 500;
var x = d3.time.scale()
.domain([new Date(2010, 0, 2), new Date(2010, 1, 1)])
.range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(6, 0)
.tickFormat(d3.time.format("%m/%d"));
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height / 2 + ")")
.call(xAxis);
setInterval(update, duration + delay);
function update() {
x.domain([new Date(2010, 0, 2), new Date(2010, 0, Math.floor(Math.random() * 21) + 10)]);
g.transition()
.duration(duration)
.call(xAxis);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment