Skip to Content
文档

折线图

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

StorybookRecharts

用法

¥Usage

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>

示例

¥Examples

轴标签

¥Axes Label

要向 x 轴和 y 轴添加标签,请使用 recharts 中的 Label 组件。

¥To add labels to the x and y axes, use the Label component from recharts.

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

无点

¥No Dots

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

¥Set dot and activeDot to false to hide the dots completely.

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

点标签

¥Point Labels

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

¥Render the LabelList component from recharts inside the Line component to show labels at each data point.

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

渐变

¥Gradient

使用 Chart.Gradient 组件创建渐变。确保 id 是唯一的,并用于 Line 组件的 stroke prop。

¥Use the Chart.Gradient component to create a gradient. Ensure the id is unique and used in the stroke prop of the Line component.

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

虚线

¥Dashed

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

¥Set the strokeDasharray prop in the series object to create a dashed line.

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

多个

¥Multiple

以下是包含多个系列的折线图的示例。

¥Here's an example of a line chart with multiple series.

图例交互

¥Legend Interaction

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

¥Adding interactivity to the chart legends make it come to life. To enable this feature, set the interaction prop to "hover" or "click" in the Chart.Legend component.

<Chart.Legend interaction="hover" />
Hover on "mac"

起始和结束标记

¥Start and End Tick

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

¥By default, the chart shows the label for each tick. To show just the start and end ticks, pass the ticks prop to the XAxis component from recharts.

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

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

值格式化程序

¥Value Formatter

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

¥To format the value axis ticks, pass the tickFormatter prop to the YAxis component from recharts.

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

双轴

¥Biaxial

使用 series 对象和 YAxis 组件中的 yAxisId 属性创建具有两个 y 轴的图表。

¥Use the yAxisId prop in the series object and YAxis component to create a chart with two y-axes.

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

自定义工具提示

¥Custom Tooltip

如果你需要完全自定义工具提示,请用你自己的组件替换 Chart.Tooltip 组件。自定义工具提示的基本签名如下:

¥In event you need to customize the tooltip entirely, replace the Chart.Tooltip component with your own. The basic signature of a custom tooltip looks like:

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

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

¥To add a custom label to the series, set the label prop in the series object.

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" },
  ],
})

参考点

¥Reference Point

使用 recharts 中的参考组件高亮显示特定数据点。

¥Use the reference components from recharts to highlight a specific data point.

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

值域

¥Value Domain

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

¥Pass the domain prop to the YAxis component to set the domain (upper and lower bounds) of the value axis.

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

连接空值

¥Connect Nulls

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

¥To connect the null values, set the connectNulls prop to true in the Line component.

<Line connectNulls />

合成

¥Composition

以下是组合 CardStateChart 组件以创建令人惊叹的可视化效果的示例。

¥Here's an example of composing the Card, State and Chart components together to create a stunning visualization.

Customers by channel

Facebook Ads
325
Organic
387
Google Ads
327

线条类型

¥Line Types

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

¥Recharts provides flexible support for various kinds of line charts.

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

¥Below are the different types of line charts you can create:

<Line type="linear" />

<Line type="bump" />

<Line type="basis" />

<Line type="step" />

<Line type="stepBefore" />

<Line type="stepAfter" />

<Line type="natural" />

<Line type="monotone" />

Previous

圆环图

Next

饼图