There's nothing wrong with stack's x and y, it just takes a while to work out
how to use stack.
Imagine a vertically stacked bar chart. You need three variables to define this:
- the x axis values;
- the y axis values;
- the stacking category values, ie, how the stacks are grouped.
The x() and y() accessor functions tell stack where to get the x and y values.
If your values are actually the 'x' and 'y' variables in your data objects, then
you don't need to do anything. If they're not, and you don't define the
accessor functions, then everything breaks and you get undefines and NaNs
everywhere.
Your data has to be arranged as an array of arrays. Remember the vertically
stacked bar chart. The first array contains the values in the first row. The second
array is the second row, and so on. You split the data up using the
(grouping) stacking values. Each array has to be the same length, and the ordering
also has to be the same. Each array contains one value for each of your
possible x axis values.
All stack really does is generate y and y0 values. The first row gets all the y0 values
set to zero. The second row, the y0 values are zero plus the appropriate y value
in the first row, and so on.
Stack never writes to x, and it has no need to.
Using the vertical stacked bar chart example, y0 defines the base of each rectangle,
and y is the height.
You can also use stack to do horizontal stacking, you just need to set the x and y
accessor values the other way round, and remember that y0 actually means x0 ;)
Typically you use stack something like this:
var mylayers = stackValues.map(function(f) {
return data.filter(function(d) { return d.stackvalue == f; });
});
var mystack = d3.layout.stack()
.x(function(d) { return d.xvalue; })
.y(function(d) { return d.yvalue; })
.stack(mylayers);
And draw the shapes approximately like:
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr('x', xscale)
.attr('y', function(d) { return yscale(d.y0-d.y); })
.attr('width', mywidth)
.attr('height', function(d) { return yscale(d.y); });
Hope this helps.
- Dave
> --
> You received this message because you are subscribed to the Google Groups "d3-js" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
d3-js+un...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>
Dave Boyce
da...@iconomical.com
http://www.iconomical.com/