Appearance
Charts > Bar chart
A bar chart uses rectangular bars to display the values of different categories or groups. The length or height of each bar corresponds to the quantity or value it represents.
The Chart
Market value of 10 top global brands in 2018, in billions of dollars
Download example CSV
Copy HTML
Playground
Animation
Duration of animation in seconds
Style
Color of bars
Color of labels
Color of compare marks
Use thinner bars
Show labels inside bars
Show name labels
Show value labels
Show value label right aligned inside bar
Show background of bar
Color of bar background
Round corners bar
Usage Example (with tooltip)
vue
<template>
<BarChart :data="data" @hover="setTooltip" class="list-none max-w-lg" />
<OTTooltip :info="tooltip"> {{ tooltip?.data?.name }} </OTTooltip>
</template>
<script setup>
import useTooltip from "@/functions/useTooltip.js";
const [tooltip, setTooltip] = useTooltip();
</script>Component API
The bar chart is built using the BarChart and BarChartItem components.
The changing of position of the bars is animated using the transition-group component with CSS transition classes (class name flip-list-move). The width of the bars is animated using GSAP in the BarChartItem component.
The BarChart exposes the BarChartItem component through a scoped slot, which you can use to render different content. For example a label, icon, or other component next to the bar.
To do this, grab the component from the slot props and render it using a dynamic component:
js
<BarChart v-slot="{ item, component }">
<component :is="component" >
</BarChart >Using the slot like this is only necessary if you want to show additional content, besides the bar. By default, it renders the bar component.
Data
js
[
{ id: "1", name: "Data 1", value: 60 },
...
]INFO
Optional: color, compare, compareColor
Data Shape for Uploadable CSV (example data)
| Name | value |
|---|---|
| Spain | 12 |
| France | 7 |
| Italy | 34 |
| Germany | 20 |
Props
| Name | Default | Type | Description |
|---|---|---|---|
data | [] | Array | The dataset |
max | null | Number | The value of a full width bar, computed from data if null |
color | #eee | String | Function | The default color for bars |
bgColor | null | String | The color for the background of the bar |
compareColor | #000 | String | Function | The default color for compare marks |
small | false | Boolean | Whether or not to use thin bars |
compact | false | Boolean | Whether or not to show labels inside the bars instead of above |
rounded | true | Boolean | Whether of not to round the corners of the bars |
showName | true | Boolean | Whether or not to show name labels |
showNumber | true | Boolean | Whether or not to show number labels |
numberRight | false | Boolean | Whether or not to align number labels to the right |
format | (d) => d.toFixed(0) + '%' | Function | The format function for number label |
duration | 0.5 | Number | The time of the animation in s |
Events
| Event | Argument | Description |
|---|---|---|
@select | { target, data } | Emitted when bar is clicked |
@hover | { target, data } | Emitted when bar is hovered |
Dependencies
- GSAP
- BarChartItem component