Reputation: 329
I'm using d3 to build a visualisation which includes a modified version of the calendar example: http://mbostock.github.com/d3/ex/calendar.html.
I have turned my calendar by 90 degrees by changing the monthpath function:
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (d0) * z + "," + (w0 + 1) * z
+ "V" + w0 * z
+ "H" + 7 * z
+ "V" + w1 * z
+ "H" + (d1 + 1) * z
+ "V" + (w1 + 1) * z
+ "H" + 0
+ "V" + (w0 + 1) * z
+ "Z";
}
I would like to label the months, to the side of the calendar, at the start of each monthpath line. So I started by defining the month variable:
month = d3.time.format("%b"),
I am probably wrong but I am assuming I can insert my month variable into my monthpath function to print my labels? I'm trying to end up with a calendar similar to the one by Nathan Yau: http://flowingdata.com/2012/01/11/vehicles-involved-in-fatal-crashes/
Any ideas?
thanks
Upvotes: 3
Views: 3121
Reputation: 2141
This should do the trick - insert after the svg.selectAll("path.month")
block from the original example.
var month_name = d3.time.format("%B")
svg.selectAll("text.month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("text")
.attr("class", "month")
.attr("x", function(d) { return week(d) * cellSize; })
.attr("y", 8 * cellSize)
.text(month_name);
You will also need to modify the margins to make room for the labels:
var margin = {top: 19, right: 20, bottom: 40, left: 19},
width = 960 - margin.right - margin.left, // width
height = 156 - margin.top - margin.bottom, // height
cellSize = 17; // cell size
Upvotes: 4