Calm motion: animations on this site only fade, with no movement
Bar chart
Bars for categories, side by side or stacked
Compares amounts across categories, for example plays by month. Use one series for one amount, more series for parts that you compare. For a trend over time, use the line chart. For parts of one whole, use the donut chart.
Basic
| Category | Web | App |
|---|---|---|
| Jun | 12k | 18k |
| Jul | 15k | 22k |
| Aug | 11k | 26k |
| Sep | 18k | 31k |
| Oct | 21k | 29k |
| Nov | 24k | 35k |
- Web
- App
vue
<template>
<BlessStack :gap="3">
<BlessStack direction="row" :gap="4" wrap>
<BlessSwitch v-model="stacked">Stacked</BlessSwitch>
<BlessSwitch v-model="horizontal">Horizontal</BlessSwitch>
</BlessStack>
<BlessChart
title="Plays"
description="Thousands, by month"
:legend="['Web', 'App']"
height="auto"
>
<BlessBarChart
:data
x="month"
:series
:stacked
:horizontal
:format="(v) => `${v}k`"
label="Plays by month"
/>
</BlessChart>
</BlessStack>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { BlessBarChart, BlessChart, BlessStack, BlessSwitch } from "blessing-ui";
// plays of a podcast in thousands, by month and by app
const data = [
{ month: "Jun", web: 12, app: 18 },
{ month: "Jul", web: 15, app: 22 },
{ month: "Aug", web: 11, app: 26 },
{ month: "Sep", web: 18, app: 31 },
{ month: "Oct", web: 21, app: 29 },
{ month: "Nov", web: 24, app: 35 },
];
const series = [
{ key: "web", label: "Web" },
{ key: "app", label: "App" },
];
const stacked = ref(false);
const horizontal = ref(false);
</script>datais a list of rows.xnames the field with the category, and each item ofseries({ key, label, color }) names a field with a number.- Bars grow from zero, so a negative value goes below the zero line.
stackedputs the series on each other: the positive parts go up and the negative parts go down. horizontalturns the chart: the categories go down the start edge and the bars grow to the side. Use it for long category names.- The value axis has round steps (1, 2, 2.5, or 5 × a power of ten) that cover the data. A hover on a bar shows its category, series, and value.
- Colors go through the chart colors in order, in the same order as the legend of
BlessChart. Setcoloron a series to a token name or any CSS color. - Put the chart in a
BlessChartwithheight="auto"for a title, a description, and a legend. - The chart draws at the width of its container, measured in the browser, and
heightsets the height (default 280). - The chart is hidden from screen readers, and a table of the values is read in its place.
labelnames the table.formatsets the text of a value. Change the name of the first column withlabelsor a label pack:category. - The chart is drawn as plain SVG. There is no chart library to install.
Usage
ts
import { BlessBarChart, type BlessChartSeries } from "blessing-ui";API
Props
| Name | Type | Default | Description |
|---|---|---|---|
data* | BlessChartRow[] | — | one row for each category |
x* | string | — | the field of a row that names its category |
series* | BlessChartSeries[] | — | one bar for each series in each category |
horizontal | boolean | — | bars that grow to the side, with the categories down the start edge |
stacked | boolean | — | the series on top of each other, not side by side |
height | number | 280 | |
format | ((v: number) => string) | — | text for one value, for example `(v) => v + " ms"` |
label | string | "Chart" | names the chart and its table for screen readers |
labels | Partial<{ category: string; }> | — |