Skip to content

Instantly share code, notes, and snippets.

@adilapapaya
Last active January 6, 2016 16:27
Show Gist options
  • Save adilapapaya/534a3f28bea5b6053175 to your computer and use it in GitHub Desktop.
Save adilapapaya/534a3f28bea5b6053175 to your computer and use it in GitHub Desktop.
d3: Week of year axis formatting
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: sans-serif;
color: #444;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
font-size: 10px;
}
</style>
<body>
<h4>Week of Year Example</h4>
<div id="svg"></div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var hEach = 40; // height for each axis
var width = 960,
height = 2*hEach;
var x = d3.time.scale()
// month starts from 0!
.domain([new Date(2015, 11, 1), new Date(2016, 0, 30)])
.range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
// Week of year
var xAxis_woy = d3.svg.axis()
.scale(x)
.tickFormat(d3.time.weekOfYear)
.orient("bottom");
var svg = d3.select("#svg").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
var gx = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (5) + ")")
.call(xAxis);
var gx_woy = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (hEach) + ")")
.call(xAxis_woy);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment