question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Tooltip opacity always 1

See original GitHub issue

I need to have custom Tooltip and I design a react-component using hooks, check out the code:

import {Line} from "react-chartjs-2";
import {Button} from "reactstrap";
import React, {useState, useRef} from "react";
// noinspection ES6UnusedImports
// eslint-disable-next-line no-unused-vars
// import Drag from "../DraggableChart";

export default function ProgramChart({data}) {
    const [tooltipData, setTooltipData] = useState(null);
    const chartRef = useRef(null);
    return (
        <div>
            <Line
                ref={chartRef}
                options={{
                    legend: {
                        display: false
                    },
                        tooltips: {
                        "enabled": false,
                        "mode": "point",
                        "intersect": false,
                        custom: (tooltipModel) => {
                            // hide tooltip if needed
                            if (tooltipModel.opacity === 0) {
                                setTooltipData(null);
                                console.log("Hide tooltip");
                                return;
                            }
                            const chart = chartRef.current.chartInstance;
                            const canvas = chart.ctx.canvas;

                            if (canvas) {
                                const position = canvas.getBoundingClientRect();
                               
                                // set position of tooltip
                                const left = position.left + tooltipModel.caretX;
                                const top = position.top + tooltipModel.caretY;

                                setTooltipData({top, left});
                            }
                        }
                    },
                 }}
                data={chartData}
                style={{
                    display: "flex",
                    justifyContent: "center",
                    alignItems: "center",
                    textAlign: "center",
                    flex: 1,
                    height: "100%",
                }}/>
            {tooltipData ? <GraphTooltip data={tooltipData}/> : <div/>}
        </div>
    );
};

const GraphTooltip = ({data}) => <div
    style={{
        padding: 20,
        position: "fixed",
        border: "1px solid",
        borderColor: "#fff8f9",
        backgroundColor: "rgba(53,53,53,0.81)",
        borderRadius: 4,
        top: data.top,
        left: data.left,
    }}
>
    <Button>Whatever</Button>
</div>;

Tooltip is rendered but it never disappear!

If I remove setTooltipData({top, left}); line than there are no problem…

Have anyone any idea?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:9

github_iconTop GitHub Comments

2reactions
wongJonathancommented, Apr 24, 2020

I decided to take it out due to having issues creating my own that looked nice but I found what I used that worked.

const customTooltip = useCallback((tooltipModel: any) => {
    // if chart is not defined, return early
    let chart = chartRef.current;
    if (!chart) {
      return;
    }

    // hide the tooltip when chartjs determines you've hovered out
    if (tooltipModel.opacity === 0) {
      setOpenToolTip(false);
      return;
    }

    const position = chart.chartInstance.canvas.getBoundingClientRect();

    // assuming your tooltip is `position: fixed`
    // set position of tooltip
    const left = position.left + tooltipModel.caretX;
    const top = position.top - tooltipModel.height;

    // set values for display of data in the tooltip
    const label = tooltipModel.dataPoints[0].xLabel;
    const index = tooltipModel.dataPoints[0].index;

    setPositionAndData({top, left, label, index});
    setOpenToolTip(true);
  }, []);

And then I called it in the options:

tooltips: {
            enabled: false,
            custom: customTooltip,
          }

From what I remember, not defining the customTooltip with a useCallback gave the error where opacity did not change. Hope it helps!

1reaction
PCPbiscuitcommented, Sep 1, 2021

None of you guys have a problem where hovering while using a custom tooltip rerenders the chart itself?

@PCPbiscuit I’m facing same issue, did you find any workaround ?

The only workaround which works for me is using test-react-chartjs2

Read more comments on GitHub >

github_iconTop Results From Across the Web

custom tooltip opacity always 1 - Stack Overflow
opacity === 0) { // never called because opacity is always 1 this.hide(); return; } // const position = this.chartRef.current.chartInstance.
Read more >
Custom Tooltip opacity always 1 · Issue #450 - GitHub
I need to have custom Tooltip and I design a react-component using hooks, check out the code: ... opacity === 0) { setTooltipData(null);...
Read more >
Developers - Custom Tooltip opacity always 1 - - Bountysource
I need to have custom Tooltip and I design a react-component using hooks, check out the code: import {Line} from "react-chartjs-2"; import {Button}...
Read more >
Tooltip - Chart.js
The bubble, doughnut, pie, polar area, and scatter charts override the tooltip defaults. To change the overrides for those chart types, ...
Read more >
Tooltips · Bootstrap v5.0
Documentation and examples for adding custom Bootstrap tooltips with CSS and ... One way to initialize all tooltips on a page would be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found