"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "desc" },
data: [
{ name: "Google", value: 1200000 },
{ name: "Direct", value: 100000 },
{ name: "Bing", value: 200000 },
{ name: "Yahoo", value: 20000 },
{ name: "ChatGPT", value: 1345000 },
{ name: "Github", value: 100000 },
{ name: "Yandex", value: 100000 },
],
series: [{ name: "name", color: "teal.subtle" }],
})
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Bar />
<BarList.Value />
</BarList.Content>
</BarList.Root>
)
}
用法
¥Usage
import { BarList, Chart, useChart } from "@chakra-ui/charts"
<BarList.Root>
<BarList.Content>
<BarList.Bar />
<BarList.Value />
</BarList.Content>
</BarList.Root>
示例
¥Examples
排序顺序
¥Sort Order
将 sort
键设置为 { by: "value", direction: "asc" }
,以便按升序对柱状图进行排序。
¥Set the sort
key to { by: "value", direction: "asc" }
to sort the bars in
ascending order.
const chart = useChart<BarListData>({
sort: { by: "value", direction: "asc" },
// ...
})
"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "asc" },
data: [
{ name: "Google", value: 1200000 },
{ name: "Direct", value: 100000 },
{ name: "Bing", value: 200000 },
{ name: "Yahoo", value: 20000 },
{ name: "ChatGPT", value: 1345000 },
{ name: "Github", value: 100000 },
{ name: "Yandex", value: 100000 },
],
series: [{ name: "name", color: "teal.subtle" }],
})
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Bar />
<BarList.Value />
</BarList.Content>
</BarList.Root>
)
}
格式值
¥Format Value
将 valueFormatter
属性传递给 BarList.Value
组件,以格式化条形的值。
¥Pass the valueFormatter
prop to the BarList.Value
component to format the
value of the bars.
<BarList.Value valueFormatter={(value) => value.toLocaleString()} />
"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "desc" },
data: [
{ name: "Created", value: 120 },
{ name: "Initial Contact", value: 90 },
{ name: "Booked Demo", value: 45 },
{ name: "Closed", value: 10 },
],
series: [{ name: "name", color: "pink.subtle" }],
})
const getPercent = (value: number) =>
chart.getValuePercent("value", value).toFixed(0)
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Bar />
<BarList.Value
valueFormatter={(value) => `${value} (${getPercent(value)}%)`}
/>
</BarList.Content>
</BarList.Root>
)
}
标签
¥Labels
要向柱状图添加名称和值标签,请使用 BarList.Label
组件。
¥To add name and value labels to the bars, use the BarList.Label
component.
<BarList.Label title="Search Engine" flex="1">
<BarList.Bar />
</BarList.Label>
Search Engine
Downloads
"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "desc" },
data: [
{ name: "Google", value: 1200000 },
{ name: "Direct", value: 100000 },
{ name: "Bing", value: 200000 },
{ name: "Yahoo", value: 20000 },
{ name: "ChatGPT", value: 1345000 },
{ name: "Github", value: 100000 },
{ name: "Yandex", value: 100000 },
],
series: [{ name: "name", color: "teal.subtle" }],
})
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Label title="Search Engine" flex="1">
<BarList.Bar />
</BarList.Label>
<BarList.Label title="Downloads" titleAlignment="end">
<BarList.Value />
</BarList.Label>
</BarList.Content>
</BarList.Root>
)
}
链接
¥Link
要使栏渲染链接,请将 label
属性传递给 BarList.Bar
组件。
¥To make the bars render a link, pass the label
prop to the BarList.Bar
component.
<BarList.Bar
label={({ payload }) => <a href={payload.href}>{payload.name}</a>}
/>
"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "desc" },
data: [
{ name: "Created", value: 120, href: "#" },
{ name: "Initial Contact", value: 90, href: "#" },
{ name: "Booked Demo", value: 45, href: "#" },
{ name: "Closed", value: 10, href: "#" },
],
series: [{ name: "name", color: "pink.subtle" }],
})
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Bar
label={({ payload }) => <a href={payload.href}>{payload.name}</a>}
/>
<BarList.Value />
</BarList.Content>
</BarList.Root>
)
}
工具提示
¥Tooltip
将 tooltip
属性传递给 BarList.Bar
组件,以在鼠标悬停在柱状图上时显示工具提示。
¥Pass the tooltip
prop to the BarList.Bar
component to show a tooltip when
hovering over the bar.
Search Engine
Downloads
"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "desc" },
data: [
{ name: "Google", value: 1200000 },
{ name: "Direct", value: 100000 },
{ name: "Bing", value: 200000 },
{ name: "Yahoo", value: 20000 },
{ name: "ChatGPT", value: 1345000 },
{ name: "Github", value: 100000 },
{ name: "Yandex", value: 100000 },
],
series: [{ name: "name", color: "teal.subtle", label: "Search Engine" }],
})
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Label title="Search Engine" flex="1">
<BarList.Bar tooltip />
</BarList.Label>
<BarList.Label title="Downloads" titleAlignment="end">
<BarList.Value />
</BarList.Label>
</BarList.Content>
</BarList.Root>
)
}
多个值
¥Multiple values
以下是如何渲染条形的值和百分比的示例。
¥Here's an example of how to render the value and percent of the bars.
Search Engine
Downloads
%
"use client"
import { BarList, type BarListData, useChart } from "@chakra-ui/charts"
const Demo = () => {
const chart = useChart<BarListData>({
sort: { by: "value", direction: "desc" },
data: [
{ name: "Google", value: 1200000 },
{ name: "Direct", value: 100000 },
{ name: "Bing", value: 200000 },
{ name: "Yahoo", value: 20000 },
{ name: "ChatGPT", value: 1345000 },
{ name: "Github", value: 100000 },
{ name: "Yandex", value: 100000 },
],
series: [{ name: "name", color: "teal.subtle" }],
})
const getPercent = (value: number) =>
chart.getValuePercent("value", value).toFixed(2)
return (
<BarList.Root chart={chart}>
<BarList.Content>
<BarList.Label title="Search Engine" flex="1">
<BarList.Bar />
</BarList.Label>
<BarList.Label title="Downloads" minW="16" titleAlignment="end">
<BarList.Value />
</BarList.Label>
<BarList.Label title="%" minW="16" titleAlignment="end">
<BarList.Value valueFormatter={(value) => `${getPercent(value)}%`} />
</BarList.Label>
</BarList.Content>
</BarList.Root>
)
}