Skip to content

Instantly share code, notes, and snippets.

@bunkat
Created October 7, 2012 19:51
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 bunkat/3849376 to your computer and use it in GitHub Desktop.
Save bunkat/3849376 to your computer and use it in GitHub Desktop.
Round date scale
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v2.min.js?2.9.6"></script>
<script>
var margin = {top: 100, right: 100, bottom: 100, left: 100},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var data = [
{date: new Date(2011,11,31), amount: 200},
{date: new Date(2012,0,1), amount: 100},
{date: new Date(2012,0,2), amount: 300},
{date: new Date(2012,0,3), amount: 400},
{date: new Date(2012,1,1), amount: 400}
];
var month = data.filter(function (d) { return d.date.getMonth() === 0; });
var x = d3.time.scale()
.domain(d3.extent(month, function(d) { return d.date; }))
.range([0, width])
.nice(d3.time.month);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.days,7)
.tickFormat(d3.time.format('%b %d'));
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment