Skip to content
On this page

Charts > Stacked bar chart ​

A stacked bar chart that offers the flexibility to view sections grouped together or separated into individual columns.

The Chart ​

Download example CSV
Copy HTML

Playground ​

Data

Number of bars
Number of categories
Show a compare mark when bars are separated

Style

Color for each segment (separate with comma)
Background color for bars in non-stacked state
Align labels on the left
Rounded corners
Add a white border between segments

Usage ​

The stacked bar chart is built using html and can transform into separate bar charts using the stacked prop.

The changing of position of the bars is animated using the transition-group component with CSS transition classes (class name flip-list-delay-move). The width of each segment is animated using the Motion One library.

For the non-stacked state, you can control the value of a full width bar using the max prop. This can be a fixed number or an array with a different number for each segment. If no value is given, the component uses the max value in the dataset.

Component API ​

Data ​

js
[
  {
    id: "data-1",
    name: "Data 1",
    values: {
      val1: { id: "val1", name: "val 1", value: 60 },
      val2: { id: "val2", name: "val 2", value: 40 },
    },
  },
  ...
]

Optional: color and compare on value object

Props ​

NameDefaultDescriptionType
data[]The datasetArray
maxnullThe value of a full width bar in non-stacked stateNumber | Array
color{ val1: '#eee', val2: '#ccc' }The default color for each segmentObject
bgColor#f3f4f6The color for background of bar in non-stacked stateString
stackedtrueWhether or not to show stacked bars or separate barchartsNumber
textLeftfalseWhether or not to align labels on the leftBoolean
roundedtrueWhether of not to round the corners of the barsBoolean
whiteBordertrueWhether or not to use a white border between segmentsBoolean
showValuestrueWhether or not to show values on hoverBoolean
minValueForLabel4The minimum value for a segment to show a labelNumber

Events ​

NameArgumentDescription
@select{ target, data }Emitted when bar is clicked
@hover{ target, data } | nullEmitted when bar is hovered

Dependencies ​

  • Motion One