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

如何将Recharts Tooltip的cursor垂直悬停线设置为虚线?

解决Recharts Tooltip垂直悬停线设为虚线的问题

Recharts的Tooltip组件默认的cursor仅支持开关,无法直接修改样式,但可以通过自定义cursor元素实现虚线效果,具体操作如下:

  • 自定义一个Cursor组件,利用Recharts自动传递的x属性定位线条,同时设置strokeDasharray实现虚线:
const CustomCursor = ({ x }) => (
  <line
    x1={x}
    x2={x}
    y1="0"
    y2="100%"
    stroke="#8884d8" // 可自定义线条颜色
    strokeWidth={2} // 可自定义线条宽度
    strokeDasharray="3 3" // 虚线样式,数值可按需调整
  />
);
  • 修改Tooltip的cursor属性,替换为自定义组件:
<Tooltip content={CustomTooltip} cursor={<CustomCursor />} />

Recharts会在鼠标悬停时自动将当前x轴坐标传递给自定义cursor组件,确保线条精准跟随鼠标位置。你还可以根据图表风格调整stroke、strokeWidth等属性,适配需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 11:02:02