简体   繁体   English

重新图表关闭特定行的工具提示

[英]Recharts turn off tooltip for specific Line

I have a LineChart with N lines.我有一个带有 N 行的LineChart One line is fixed (straight line with a fixed y-axis value and same x-axis values as all other lines, the time value).一条线是固定的(具有固定 y 轴值和与所有其他线相同的 x 轴值的直线,即时间值)。 In most cases, there's only the fixed line and just one other line with values that change.在大多数情况下,只有固定线和另一条线的值会发生变化。 Both display correctly, but when I hover over them, the Tooltip shows data for the fixed line.两者都显示正确,但是当我将鼠标悬停在它们上面时,工具提示会显示固定线的数据。 I need to stop displaying the fixed line data point inside the Tooltip and start showing only the other line's data (which gets displayed ok when there's no fixed line on the chart).我需要停止在工具提示内显示固定线数据点,并开始只显示另一条线的数据(当图表上没有固定线时,它可以正常显示)。

The Tooltip is just:工具提示只是:

<Tooltip
  contentStyle={{ fontSize: 12 }}
  labelStyle={{ fontSize: 12 }}
/>

The fixed line is:固定线路是:

<Line
  key="fixed-key"
  isAnimationActive={false}
  dot={false}
  type="linear"
  dataKey="value"
  data={fixedData}
  connectNulls
  activeDot={false}
  legendType="rect"
  name="Threshold"
/>

and the dynamic line is almost the same, just a few extra props (might be worth mentioning that this line gets it's data from the data passed directly to the parent component, the LineChart :和动态线几乎相同,只是一些额外的道具(可能值得一提的是,这条线从直接传递给父组件LineChart的数据中获取数据:

<Line
  key={`line-data-${id}`}
  isAnimationActive={false}
  dot={false}
  type="linear"
  dataKey={id}
  connectNulls
  stroke={lineColor || colorIndex[index]}
  activeDot={{ r: 5 }}
  legendType="rect"
  name={widgetLabel || formatLabel(metric)}
  strokeDasharray={lineType === 'dashed' ? '1 1' : null}
/>

So, to reiterate - the chart itself works fine, the only issue is the Tooltip component showing data for the first line, instead of just the second one - don't know how to do that, to omit a line from the Tooltip display.所以,重申一下 - 图表本身工作正常,唯一的问题是工具提示组件显示第一行的数据,而不仅仅是第二行 - 不知道如何做到这一点,从工具提示显示中省略一行。

Is this possible in Recharts?这在 Recharts 中是可能的吗?

In case this helps anybody in the future.如果这对将来的任何人有帮助。

You can set the name to foo for the specific <Line> that you want to hide the tooltip.您可以将要隐藏工具提示的特定<Line>name设置为foo

https://recharts.org/en-US/api/Line https://recharts.org/en-US/api/Line

The name of data.数据名称。 This option will be used in tooltip and legend to represent a line.此选项将在工具提示和图例中用于表示一条线。 If no value was set to this option, the value of dataKey will be used alternatively.如果此选项未设置任何值,则将交替使用 dataKey 的值。

Then use a CustomTooltip Formatter and check if the name is the same as the specific line.然后使用 CustomTooltip Formatter 并检查名称是否与特定行相同。

const tooltipFormatter = ({ value, name }) => {
    if (name === 'foo') return
    return your-jsx-for-tooltip
}

<Tooltip formatter={tooltipFormatter} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM