"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
Tooltip,
XAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid stroke={chart.color("border.muted")} vertical={false} />
<XAxis
axisLine={false}
tickLine={false}
dataKey={chart.key("month")}
tickFormatter={(value) => value.slice(0, 3)}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
用法
import { Chart, useChart } from "@chakra-ui/charts"
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts"
<Chart.Root>
<AreaChart>
<CartesianGrid />
<XAxis />
<YAxis />
</AreaChart>
</Chart.Root>
示例
值轴
使用 recharts
中的 YAxis
组件来显示 y 轴。
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import { Area, AreaChart, Tooltip, XAxis, YAxis } from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "orange.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart
accessibilityLayer
data={chart.data}
margin={{ bottom: 24, left: 24 }}
>
<XAxis
dataKey={chart.key("month")}
tickMargin={8}
tickFormatter={(value) => value.slice(0, 3)}
stroke={chart.color("border")}
/>
<YAxis stroke={chart.color("border")} />
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
{chart.series.map((item) => (
<Area
type="natural"
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
虚线区域
将 strokeDasharray
属性设置为您希望显示为虚线的 series
。
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
Tooltip,
XAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid", strokeDasharray: "5 3" },
{ name: "mac", color: "orange.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid stroke={chart.color("border.muted")} vertical={false} />
<XAxis
axisLine={false}
tickLine={false}
dataKey={chart.key("month")}
tickFormatter={(value) => value.slice(0, 3)}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
strokeWidth={2}
strokeDasharray={item.strokeDasharray}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
渐变区域
使用 Chart.Gradient
组件为区域创建渐变填充。
注意: 渐变的 id
必须是唯一的,并在 Area
组件的 fill
属性中引用。
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
Tooltip,
XAxis,
YAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid
stroke={chart.color("border")}
vertical={false}
strokeDasharray="3 3"
/>
<XAxis
dataKey={chart.key("month")}
tickLine={false}
axisLine={false}
tickMargin={8}
tickFormatter={(value) => value.slice(0, 3)}
/>
<YAxis tickLine={false} axisLine={false} />
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
{chart.series.map((item) => (
<defs key={item.name}>
<Chart.Gradient
id={`${item.name}-gradient`}
stops={[
{ offset: "0%", color: item.color, opacity: 0.3 },
{ offset: "100%", color: item.color, opacity: 0.05 },
]}
/>
</defs>
))}
{chart.series.map((item) => (
<Area
key={item.name}
type="natural"
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={`url(#${item.name}-gradient)`}
stroke={chart.color(item.color)}
strokeWidth={2}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
按值填充
使用 Chart.Gradient
组件创建一个渐变填充,根据值从一种颜色变为另一种颜色。
<defs>
<Chart.Gradient
id="uv-gradient"
stops={[
{ offse: "0%", color: "teal.solid", opacity: 1 },
{ offset: "100%", color: "red.solid", opacity: 1 },
]}
/>
</defs>
当值为正时,使用第一种颜色;当值为负时,使用第二种颜色。
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import { Area, AreaChart, CartesianGrid, Tooltip, XAxis, YAxis } from "recharts"
const data = [
{ name: "Product A", uv: 4000, pv: 2400, amt: 2400 },
{ name: "Product B", uv: 3000, pv: 1398, amt: 2210 },
{ name: "Product C", uv: -1000, pv: 9800, amt: 2290 },
{ name: "Product D", uv: 500, pv: 3908, amt: 2000 },
{ name: "Product E", uv: -2000, pv: 4800, amt: 2181 },
{ name: "Product F", uv: -250, pv: 3800, amt: 2500 },
{ name: "Product G", uv: 3490, pv: 4300, amt: 2100 },
]
const gradientOffset = () => {
const max = Math.max(...data.map((i) => i.uv))
const min = Math.min(...data.map((i) => i.uv))
if (max <= 0) return 0
if (min >= 0) return 1
return max / (max - min)
}
const offset = gradientOffset()
const Demo = () => {
const chart = useChart({
data,
series: [
{ name: "uv", color: "teal.solid" },
{ name: "pv", color: "purple.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid strokeDasharray="3 3" stroke={chart.color("border")} />
<XAxis
axisLine={false}
tickLine={false}
dataKey={chart.key("name")}
tickFormatter={(value) => value.replace("Product ", "")}
/>
<YAxis
axisLine={false}
tickLine={false}
tickFormatter={chart.formatNumber({
style: "currency",
currency: "USD",
currencyDisplay: "narrowSymbol",
notation: "compact",
})}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<defs>
<Chart.Gradient
id="uv-gradient"
stops={[
{ offset, color: "teal.solid", opacity: 1 },
{ offset, color: "red.solid", opacity: 1 },
]}
/>
</defs>
<Area
type="monotone"
isAnimationActive={false}
dataKey={chart.key("uv")}
fill="url(#uv-gradient)"
fillOpacity={0.2}
stroke={chart.color("gray.solid")}
/>
</AreaChart>
</Chart.Root>
)
}
百分比
将面积图渲染为百分比形式,值归一化为 100%
- 将
Area
组件上的stackId
属性设置为相同的值 - 将
AreaChart
组件上的stackOffset
属性设置为expand
- 通过
tickFormatter
属性将 y 轴格式化为百分比格式
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
Tooltip,
XAxis,
YAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart accessibilityLayer stackOffset="expand" data={chart.data}>
<CartesianGrid stroke={chart.color("border")} vertical={false} />
<XAxis
dataKey={chart.key("month")}
tickLine={false}
axisLine={false}
tickMargin={8}
tickFormatter={(value) => value.slice(0, 3)}
/>
<YAxis
tickLine={false}
axisLine={false}
tickFormatter={chart.formatNumber({ style: "percent" })}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
点
在 Area
组件上设置 dot
属性以显示映射到每个数据点的点。
<Area dot={{ fill: "red", fillOpacity: 1 }} activeDot={false} />
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
Tooltip,
XAxis,
YAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 349, month: "August" },
{ windows: 180, mac: 86, linux: 400, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data} margin={{ right: 20 }}>
<CartesianGrid stroke={chart.color("border.muted")} vertical={false} />
<YAxis stroke={chart.color("border")} axisLine={false} />
<XAxis
axisLine={false}
tick={false}
dataKey={chart.key("month")}
tickFormatter={(value) => value.slice(0, 3)}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
{chart.series.map((item) => (
<Area
isAnimationActive={false}
stackId="b"
legendType="none"
tooltipType="none"
key={item.name}
dataKey={chart.key(item.name)}
dot={{ fill: chart.color(item.color), fillOpacity: 1 }}
activeDot={false}
fill="none"
stroke="none"
/>
))}
</AreaChart>
</Chart.Root>
)
}
连接空值
将 connectNulls
属性传递给 Area
组件,即使中间有 null
值也能连接数据点。当您希望在缺少数据点的情况下显示连续线时,这很有用。
<Area connectNulls={false} />
<Area connectNulls={true} />
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import { Box, For, Heading, SimpleGrid } from "@chakra-ui/react"
import { Area, AreaChart, CartesianGrid, Tooltip, XAxis } from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ sales: 186, month: "January" },
{ sales: null, month: "February" },
{ sales: 190, month: "March" },
{ sales: 195, month: "May" },
{ sales: null, month: "June" },
{ sales: 175, month: "August" },
{ sales: 180, month: "October" },
{ sales: 185, month: "November" },
{ sales: 300, month: "December" },
],
series: [{ name: "sales", color: "teal.solid" }],
})
return (
<SimpleGrid gap="10" minChildWidth="400px">
<For each={["false", "true"]}>
{(connectNulls) => (
<Box key={connectNulls.toString()}>
<Heading size="md" mb="4">
{`<Area connectNulls={${connectNulls.toString()}} />`}
</Heading>
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid
stroke={chart.color("border.muted")}
vertical={false}
/>
<XAxis
axisLine={false}
tickLine={false}
dataKey={chart.key("month")}
tickFormatter={(value) => value.slice(0, 3)}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
connectNulls={connectNulls === "true"}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
</Box>
)}
</For>
</SimpleGrid>
)
}
参考线
使用 recharts
中的 ReferenceLine
组件向图表添加参考线。当您想要突出显示图表中的特定值时,参考线很有用。
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
ReferenceLine,
Tooltip,
XAxis,
YAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid stroke={chart.color("border.muted")} vertical={false} />
<YAxis stroke={chart.color("border")} />
<XAxis
axisLine={false}
tickLine={false}
dataKey={chart.key("month")}
tickFormatter={(value) => value.slice(0, 3)}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
<ReferenceLine
x="August"
label={{
value: "Black Friday",
position: "insideTopRight",
style: { fill: chart.color("red.fg"), fontWeight: "500" },
}}
stroke={chart.color("red.solid")}
/>
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
参考区域
使用 recharts
中的 ReferenceArea
组件向图表添加参考区域。当您想要突出显示图表中的特定范围时,参考区域很有用。
"use client"
import { Chart, useChart } from "@chakra-ui/charts"
import {
Area,
AreaChart,
CartesianGrid,
Legend,
ReferenceArea,
ReferenceLine,
Tooltip,
XAxis,
YAxis,
} from "recharts"
const Demo = () => {
const chart = useChart({
data: [
{ windows: 186, mac: 80, linux: 120, month: "January" },
{ windows: 165, mac: 95, linux: 110, month: "February" },
{ windows: 190, mac: 87, linux: 125, month: "March" },
{ windows: 195, mac: 88, linux: 130, month: "May" },
{ windows: 182, mac: 98, linux: 122, month: "June" },
{ windows: 175, mac: 90, linux: 115, month: "August" },
{ windows: 180, mac: 86, linux: 124, month: "October" },
{ windows: 185, mac: 91, linux: 126, month: "November" },
],
series: [
{ name: "windows", color: "teal.solid" },
{ name: "mac", color: "purple.solid" },
{ name: "linux", color: "blue.solid" },
],
})
return (
<Chart.Root maxH="sm" chart={chart}>
<AreaChart data={chart.data}>
<CartesianGrid stroke={chart.color("border.muted")} vertical={false} />
<YAxis stroke={chart.color("border")} />
<XAxis
axisLine={false}
tickLine={false}
dataKey={chart.key("month")}
tickFormatter={(value) => value.slice(0, 3)}
ticks={["February", "June"]}
/>
<Tooltip
cursor={false}
animationDuration={100}
content={<Chart.Tooltip />}
/>
<Legend content={<Chart.Legend />} />
<ReferenceLine x="February" stroke={chart.color("red.solid")} />
<ReferenceLine x="June" stroke={chart.color("red.solid")} />
<ReferenceArea
x1="February"
x2="June"
fill={chart.color("red.solid")}
label={{
position: "insideTop",
value: "Feb - June '24",
style: { fill: chart.color("red.fg") },
}}
fillOpacity={0.2}
/>
{chart.series.map((item) => (
<Area
key={item.name}
isAnimationActive={false}
dataKey={chart.key(item.name)}
fill={chart.color(item.color)}
fillOpacity={0.2}
stroke={chart.color(item.color)}
stackId="a"
/>
))}
</AreaChart>
</Chart.Root>
)
}
面积图类型
Recharts 为各种面积图提供了灵活的支持。
以下是您可以创建的不同类型的面积图
<Area type="linear" />
<Area type="bump" />
<Area type="basis" />
<Area type="step" />
<Area type="stepBefore" />
<Area type="stepAfter" />
<Area type="natural" />
<Area type="monotone" />