Skip to content

Commit

Permalink
duplicate small-multiple-column-timeline as starting point
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbernard committed Feb 8, 2017
1 parent 6523082 commit f43ed71
Show file tree
Hide file tree
Showing 9 changed files with 946 additions and 0 deletions.
171 changes: 171 additions & 0 deletions small-multiples-column-category/chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@

function makeChart(data, stylename, media, plotpadding, legAlign, yAlign, numberOfColumns, numberOfRows, yMin, yMax, yAxisHighlight, numTicksy, xAxisTickFormat){

var titleYoffset = d3.select("#"+media+"Title").node().getBBox().height
var subtitleYoffset = d3.select("#"+media+"Subtitle").node().getBBox().height;

// return the series names from the first row of the spreadsheet
var seriesNames = Object.keys(data[0]).filter(function(d){ return d != 'date'; });

//Select the plot space in the frame from which to take measurements
var frame = d3.select("#" + media + "chart");
var plot = d3.select("#" + media + "plot");

var yOffset = d3.select("#" + media + "Subtitle").style("font-size");
yOffset = Number(yOffset.replace(/[^\d.-]/g, ''));

// console.log(yOffset)

//Get the width,height and the marginins unique to this chart
var w = plot.node().getBBox().width;
var h = plot.node().getBBox().height - yOffset;
var margin = plotpadding.filter(function(d){
return (d.name === media);
});
margin = margin[0].margin[0]
var colours= d3.scale.ordinal()
.domain([0,0])
.range(stylename.fillcolours);


//CREATE THE PLOT WIDTHS, BUT FOR EACH INDIVIDUAL GRAPH
var plotWidth = ((w - (yOffset * numberOfColumns))/numberOfColumns);
var plotHeight = (h/numberOfRows)-(margin.top + margin.bottom);

console.log(w,plotWidth, w/plotWidth);
// calculate maximum and minimum for the y-axis
if(!yMin || !yMax){
data.forEach(function(d,i){
seriesNames.forEach(function(e){
if(i==0) yMin = yMax = Number(d[e]);
yMin = Math.min(yMin, Number(d[e]));
yMax = Math.max(yMax, Number(d[e]));
});
});
}

var xDomain = d3.extent(data, function(d) {return d.date;});


var yScale = d3.scale.linear()
.range([plotHeight, 0])
.domain([yMin,yMax])

var yAxis = d3.svg.axis()
.scale(yScale)
.ticks(numTicksy)
.orient("right")
.tickFormat(function(d){
return d/divisor;
});

// set the first and last date ticks for the x-axis
var minDate = data[0].date,
maxDate = data[data.length - 1].date;



var smallMultiple = plot.selectAll('g')
.data(seriesNames)
.enter()
.append('g')
.attr({
'transform': function(d, i) {
var yPos = yOffset + Number((Math.floor( i / numberOfColumns) * (plotHeight + margin.top + margin.bottom + 4) + margin.top));
var xPos = i % numberOfColumns;
return 'translate(' + ((plotWidth + yOffset *1.3) * xPos) + ',' + yPos + ')';
},
'id':function(d){ return d; },
'xPosition': function (d,i) {
xPos = i%numberOfColumns;
return xPos;
}
});

smallMultiple.append('text')
.attr({
'class':media + 'item-title',
'dx': function() {return (plotWidth-((yOffset/2) * numberOfColumns))/2;},
'dy': function() {return -yOffset/numberOfColumns;},
})
.text(function(d) {return d.toUpperCase(); });

var yLabel = smallMultiple.append("g")
.attr("class", media+"yAxis")
.call(yAxis)
// calculate what the ticksize should be now that the text for the labels has been drawn
var yLabelOffset=yLabel.node().getBBox().width
var yticksize=colculateTicksize(yAlign,yLabelOffset);

yLabel.call(yAxis.tickSize(yticksize))

yLabel
.attr("transform",function(){
if (yAlign=="right"){
return "translate(0,"+margin.top+")"
}
else return "translate("+(plotWidth-margin.right)+","+margin.top+")"
})

yLabel.selectAll('text')
.attr("style", null)
.attr("x",yticksize+(yLabelOffset*.8))

//identify 0 line if there is one
var originValue = 0;
var origin = plot.selectAll(".tick").filter(function(d, i) {
return d==originValue || d==yAxisHighlight;
}).classed(media+"origin",true);

var xScale = d3.time.scale()
.domain(xDomain)
.range([0, (plotWidth - (yLabelOffset * 1.4))]);

var xAxis = d3.svg.axis()
.scale(xScale)
.tickSize(yOffset/2)
.orient("bottom")
.tickFormat(xAxisTickFormat)
.tickValues([minDate,maxDate]);

smallMultiple.append('g')
.each(function(seriesNames){
var bars = d3.select(this).selectAll('rect');
bars.data(data)
.enter()
.append('rect')
.style("fill", function (d) {
return colours(0)
})
.attr("id",function(d) { return d.date+"-"+d[seriesNames]; })
.attr("class",media+"fill")
.attr("width", (plotWidth - ((yOffset/2) * numberOfColumns))/data.length - 2)
.attr("x", function(d) { return xScale(d.date); })
.attr("y", function(d) { return yScale(Math.max(0, d[seriesNames]))})
.attr("height", function(d) {return (Math.abs(yScale(d[seriesNames]) - yScale(0))); })
.attr("transform",function(){
if(yAlign=="right") {
return "translate(2,"+(margin.top)+")"
}
else {return "translate("+(margin.left+yLabelOffset)+","+(margin.top)+")"}
})
})
var xLabel=smallMultiple.append("g")
.attr("class",media+"xAxis")
.attr("transform",function(){
if(yAlign=="right") {
return "translate("+d3.select('.' + media + 'fill').node().getBBox().width/1.3+","+(plotHeight+margin.top)+")"
}
else {return "translate("+(margin.left+yLabelOffset)+","+(plotHeight+margin.top)+")"}
})
.call(xAxis);
//you now have a chart area, inner margin data and colour palette - with titles pre-rendered

function colculateTicksize(align, offset) {
if (align=="right") {
return plotWidth-margin.left - offset + 2
}
else {return plotWidth-margin.right - offset}
}

}
5 changes: 5 additions & 0 deletions small-multiples-column-category/d3.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions small-multiples-column-category/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
date,x31-Jan-10,8.7526-Feb-10,8.7531-Mar-10,8.7530-Apr-10,9.531-May-10,9.530-Jun-10,10.2530-Jul-10,10.7531-Aug-10,10.7530-Sep-10,10.7529-Oct-10,10.7530-Nov-10,10.7531-Dec-10,10.7531-Jan-11,11.2528-Feb-11,11.2531-Mar-11,11.7529-Apr-11,1231-May-11,1230-Jun-11,12.2529-Jul-11,12.531-Aug-11,12.530-Sep-11,1231-Oct-11,11.530-Nov-11,11.530-Dec-11,1131-Jan-12,10.529-Feb-12,10.530-Mar-12,9.7530-Apr-12,931-May-12,8.529-Jun-12,8.531-Jul-12,831-Aug-12,7.528-Sep-12,7.531-Oct-12,7.2530-Nov-12,7.2531-Dec-12,7.2531-Jan-13,7.2528-Feb-13,7.2529-Mar-13,7.2530-Apr-13,7.531-May-13,828-Jun-13,831-Jul-13,8.530-Aug-13,930-Sep-13,931-Oct-13,9.529-Nov-13,1031-Dec-13,1031-Jan-14,10.528-Feb-14,10.7531-Mar-14,10.7530-Apr-14,1130-May-14,1130-Jun-14,1131-Jul-14,1129-Aug-14,1130-Sep-14,1131-Oct-14,11.2528-Nov-14,11.2531-Dec-14,11.7530-Jan-15,12.2527-Feb-15,12.2531-Mar-15,12.7530-Apr-15,13.2529-May-15,13.2530-Jun-15,13.7531-Jul-15,14.2531-Aug-15,14.2530-Sep-15,14.2530-Oct-15,14.2530-Nov-15,14.2531-Dec-15,14.2529-Jan-16,14.2529-Feb-16,14.2531-Mar-16,14.2529-Apr-16,14.25
Expand Down
Loading

0 comments on commit f43ed71

Please sign in to comment.