This example demonstrates how to create grouped bars in a BarChart
by using the positionBy
property to segment bars by a secondary dimension (e.g., "color") and foregroundStyleBy
to apply distinct colors to each group. This pattern is useful when comparing subcategories within a larger category.
The dataset includes different object types (Cube
, Sphere
, Pyramid
) grouped by color (Green
, Purple
, Pink
, Yellow
). The chart displays the count of each shape per color, with grouped and color-coded bars.
positionBy
Groups bars by the specified value
(e.g., color).
The axis
indicates how the bars are positioned:
'horizontal'
: groups by Y-axis (stacked vertically within each color group).'vertical'
: groups by X-axis (used for transposed layouts).foregroundStyleBy
This chart will render vertical groups of bars by color (e.g., Green, Purple...), and each group will contain the respective shapes (Cube, Sphere, Pyramid) with the appropriate height and color.
This grouped bar layout is ideal for: