Skip to Content
文档

柱状图

用于使用不同高度或长度的矩形条显示分类数据

StorybookRecharts

用法

¥Usage

import { Chart, useChart } from "@chakra-ui/charts"
import { Bar, BarChart, CartesianGrid, XAxis, YAxis } from "recharts"
<Chart.Root>
  <BarChart>
    <CartesianGrid />
    <XAxis />
    <YAxis />
    <Bar />
  </BarChart>
</Chart.Root>

示例

¥Examples

条形颜色

¥Bar color

以下是根据数据为条形着色的示例。

¥Here's an example of coloring the bars based on the data.

使用 recharts 中的 Cell 组件为柱状图着色。

<Bar dataKey="allocation">
  <Cell fill="red" />
</Bar>

条形标签

¥Bar Label

渲染来自 rechartsLabelList 组件以显示柱状图的标签。

¥Render the LabelList component from recharts to display the label of the bar.

格式化程序

¥Formatter

使用 useChart 钩子提供的格式化程序格式化数值轴。

¥Use the formatter provided from the useChart hook to format the value axis.

<YAxis
  tickFormatter={chart.formatNumber({
    style: "currency",
    currency: "USD",
    notation: "compact",
  })}
/>

无间隙

¥No Gap

要消除条形之间的间隙,请在 BarChart 组件上将 barCategoryGap 属性设置为 0

¥To remove the gap between the bars, set the barCategoryGap prop to 0 on the BarChart component.

填充内容值

¥Fill With Value

组合 recharts 中的 LabelListCell 组件,使其根据值向上或向下渲染柱状图。

¥Compose the LabelList and Cell components from recharts to render bars upward or downward based on the value.

<Bar dataKey="views">
  <LabelList dataKey="views" position="top" />
  {chart.data.map((item, index) => (
    <Cell key={index} fill={item.views > 0 ? "green" : "red"} />
  ))}
</Bar>

水平

¥Horizontal

layout="vertical" 属性传递给 BarChart 组件,以水平方式渲染柱状图。

¥Pass the layout="vertical" prop to the BarChart component to render the bars horizontally.

圆角

¥Rounded

radius 属性传递给 Bar 组件,以圆角方式渲染柱状图。

¥Pass the radius prop to the Bar component to render the bars with rounded corners.

范围

¥Range

将一个值数组传递给 dataKey 属性,将会渲染一个范围栏,指示值的上下限。

¥Passing an array of values to the dataKey prop will render a range bar that indicates the lower and upper bounds of the values.

const chart = useChart({
  data: [
    { name: "UK", value: [10, 20] },
    // ...
  ],
})

多个

¥Multiple

渲染多个 Bar 组件以创建包含多个系列的柱状图。

¥Render multiple Bar components to create a bar chart with multiple series.

图例位置

¥Legend Position

layout 属性从 recharts 传递给 Legend 组件以配置图例的位置。

¥Pass the layout prop to the Legend component from recharts to configure the position of the legend.

百分比

¥Percent

BarChart 组件上将 stackOffset 属性设置为 expand,以将值标准化为 100% 来渲染柱状图。

¥Set the stackOffset prop to expand on the BarChart component to render the bars with value normalized to 100%.

堆叠

¥Stacked

渲染多个 Bar 组件,并将它们的 stackId 属性设置为相同的值以堆叠它们。

¥Render multiple Bar components and set their stackId prop to the same value to stack them.

堆叠混合

¥Stacked Mix

渲染多个具有不同 stackId 属性的 Bar 组件,以创建一些系列堆叠而一些系列不堆叠的柱状图。

¥Render multiple Bar components with different stackId props to create a bar chart with some series stacked and some not.

参考线

¥Reference Lines

使用 recharts 中的 ReferenceLine 组件引用图表上的特定值。

¥Use the ReferenceLine component from recharts to make reference to a specific value on the chart.

直方图

¥Histogram

对于数学高手,你可以编写柱状图来创建直方图。

¥For those mathematics wiz, you can compose the barchart to create a histogram.

头像刻度

¥Avatar Ticks

以下是利用 svg foreignObject 将图片渲染为 XAxis 标记的示例。

¥Here's an example of rendering images as the XAxis tick by leveraging svg foreignObject.

K 线图

¥Candlestick

将柱状图与 ErrorBarBar 组件组合,以创建 K 线图。

¥Combine the bar chart with the ErrorBar and Bar components to create a candlestick chart.

合成

¥Composition

以下是组合 BarChartCardSegmentGroup 组件的示例。

¥Here's an example of composing the BarChart, Card and SegmentGroup components.

OS Downloads

Previous

面积图

Next

条形列表