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.

Issue with tooltip: mode 'index'

See original GitHub issue

Please check this code pen :- https://codepen.io/bhavin00-the-reactor/pen/abZqXWz

I used tooltip mode index. as I want only 1 tooltip item for each line, there is two line so I need 2 tooltip items (1 for each line).

When I used mode: index then It is displaying only 1 tooltip item for 1 line, but then there is issue with tooltip location some time…

I attached below image please check & help me to resolve this…

image

In above image, I am hovering before 3rd Jan place but tooltip is coming for 6th Jan…

Is there any other alternative right now. as It looks issue with mode: index

Expected Behavior

Current Behavior

Possible Solution

Steps to Reproduce

Context

Environment

  • Chart.js version: 2.9.4
  • Browser name and version: Chrome latest
  • Link to your project:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kurklecommented, Jan 13, 2021

index mode works with the data index and for proper functionality each dataset has to have equal amount of data. The pen uses 2.7.3, but if you are really using 2.9.4 as you say, then you’d probably get the result you are looking for with mode: 'nearest', axis: 'x', intersect: false

edit: nvm, it only works correctly in v3.

0reactions
bhavin00commented, Jan 13, 2021

Hey Guys,

I did 1 work around, I set, mode: x, so then there is many tooltipItems for same line,

to get only 1 tooltipItem for each line, I customized tooltip.body.

tooltips: {
        mode: 'x',
        axis: 'x',
        intersect: false,
        backgroundColor: '#FFFFFF',
        bodyFontSize: 13,
        bodyFontColor: '#333333',
        bodyFontFamily: 'Open Sans',
        titleFontColor: '#333333',
        titleFontFamily: 'Open Sans',
        titleFontStyle: 'bold',
        titleFontSize: 13,
        titleMarginBottom: 15,
        displayColors: true,
        xPadding: 15,
        yPadding: 15,
        bodySpacing: 12,
        callbacks: {
            label(tooltipItem, data) {
                let label = data.datasets[tooltipItem.datasetIndex].label || '';
                if (label) {
                    label += ' - ';
                }
                label += `${Math.round(tooltipItem.yLabel)}%`;

                return label;
            }
        },
        custom(tooltipModel) {
            if(get(tooltipModel, 'dataPoints.length', 0) > 1) {
                const dataPointIdexes = [];
                const result = [];
                const result1 = [];
                forEach(tooltipModel.dataPoints, (dp, index) => {
                    if(!includes(dataPointIdexes, dp.datasetIndex)) {
                        result.push(dp);
                        dataPointIdexes.push(dp.datasetIndex);
                        result1.push(tooltipModel.body[index]);
                    }
                });
                tooltipModel.dataPoints = result;
                tooltipModel.body = result1;
                tooltipModel.height = tooltipHeaderHeight + (tooltipItemHeight * tooltipModel.body.length);
            }
        }
    },
    hover: {
        mode: 'x',
        intersect: false
    }

Hope It will help someone.

I am closing this bug as It is already created. as per above message of @LeeLenaleee.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I change the appearance of tooltips with mode: index ...
I want to change how Chart.js renders tooltips when using index mode. By default, Chart.js displays a line break between tooltips for each ......
Read more >
Tooltip - Chart.js
If true, the tooltip mode applies only when the mouse position intersects with an element. If false, the mode will be applied at...
Read more >
CM Tooltip (CMTG) - Extras - Performance and Debug Section
The performance of the Glossary Index Page is low, and you're not using it. Enable embedded mode? - This option was designed to...
Read more >
Chart.js - Line Chart Tooltip Hover Mode - CodePen
Insecure Resource. The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https....
Read more >
Z-Index - Stacks
.z-base, z-index: 0; .z-selected, z-index: 25; .z-active, z-index: 50; .z-dropdown, z-index: 1000; .z-popover, z-index: 2000; .z-tooltip, z-index: 3000;.
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