首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在新条形图“自定义层”上添加工具提示?

如何在新条形图“自定义层”上添加工具提示?
EN

Stack Overflow用户
提问于 2022-05-31 06:42:46
回答 1查看 644关注 0票数 1

我已经使用@nivo/bar来绘制我的react应用程序中的条形图。添加行以显示平均值。在酒吧里,工具提示很好用。当用户悬停在线上以显示平均值时,我想添加一个工具提示。附像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-21 12:27:03

我不确定是否有人需要实现类似的功能。但我对此做了一些研究,以下是我自己问题的答案:

代码语言:javascript
复制
const CustomTagretLine = ({ props }) => {
const { bars } = props
const { showTooltipAt, hideTooltip } = useTooltip()


//show tooltip on mouse enter
const handleMouseEnter = (point) => {
  showTooltipAt(
    props.tooltip(point),
    [point.x + props.margin.left, point.y + props.margin.top],
    'top'
  )
}

//move tooltip with mouse position
const handleMouseMove = (point) => {
  showTooltipAt(
    props.tooltip(point),
    [point.x + props.margin.left, point.y + props.margin.top],
    'top'
  )
}

const handleMouseLeave = () => {
  hideTooltip()
}

return (
  <>
    {bars.map((bar, idx) => (
      <>
        {bar.height && (
          <line
            onMouseOver={() =>
              handleMouseEnter({
                x: bar.x - 5,
                y: props.yScale(bar?.data?.data?.avgVal || 0), //whatever you want
                absX: bar.x - 5 + 70,
                absY: props.yScale(bar?.data?.data?.avgVal || 0) + 50,
                width: 70,
                height: 4,
                data: {
                  ...bar?.data?.data,
                  isAvg: true,
                },
                formattedValue: formatNumber(
                  parseInt(bar?.data?.data?.avgVal) || 0
                ),
              })
            }
            onMouseLeave={() =>
              handleMouseLeave({
                x: bar.x - 5,
                y: props.yScale(bar?.data?.data?.avgVal || 0), //whatever you want
                absX: bar.x - 5 + 70,
                absY: props.yScale(bar?.data?.data?.avgVal || 0) + 50,
                width: 70,
                height: 4,
                data: {
                  ...bar?.data?.data,
                  isAvg: true,
                },
                formattedValue: formatNumber(
                  parseInt(bar?.data?.data?.avgVal) || 0
                ),
              })
            }
            onMouseMove={() =>
              handleMouseMove({
                x: bar.x - 5,
                y: props.yScale(bar?.data?.data?.avgVal || 0), //whatever you want
                absX: bar.x - 5 + 70,
                absY: props.yScale(bar?.data?.data?.avgVal || 0) + 50,
                width: 70,
                height: 4,
                data: {
                  ...bar?.data?.data,
                  isAvg: true,
                },
                formattedValue: formatNumber(
                  parseInt(bar?.data?.data?.avgVal) || 0
                ),
              })
            }
            key={idx}
            opacity="1"
            x1={bar.x - 5}
            x2={bar.x + bar.width + 5}
            y1={props.yScale(bar?.data?.data?.avgVal || 0)}
            y2={props.yScale(bar?.data?.data?.avgVal || 0)}
            stroke="#000000"
            strokeWidth="3"
          ></line>
        )}
      </>
    ))}
  </>
)}

CustomTargetLine是自定义层,可以像这样在条形图中添加

代码语言:javascript
复制
layers={[
      'grid',
      'axes',
      'bars',
      'legends',
      'annotations',
      (props) => <CustomTagretLine props={props} />,
    ]} 

参考文献:https://nivo.rocks/bar/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72443244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档