[英]Recharts: Turn off background hover for bar charts
Is there a way to turn off this gray background that appears on hover for bar charts in Recharts ? 有没有一种方法可以关闭“图表”中条形图悬停在鼠标悬停时出现的灰色背景?
Using version 1.4.1. 使用1.4.1版。 Code (simplified) looks like this:
代码(简化)如下所示:
import React from "react"
// Recharts
import { Bar, BarChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"
import CustomTooltip from "../CustomTooltip"
const BarChart = ({ chartData, chartInfo }) => (
<ResponsiveContainer width="99%" height={260}>
<BarChart data={chartData}>
<XAxis
dataKey="label"
type="category"
allowDuplicatedCategory={true}
tickLine={false}
orientation="bottom"
/>
<YAxis
tickLine={false}
orientation="left"
axisLine={false}
/>
<Tooltip
isAnimationActive={false}
separator={": "}
content={<CustomTooltip />}
/>
<CartesianGrid vertical={false} />
{chartInfo.map(bar => (
<Bar
key={bar.dataKey}
name={bar.name}
dataKey={bar.dataKey}
isAnimationActive={false}
/>
))}
</BarChart>
</ResponsiveContainer>
)
export default BarChart
I've poured over the API docs as well as looking through the source code. 我仔细阅读了API文档并浏览了源代码。 Doesn't seem to be a way to do it, but some of the demos have it disabled, like this one .
似乎没有一个方法可以做到这一点,但有些演示有禁用它,像这一个 。
I tried setting up mine with custom shapes like that demo and rendering with Cell
instead of Bar
, but the background was still there on hover. 我尝试设置具有类似演示的自定义形状的矿井,并使用
Cell
而不是Bar
渲染,但是背景仍然悬停在那儿。 The background color is #ccc
but searching the repository with that keyword yielded no clear methods or props to try to override or hook into. 背景颜色为
#ccc
但是使用该关键字搜索存储库不会产生清晰的方法或道具来尝试覆盖或插入。
You can use the cursor
prop on the <Tooltip />
to achieve what you need: 您可以使用
<Tooltip />
上的cursor
道具实现所需的功能:
<Tooltip cursor={{fill: '#f00'}} />
Here 'sa working fiddle I made based on some example from their docs. 这是我根据他们的文档中的一些示例制作的一个小提琴。
You can simply use transparent
as a value for the fill
property. 您可以简单地将
transparent
用作fill
属性的值。
const {BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend} = Recharts; const data = [ {name: 'Page A', uv: 4000, pv: 2400, amt: 2400}, {name: 'Page B', uv: 3000, pv: 1398, amt: 2210}, {name: 'Page C', uv: 2000, pv: 9800, amt: 2290}, {name: 'Page D', uv: 2780, pv: 3908, amt: 2000}, {name: 'Page E', uv: 1890, pv: 4800, amt: 2181}, {name: 'Page F', uv: 2390, pv: 3800, amt: 2500}, {name: 'Page G', uv: 3490, pv: 4300, amt: 2100}, ]; const SimpleBarChart = React.createClass({ render () { return ( <BarChart width={600} height={300} data={data} margin={{top: 5, right: 30, left: 20, bottom: 5}}> <CartesianGrid strokeDasharray="3 3"/> <XAxis dataKey="name"/> <YAxis/> <Tooltip cursor={{fill: 'transparent'}}/> <Legend /> <Bar dataKey="pv" fill="#8884d8" /> <Bar dataKey="uv" fill="#82ca9d" /> </BarChart> ); } }) ReactDOM.render( <SimpleBarChart />, document.getElementById('container') );
body { margin: 0; } #container { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; padding: 10px; width: 800px; height: 800px; background-color: #fff; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script> <script src="https://npmcdn.com/react@15.6.2/dist/react-with-addons.min.js"></script> <script src="https://npmcdn.com/react-dom@15.6.2/dist/react-dom.min.js"></script> <script src="https://npmcdn.com/prop-types@15.6.2/prop-types.min.js"></script> <script src="https://npmcdn.com/recharts@1.4.2/umd/Recharts.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div>
The grey area you mentioned is called cursor
. 您提到的灰色区域称为
cursor
。 Quoting the Tooltip
API docs: 引用
Tooltip
API文档:
If set false, no cursor will be drawn when tooltip is active.
如果设置为false,则在激活工具提示时不会绘制任何光标。
So the answer to your question is: 因此,您的问题的答案是:
<Tooltip cursor={false} />
BTW, I'm using recharts@1.7.1 顺便说一句,我正在使用recharts@1.7.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.