Bar chart (vertical, grouped)
Grouped bar charts are suited to making direct comparisons between multiple sets of similar things, while also comparing those sets using another differentiating dimension.
sszvis.groupedBarsVertical
Also available as sszvis.groupedBars() for backward compatibility. For horizontal grouped bars, see sszvis.groupedBarsHorizontal().
Data structure
This chart requires an array of arrays, grouped by the desired dimension. Each group should be an array of objects, where each object represents a bar in the group.
const data = [
[
{ category: "A", value: 10 },
{ category: "B", value: 20 },
{ category: "C", value: 30 },
],
[
{ category: "A", value: 15 },
{ category: "B", value: 25 },
{ category: "C", value: 35 },
],
];Configuration
In order to construct a grouped bar chart, we can use the sszvis.cascade() function to group the data by the desired dimension. The data is then mapped to the grouped data structure.
state.groupedData = sszvis.cascade().arrayBy(xAcc).apply(state.data);groupedBarsVertical.groupScale(scale)
This should be a scale function for determining the correct group offset of a member of a group. This function is passed the group member, and should return a value for the group offset which is the same for all members of the group. The within-group offset (which is different for each member) is then added to this group offset in order to position the bars individually within the group. So, for instance, if the groups are based on the "city" property, the groupScale should return the same value for all data objects with "city = Zurich".
groupedBarsVertical.groupSize(size)
This property tells groupedBarsVertical how many bars to expect for each group. It is used to assist in calculating the within-group layout and size of the bars. This number is treated as the same for all groups. Groups with less members than this number will have visible gaps. (Note that having less members in a group is not the same as having a member with a missing value, which will be discussed later)
groupedBarsVertical.groupWidth(width)
The width of the groups. This value is treated as the same for all groups. The width available to the groups is divided up among the bars. Often, this value will be the result of calling .rangeBand() on a d3.scale.ordinal scale.
groupedBarsVertical.groupSpace(space)
The percentage of space between each group. (default: 0.05). Usually the default is fine here.
groupedBarsVertical.y(yPosition)
The y-position of the bars in the group. This function is given a data value and should return a y-value. It should be similar to other functions you have already seen for positioning bars.
groupedBarsVertical.height(height)
The height of the bars in the group. This function is given a data value and should return a height value. It should be similar to other functions you have already seen for setting the height of bars.
groupedBarsVertical.fill([fill])
A functor which gives the color for each bar (often based on the bar's group). This can be a string or a function.
groupedBarsVertical.stroke([stroke])
The stroke color for each bar (default: none)
groupedBarsVertical.defined([isDefined])
A predicate function which can be used to determine whether a bar has a defined value. (default: true). Any bar for which this function returns false, meaning that it has an undefined (missing) value, will be displayed as a faint "x" in the grouped bar chart. This is in order to distinguish bars with missing values from bars with very small values, which would display as a very thin rectangle.