chartjs-plugin-crosshair icon indicating copy to clipboard operation
chartjs-plugin-crosshair copied to clipboard

Cannot read property 'dragStarted' of undefined

Open pratikdhavale opened this issue 5 years ago • 15 comments

Uncaught TypeError: Cannot read property 'dragStarted' of undefined at Object.beforeTooltipDraw (chartjs-plugin-crosshair.js:394) at Object.notify (Chart.min.js:10) at t._drawTooltip (Chart.min.js:10) at t.draw (Chart.min.js:10) at t.render (Chart.min.js:10) at Object.callback (Chart.min.js:10) at Object.advance (Chart.min.js:10) at Object.startDigest (Chart.min.js:10) at Chart.min.js:10

pratikdhavale avatar May 12 '19 14:05 pratikdhavale

+1 on this.

I'm also using the plugin with react-chartjs-2, chart.js version 2.7.3

filingroove avatar Jun 11 '19 07:06 filingroove

im also getting this problem.

knvpk avatar Jun 15 '19 06:06 knvpk

I checked out the code and found that the x-axis should be type linear or time, then plugin is working fine.

knvpk avatar Jun 15 '19 06:06 knvpk

Well that sucks. That means it isn't compatible with https://github.com/nagix/chartjs-plugin-streaming since it uses a "realtime" axis.

iamstickfigure avatar Aug 01 '19 19:08 iamstickfigure

Until this gets fixed, if you're also using the streaming plugin, you can get it to work with "realtime" scales by making a small change to the crosshair plugin's JS. Replacing all conditionals checking for "time" scales to use "realtime" as an alternative.

ie. "linear"===a||"time"===a becomes "linear"===a||"time"===a||"realtime"===a

"time"===t.options.scales.xAxes[0].type? becomes ("time"===t.options.scales.xAxes[0].type||"realtime"===t.options.scales.xAxes[0].type)?

"linear"!==t&&"time"!==t becomes "linear"!==t&&"time"!==t&&"realtime"!==t

iamstickfigure avatar Aug 01 '19 20:08 iamstickfigure

I have a similar issue with using react-chartjs2. When I specify the x-axis as time,

options={{     
    suppressTooltips: true,
    responsive: true,
    scales: {
        xAxes: [{
            type: 'time'
        }]
    },
    tooltips: {
        mode: 'index',
        intersect: false
    }
}}

with the data,

datasets={
    ...
    data=[
        {x: 1565155933153, y: 1.5},
        {x: 1565155933163, y: 1.6},
        ...
    ]
}

However it still gives the error dragStated undefined.

raymond1927 avatar Aug 07 '19 05:08 raymond1927

@raymond1927 Any chance you have more than one chart on that page? If so, any charts which should not have the crosshair need to disable this plugin. Otherwise, this plugin will try to apply itself to every chart, even if it doesn't have the right scale type.

iamstickfigure avatar Aug 07 '19 15:08 iamstickfigure

@iamstickfigure Thanks for the help, how do I disable a plugin per chart.

raymond1927 avatar Aug 08 '19 02:08 raymond1927

@raymond1927 In the place where you'd normally put the options for the plugin, just use false.

https://www.chartjs.org/docs/latest/developers/plugins.html#disable-plugins

options={     
    suppressTooltips: true,
    responsive: true,
    scales: {
        xAxes: [{
            type: 'time'
        }]
    },
    tooltips: {
        mode: 'index',
        intersect: false
    },
    plugins: {
        crosshair: false
    }
}

iamstickfigure avatar Aug 08 '19 15:08 iamstickfigure

I tried the suggested options, which didn't work for me (using Chart.js 3.6.0). What I ultimately discovered is that for reasons unknown, the afterInit() hook never runs (guessing either a bug or change in Chart.js), which leads to the plugin not initialising correctly. Changing the hook from afterInit to either start() or beforeInit() resolves the problem.

It also requires the X axis scale to be named 'x' and not 'xAxes'.

I will submit a PR.

TimUnderhay avatar Oct 23 '21 21:10 TimUnderhay

Submitted PR https://github.com/AbelHeinsbroek/chartjs-plugin-crosshair/pull/88.

TimUnderhay avatar Oct 23 '21 22:10 TimUnderhay

Is there any update on this? I noticed the PR is still unmerged. This issue happens for me whenever moving mouse over the chart area before the page is finished loading.

lowkeynicc avatar Jan 03 '22 16:01 lowkeynicc

nudge @AbelHeinsbroek

DaveSkender avatar Jan 16 '22 01:01 DaveSkender

Another temporary workaround that seems to work is to add optional chaining at trace.js#L277:

-    return !chart.crosshair.dragStarted && !chart.crosshair.suppressTooltips;
+    return !chart.crosshair?.dragStarted && !chart.crosshair.suppressTooltips;

JBaczuk avatar Jun 07 '22 23:06 JBaczuk

We just had to remove this plugin from a large production application given it intermittently caused blank screens for for our users. It would be an awesome one to fix, especially given there already appear to be proposed solutions.

ilias-t avatar May 10 '23 22:05 ilias-t