The documents for the d3 d3.stack stacking function show an example with an array of objects (each json object representing a collection of points for any x axis is measured). For instance:
var data = [ {month: new Date(2015, 0, 1), apples: 3840, bananas: 1920, cherries: 960}, {month: new Date(2015, 1, 1), apples: 1600, bananas: 1440, cherries: 720} ]
I am trying to create a complex histogram with a matrix of data series ( [ [], [], [], etc ] ). It is easy enough to iterate through the lines and get a series of histograms (after setting the x scale and domain in another place):
for(let i=0; i<data.length; i++){ bins[i] = d3.histogram() .domain(x.domain()) .thresholds(x.ticks(10)) (data[i]); }
And create groups for each data series inside another loop:
let bars = this.svg.selectAll(".series" + i) .data(this.bins[i]) .enter().append("g") .classed("series" + i, true)
But of course, I do it in such a way that I'm stuck here. How can I bars.append("rect") find the correct x, y coordinates for this particular series? In other words, I have a really useful array of bins at the moment, looking something like this:
[ [[1,2,3,3], [5,8,9], [10], ... etc], //series0 grouping by bins of 5 [[1,3], [7,7,9,9], [11], ... etc], //series1 [[2,3,3], [8,9], [10,12], ... etc], //series2 ...etc ]
Is there a way to call stack without iterating over all the data in the json key, a pair of values?