Leave Yamada UI a star

Star
Yamada UIYamada UIv1.5.0

BarChart

BarChart is a component for drawing bar charts to compare multiple sets of data.

Source@yamada-ui/charts

Import

pnpm add @yamada-ui/charts
Copied!
import { BarChart } from "@yamada-ui/charts"
Copied!

Usage

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return <BarChart data={data} series={series} dataKey="name" />
Copied!

Change Size

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart data={data} series={series} dataKey="name" size="sm" />
    <BarChart data={data} series={series} dataKey="name" size="md" />
    <BarChart data={data} series={series} dataKey="name" size="lg" />
    <BarChart data={data} series={series} dataKey="name" size="full" />
  </VStack>
)
Copied!

Stack

To stack, set type to "stacked".

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return <BarChart data={data} series={series} dataKey="name" type="stacked" />
Copied!

Additionally, to stack individually, set a common string to stackId.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500", stackId: "stackId" },
    { dataKey: "ぼうぎょ", color: "blue.500", stackId: "stackId" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return <BarChart data={data} series={series} dataKey="name" />
Copied!

Convert to Percentage

To stack as a percentage, set type to "percent".

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return <BarChart data={data} series={series} dataKey="name" type="percent" />
Copied!

Change Direction

To change the direction, set layoutType to "horizontal" or "vertical". The default is "horizontal".

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <BarChart data={data} series={series} dataKey="name" layoutType="vertical" />
)
Copied!

Display Legend

To display the legend, set withLegend to true. To change the position of the legend, set legendProps.verticalAlign to "bottom" or similar.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart data={data} series={series} dataKey="name" withLegend />

    <BarChart
      data={data}
      series={series}
      dataKey="name"
      withLegend
      legendProps={{ verticalAlign: "bottom", mb: "0", mt: "4" }}
    />
  </VStack>
)
Copied!

Display Label

To display label, set xAxisLabel or yAxisLabel with a string.

Editable example

const data = useMemo(
  () => [
    {
      name: "ゼニガメ",
      HP: 44,
      こうげき: 48,
      ぼうぎょ: 65,
      とくこう: 50,
      とくぼう: 64,
      すばやさ: 43,
    },
    {
      name: "ヒトカゲ",
      HP: 39,
      こうげき: 52,
      ぼうぎょ: 43,
      とくこう: 60,
      とくぼう: 50,
      すばやさ: 65,
    },
    {
      name: "フシギダネ",
      HP: 45,
      こうげき: 49,
      ぼうぎょ: 49,
      とくこう: 65,
      とくぼう: 65,
      すばやさ: 45,
    },
  ],
  [],
)

const series: AreaProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <BarChart
    data={data}
    series={series}
    dataKey="name"
    xAxisLabel="ポケモン"
    yAxisLabel="ステータス"
  />
)
Copied!

Display Reference Line

To display a reference line, set referenceLineProps with props.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <BarChart
    data={data}
    series={series}
    dataKey="name"
    referenceLineProps={[{ y: 100, label: "Avg.", color: "red.500" }]}
  />
)
Copied!

Hide Tooltip

To hide the tooltip, set withTooltip to false. The default is true.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <BarChart data={data} series={series} dataKey="name" withTooltip={false} />
)
Copied!

Hide Axis

To hide the axis, set withYAxis or withXAxis to false. The default is true.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart data={data} series={series} dataKey="name" withYAxis={false} />
    <BarChart data={data} series={series} dataKey="name" withXAxis={false} />
  </VStack>
)
Copied!

Change Grid Layout

To change the grid layout, set gridAxis to "x", "y", "xy", or "none".

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart data={data} series={series} dataKey="name" gridAxis="x" />
    <BarChart data={data} series={series} dataKey="name" gridAxis="y" />
    <BarChart data={data} series={series} dataKey="name" gridAxis="xy" />
    <BarChart data={data} series={series} dataKey="name" gridAxis="none" />
  </VStack>
)
Copied!

Mark Ticks

To mark ticks, set tickLine to "x", "y", "xy", or "none".

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart data={data} series={series} dataKey="name" tickLine="x" />
    <BarChart data={data} series={series} dataKey="name" tickLine="y" />
    <BarChart data={data} series={series} dataKey="name" tickLine="xy" />
    <BarChart data={data} series={series} dataKey="name" tickLine="none" />
  </VStack>
)
Copied!

Synchronize

To synchronize the behavior of the charts, set a common string to syncId of each chart.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart data={data} series={series} dataKey="name" syncId="syncId" />
    <BarChart data={data} series={series} dataKey="name" syncId="syncId" />
  </VStack>
)
Copied!

Add Unit

To add a unit, set a string to unit.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return <BarChart data={data} series={series} dataKey="name" unit="P" />
Copied!

Format

To format tooltip labels, use labelFormatter, and to format values, use valueFormatter.
To format axis labels, use xAxisTickFormatter or yAxisTickFormatter.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <VStack>
    <BarChart
      data={data}
      series={series}
      dataKey="name"
      labelFormatter={(value) => `ポケモン: ${value}`}
      valueFormatter={(value) => `${value}P`}
    />
    <BarChart
      data={data}
      series={series}
      dataKey="name"
      xAxisTickFormatter={(value) => `ポケモン: ${value}`}
      yAxisTickFormatter={(value) => `${value}P`}
    />
  </VStack>
)
Copied!

Adjust Opacity

To adjust opacity, set a number or an array of numbers to fillOpacity.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <BarChart
    data={data}
    series={series}
    dataKey="name"
    fillOpacity={[0.8, 0.7]}
  />
)
Copied!

Adjust Dashed Line

To adjust the dashed line, set a string to strokeDasharray.

Editable example

const data = useMemo(
  () => [
    {
      name: "エンテイ",
      HP: 115,
      こうげき: 115,
      ぼうぎょ: 85,
      とくこう: 90,
      とくぼう: 75,
      すばやさ: 100,
    },
    {
      name: "ライコウ",
      HP: 90,
      こうげき: 85,
      ぼうぎょ: 75,
      とくこう: 115,
      とくぼう: 100,
      すばやさ: 115,
    },
    {
      name: "スイクン",
      HP: 100,
      こうげき: 75,
      ぼうぎょ: 115,
      とくこう: 90,
      とくぼう: 115,
      すばやさ: 85,
    },
  ],
  [],
)

const series: BarProps[] = useMemo(
  () => [
    { dataKey: "HP", color: "green.500" },
    { dataKey: "こうげき", color: "red.500" },
    { dataKey: "ぼうぎょ", color: "blue.500" },
    { dataKey: "とくこう", color: "purple.500" },
    { dataKey: "とくぼう", color: "orange.500" },
    { dataKey: "すばやさ", color: "cyan.500" },
  ],
  [],
)

return (
  <BarChart
    data={data}
    series={series}
    dataKey="name"
    strokeDasharray="15 15"
  />
)
Copied!

Edit this page on GitHub

PreviousBadgeNextCalendar