sszvis.stackedBarHorizontal
Data structure
The stackedBarHorizontal component requires an array of arrays, where each array represents a slice of the stack. Each slice should be an array consist of two values for the x0 and x1 properties, followed a data object, series and stack properties.
const stackedData = [
[
[0,10, data: {...}, series: "key1", stack: "A"],
[0,8, data: {...}, series: "key1", stack: "B"],
[0,16, data: {...}, series: "key1", stack: "C"],
key: "key1"
],
[
[10,16, data: {...}, series: "key2", stack: "A"],
[8,20, data: {...}, series: "key2", stack: "B"],
[16,18, data: {...}, series: "key2", stack: "C"],
key: "key2"
],
]sszvis.stackedBarHorizontalLayout returns those series inside an object, alongside the metadata:
const layout = {
series: stackedData,
keys: ["key1", "key2"],
maxValue: 20,
minValue: 0,
};Caution
Because it uses a d3.stack layout under the hood, this component will assign two special values to each data point passed to it: 0, the baseline value for each point, and 1, the extent of each point. This assignment will overwrite any existing properties on the data object named 0 or 1. These are intermediate values used to compute the bars' positioning in stack, and will be assigned even though the stack is horizontal. In the end, these values are used to calculate x positions and bar widths.
Configuration
In order to construct a stacked data structure we use sszvis.stackedBarHorizontalLayout to build a generator function that takes the data and returns the stacked data. The generator function is then called with the data to get the stacked data. It takes the y, c, and x accessors as arguments and returns an object with the series in .series and the aggregated .keys, .maxValue and .minValue beside them.
sszvis.stackedBarHorizontalData is the older form of the same generator. It returns the series array itself, with .maxValue and .minValue assigned onto it - deprecated, because every array operation drops them - and no .keys.
const stackLayout = sszvis.stackedBarHorizontalLayout(yAcc, cAcc, xAcc);
const layout = stackLayout(data);
state.stackedData = layout.series;
state.categories = layout.keys;
state.maxStacked = layout.maxValue;stackedBarHorizontal.xAccessor(xAcc)
Specifies an x-accessor for the stack layout. The result of this function is used to compute the horizontal extent of each element in the stack. The return value must be a number.
stackedBarHorizontal.xScale(xScale)
Specifies an x-scale for the stack layout. This scale is used to position the elements of each stack, both the left offset value and the width of each stack segment.
stackedBarHorizontal.width(width)
Specifies a width for the bars in the stack layout. This value is not used in the horizontal orientation. (xScale is used instead).
stackedBarHorizontal.yScale(yScale)
A y-scale. After the stack is computed, the y-scale is used to position each stack.
stackedBarHorizontal.height(height)
Specify the height of each rectangle. This value determines the height of each element in the stacks.
stackedBarHorizontal.fill([fill])
Specify a fill value for the rectangles (default black).
stackedBarHorizontal.stroke([stroke])
Specify a stroke value for the stack rectangles. Defaults to a white one-pixel line, which separates touching stack segments. Pass "none" for seamless stacks, or null to leave the stroke unset entirely.
stackedBarHorizontal.transition([transition])
Whether to animate the rectangles into their new geometry. Defaults to true. Pass false to write each render synchronously, which is what you want when another component drives the timing.
Chart
In this example, twelve distinct categories make up each stack, and the total for each stack is 100.