Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onhover line #11695

Open
2 of 3 tasks
hrmj008 opened this issue Mar 1, 2024 · 0 comments
Open
2 of 3 tasks

onhover line #11695

hrmj008 opened this issue Mar 1, 2024 · 0 comments

Comments

@hrmj008
Copy link

hrmj008 commented Mar 1, 2024

Documentation Is:

  • Missing or needed?
  • Confusing
  • Not sure?

Please Explain in Detail...

just want to change point coler when hover line, where to implement onhover function

following is my code

const line_chart = new Chart(ctx, {
    type: 'line',
    data: data2,
    options: {
        interaction: {
            mode: 'point'
        },
        onHover: (_, elements) => {
                line_chart.data.datasets[0].pointBackgroundColor = 'red'
                line_chart.update()
        },
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
           
            legend: {
                // position: 'top',
                display: false,
            },
            title: {
                display: false,
    
            },
            tooltip: {
                // Disable the on-canvas tooltip
                enabled: false,
    
                external: function(context) {
                    // line_chart.data.datasets[0].pointBackgroundColor = 'red'
                    // Tooltip Element
                    let tooltipEl = document.getElementById('dashboard_chart_tooltip');
                    tooltipEl.style.display = 'flex';
                    
                    const tooltipModel = context.tooltip;
                    if (tooltipModel.opacity === 0) {
                        tooltipEl.style.display = 'none';
                        // line_chart.data.datasets[0].pointBackgroundColor = 'transparent'
                        // line_chart.update()
                        return;
                    }
    
                 
                    const position = context.chart.canvas.getBoundingClientRect();
                    const bodyFont = Chart.helpers.toFont(tooltipModel.options.bodyFont);
                    // let data = fake_data.daily.find(item => item.label == tooltipModel.title[0])
                    document.getElementById('dashboard_chart_tooltip_item_title').innerHTML = 1
                    document.getElementById('dashboard_chart_tooltip_item_alert_data').innerHTML = 2
                    document.getElementById('dashboard_chart_tooltip_item_feedback_data').innerHTML = 3
                    document.getElementById('dashboard_chart_tooltip_item_feedback_in_time_data').innerHTML = 4
                    // Display, position, and set styles for font
                    tooltipEl.style.opacity = 1;
                    tooltipEl.style.position = 'absolute';
                    tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
                    tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
                    tooltipEl.style.font = bodyFont.string;
                    tooltipEl.style.padding = tooltipModel.padding + 'px ' + tooltipModel.padding + 'px';
                    tooltipEl.style.pointerEvents = 'none';
                    tooltipEl.style.marginLeft = '20px';
                    // line_chart.update()
                }
            }
            
        },
        scales: {
            x: {
                display: false,
                
            },
            y: {
                display: false
               
            }
        }
        
    },
});

but on hover function trigger when mouse reach canvas (not line)

Screenshot 2024-03-01 at 13 55 18

Your Proposal for Changes

need code example

Example

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant