You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Recharts面积图同X轴中间数据点hover无响应问题求助

问题解决:Recharts面积图同X轴多点hover激活问题

问题说明

使用Recharts渲染面积图时,当同一X轴坐标上存在两个以上数据点,hover时仅最大和最小Y值的点会被激活,中间点无法触发hover效果。需要保留面积填充功能的同时,让所有数据点都能响应hover。

解决方案

通过在AreaChart中叠加Scatter组件实现:面积图负责填充区域,散点图负责渲染所有可hover的点。既满足填充需求,又能让每个数据点支持hover激活。

修改后的代码如下:

import { ResponsiveContainer, AreaChart, Area, XAxis, YAxis, CartesianGrid, Label, Tooltip, Scatter } from "recharts"

const topMargin = 10;
const leftMargin = 3;
const bottomMargin = 20;
const rightMargin = 15;

const data = [
    { x: 1, y: 2 },
    { x: 2, y: 3 },
    { x: 2, y: 4 },
    { x: 2, y: 5 },
    { x: 3, y: 4 }
]

export default function Test() {
    return (
        <div style={{ width: '33vw', height: '30vh' }}>
            <ResponsiveContainer width="100%" height="100%">
                <AreaChart
                    data={data}
                    margin={{
                        top: topMargin,
                        left: leftMargin,
                        bottom: bottomMargin,
                        right: rightMargin,
                    }}
                >
                    <CartesianGrid strokeDasharray="3 3" />
                    <XAxis
                        dataKey="x"
                        axisLine={{ stroke: "black" }}
                        tick={{ fill: "black" }}
                        tickLine={{ stroke: "black" }}
                        domain={[1, 3]}
                        style={{ fontSize: "14px" }}
                        type="number"
                    >
                        <Label
                            value="x"
                            position="insideBottom"
                            style={{ textAnchor: "middle", fontSize: "14px" }}
                            fill="black"
                            fontWeight="bold"
                            dy={bottomMargin}
                        />
                    </XAxis>
                    <YAxis
                        dataKey="y"
                        axisLine={{ stroke: "black" }}
                        tick={{ fill: "black" }}
                        tickLine={{ stroke: "black" }}
                        style={{ fontSize: "14px" }}
                        domain={[2, 5]}
                        type="number"
                    >
                        <Label
                            value="y"
                            position="insideLeft"
                            angle={-90}
                            fill="black"
                            fontWeight="bold"
                            style={{ textAnchor: "middle", fontSize: "14px" }}
                        />
                    </YAxis>
                    <Tooltip />
                    {/* 面积图负责填充区域,关闭自带点避免重复渲染 */}
                    <Area
                        type="linear"
                        dataKey="y"
                        stroke="#7ABFFF"
                        fill="rgba(0, 133, 255, 0.13)"
                        dot={false}
                    />
                    {/* 散点图渲染所有可hover的点,样式与原Area保持一致 */}
                    <Scatter
                        dataKey="y"
                        dot={{ fill: "#90CAFF", stroke: "#90CAFF", fillOpacity: 1, r: 3 }}
                        activeDot={{
                            fill: "#0085FF",
                            stroke: "#0085FF",
                            fillOpacity: 1,
                            r: 3,
                        }}
                    />
                </AreaChart>
            </ResponsiveContainer>
        </div>
    )
}

关键修改点

  • 引入Scatter组件,专门负责渲染所有数据点
  • 关闭Area组件的dot={false},避免和散点图的点重复渲染
  • Scatter组件的dot和activeDot样式与原Area保持一致,保证视觉统一

这样修改后,既能保留面积图的填充效果,又能让同一X轴上的所有数据点都能响应hover激活。

内容的提问来源于stack exchange,提问作者jamm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:42:54