【问题标题】:Is there any way that I can only show the hovered line in tooltip (Recharts)?有什么办法只能在工具提示(Recharts)中显示悬停的线?
【发布时间】:2018-03-08 15:02:13
【问题描述】:

我曾尝试使用自定义工具提示,但我的问题是我不知道如何获取悬停的有效负载的索引。我想要的是仅在工具提示中显示悬停线的值。例如,我将鼠标悬停在值 1 行上,因此我只想在工具提示中显示值 1。

这是图片

这是我的代码,虽然我已经删除了自定义工具提示:

    export default class LineChartPresentational extends React.Component {
      constructor(props) {
      super();
      this.state = {
          clickedLineid: '' }}


      changeStrokeclick(data) {
         console.log(data, 'see what is coming');
         this.setState({clickedLineID: data} ) }

      render() {
         return ( 
            <div>
            <div id="lclastdataref" style={{ textAlign: 'right' }}>
            <span>Last Data Refresh: {linechartdf.date} </span>
            </div>
            <div className='line-charts'>
            <div className="line-chart-wrapper " style={{ width: window.innerWidth / 2, height: window.innerHeight / 2, }}>

        <ResponsiveContainer>
          <LineChart
            width={width} height={height} margin={{ top: 20, right: 20, bottom: 20, left: 20 }} data={linechartdata} id="Line-Chart">
            <XAxis dataKey={xAxisColumn} />
            <YAxis domain={['auto', 'auto']} />
            <Tooltip cursor={false} />
            {
              linechartdata.map((entry, index) => (
                <Line stroke={index === this.state.clickedLineID ? "#2d75ed" : "#9da0a5"} onClick={this.changeStrokeclick.bind(this, index)} name={linechartdata[index].dataKey} strokeWidth={lineThickness} dataKey={`value${index + 1}`} dot={false} className={`value${index + 1}`}/>
              ))
            }
            </LineChart>

        </ResponsiveContainer>
      </div>
    </div>
  </div> ); }}

我真的需要你的帮助。谢谢!

【问题讨论】:

    标签: javascript css reactjs recharts


    【解决方案1】:

    自定义工具提示是您执行此操作的选项。

    <Tooltip content={this.customTooltipOnYourLine}/>
    

    这里customTooltipOnYourLine 是您返回自定义工具提示的方法

        customTooltipOnYourLine(e){
            if (e.active && e.payload!=null && e.payload[0]!=null) {
                  return (<div className="custom-tooltip">
                        <p>{e.payload[0].payload["Column Name"]}</p>
                      </div>);
                }
            else{
               return "";
            }
          }
    

    查看此链接了解更多信息Recharts Tooltip

    编辑

    检查这个答案

    Answer2

    【讨论】:

      【解决方案2】:

      使用以下逻辑,您可以为每个点实现单独的工具提示。

      演示链接:Line chart with custom Tooltip

      1. 隐藏默认工具提示

      2. 在 Line 上添加鼠标事件功能(当 dot 处于活动状态时)

        <Line
              activeDot={{
                onMouseOver: this.showToolTip,
                onMouseLeave: this.hideToolTip
              }}
             ....
            />
        
      3. 自定义工具提示 div

          <div className="ui-chart-tooltip" ref={ref => (this.tooltip = ref)} >
              <div className="ui-chart-tooltip-content" />
          </div>
        
      4. showToolTip 和 hideTooltip 函数

             showToolTip = (e) => {
              let x = Math.round(e.cx);
              let y = Math.round(e.cy);
              this.tooltip.style.opacity = "1";
              this.tooltip.childNode[0].innerHTML = e.payload["value"];
        
              };
        
              hideTooltip = e => {
              this.tooltip.style.opacity = "0";
              };
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多