使用高级 Chakra UI 组件更快地构建 💎

了解更多
跳到内容
文档在线示例指南博客
赞助

折线图

用于显示通过直线段连接的数据点,展示连续数据随时间或序列变化的趋势和模式

StorybookRecharts

用法

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

示例

坐标轴标签

要为 X 轴和 Y 轴添加标签,请使用来自 rechartsLabel 组件。

<XAxis axisLine={false} label={{ value: "X Axis", position: "bottom" }} />
<YAxis axisLine={false} label={{ value: "Y Axis", position: "left", angle: -90 }} />

无点

dotactiveDot 设置为 false 可完全隐藏点。

<Line dot={false} activeDot={false} />

点标签

Line 组件内部渲染 rechartsLabelList 组件,以在每个数据点显示标签。

<Line>
  <LabelList position="right" offset={10} />
</Line>

渐变

使用 Chart.Gradient 组件创建渐变。确保 id 唯一,并将其用于 Line 组件的 stroke 属性。

<defs>
  <Chart.Gradient id="custom-gradient" stops={[]} />
</defs>
<Line stroke="url(#custom-gradient)" />

虚线

series 对象中设置 strokeDasharray 属性以创建虚线。

const chart = useChart({
  data: [
    { windows: 186, mac: 165, month: "January" },
    //...
  ],
  series: [
    { name: "windows", color: "teal.solid", strokeDasharray: "5 5" },
    { name: "mac", color: "purple.solid" },
  ],
})

多系列

这是一个包含多个系列的折线图示例。

图例交互

为图表图例添加交互性使其生动起来。要启用此功能,请在 Chart.Legend 组件中将 interaction 属性设置为 "hover""click"

<Chart.Legend interaction="hover" />
悬停在“mac”上

起始和结束刻度

默认情况下,图表显示每个刻度的标签。要仅显示起始和结束刻度,请将 ticks 属性传递给 rechartsXAxis 组件。

您可以选择将 label 属性传递给 XAxis 组件,以在坐标轴底部显示标签。

<XAxis
  ticks={["January", "August"]}
  label={{ value: "[January - August] Customers", position: "bottom" }}
/>

值格式化器

要格式化值轴刻度,请将 tickFormatter 属性传递给 rechartsYAxis 组件。

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

双轴

series 对象和 YAxis 组件中使用 yAxisId 属性可以创建带有两个 Y 轴的图表。

<YAxis yAxisId="left" />
<YAxis yAxisId="right" orientation="right" />

自定义提示框

如果您需要完全自定义提示框,请用您自己的组件替换 Chart.Tooltip 组件。自定义提示框的基本结构如下:

function CustomTooltip(props: TooltipProps<string, string>) {
  const { active, payload, label } = props
  if (!active || !payload || payload.length === 0) return null

  return <Box>{/* Your custom tooltip content */}</Box>
}

系列标签

要为系列添加自定义标签,请在 series 对象中设置 label 属性。

const chart = useChart({
  data: [
    { mac: 10, linux: 120, month: "January" },
    //...
  ],
  series: [
    { name: "mac", label: "Mac sales", color: "purple.solid" },
    { name: "linux", label: "Linux sales", color: "blue.solid" },
  ],
})

参考点

使用 recharts 的参考组件来突出显示特定数据点。

<ReferenceDot x="August" y={110} r={6} />
<ReferenceLine y={110} label={{ value: "Target", position: "top" }} />

值域

domain 属性传递给 YAxis 组件,以设置值轴的域(上限和下限)。

<YAxis domain={[0, 100]} />

连接空值

要连接空值,请在 Line 组件中将 connectNulls 属性设置为 true

<Line connectNulls />

组合

这是一个将 CardStateChart 组件组合在一起,创建出令人惊叹的可视化效果的示例。

按渠道划分的客户

Facebook 广告
325
自然流量
387
Google 广告
327

线条类型

Recharts 提供了对各种折线图的灵活支持。

以下是您可以创建的不同类型的折线图

<Line type="linear" />

<Line type="bump" />

<Line type="basis" />

<Line type="step" />

<Line type="stepBefore" />

<Line type="stepAfter" />

<Line type="natural" />

<Line type="monotone" />

上一页

甜甜圈图

下一页

饼图