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

Annotation event not updating with Chart.js 2.7

Open attack68 opened this issue 9 years ago • 30 comments

It is entirely possible this is a bug with my code and understanding rather than the tool itself as it is very new to me,

I am using the following code to roughly test the tooltip hover mechanics for an annotation label:

[{
type: 'line',
mode: 'vertical',
// ..etc.
onMousemove: function(e) {
    console.log('Annotation', e.type, this);
    console.log(this.options.label.enabled);
    this.options.label.enabled = true;
    console.log(this.options.label.enabled);
},}]

the console shows the label.enabled property changing from false to true, but the chart doesn't display a change. I have tried linking with this.chartInstance.update() but still doesn't work.

The JS console error I receive is: Unable to preventDefault inside passive event listener invocation. dispatcher @ chartjs-plugin-annotation.js:261

I'll attempt to provide demo unless you quickly spot a mistake I'm making.

attack68 avatar Oct 11 '17 15:10 attack68

You will need to call update() on your chart instance to apply the change. See https://github.com/chartjs/chartjs-plugin-annotation/blob/master/samples/horizontal-line.html for a working example.

compwright avatar Oct 18 '17 14:10 compwright

Actually I did try this after looking at that example and some other comments. However my problem still persists.

I reproduced the example you offer directly with the same errors as I receive in my own chart.

screen shot 2017-10-18 at 17 07 20

Could this be some other kind of dependency? I use Mac Os Sierra and tested in Chrome and Safari and no browser showed me annotations reacting to events.

attack68 avatar Oct 18 '17 15:10 attack68

@attack68 have you tried the example I linked to to see if it works in your environment?

What version of chartjs-plugin-annotation and chart-js are you using?

compwright avatar Oct 20 '17 15:10 compwright

Also what version of Chrome are you running?

Can you go to chrome://flags and check your "Passive Event Listener Override" option (make sure it is set to Default)?

compwright avatar Oct 20 '17 15:10 compwright

Yes I took the code you supplied in the link and substituted only two lines (the script sources): My local version of annotation is * Version: 0.5.6. Chrome I am running: Version 61.0.3163.100 (Official Build) (64-bit) I have made sure my Passive Event Listener Override is set to default, it was.

I experience the same issue in Firefox and Safari, running Mac Os Sierra. I experience the issue on two separate Macs.

attack68 avatar Oct 20 '17 16:10 attack68

@attack68 Can you try downgrading to Chart.js 2.5?

compwright avatar Oct 20 '17 17:10 compwright

Just downgraded, annotation events works with 2.5.0. Additionally the plot looks different, instead of only scatter points in 2.7.0, in 2.5.0 it has a bezier curve through the scatter points.

attack68 avatar Oct 20 '17 21:10 attack68

You can turn off the line, see the Chart.js documentation.

compwright avatar Oct 20 '17 22:10 compwright

This also happens to me. This is what I using to generate chart.

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.bundle.js"></script>
<script src="chartjs-plugin-annotation.js"></script>

After I change my chart.js lib from v-2.7 to v-2.5, it works as I expected. :)

janaka120 avatar Oct 24 '17 12:10 janaka120

Bump! This is forcing me to use 2.5 while I would need some features from the 2.7 as well.

osro avatar Nov 17 '17 11:11 osro

@compwright any update on this issue? because i am having a problem. in updating.

adeel-sohail avatar Dec 29 '17 12:12 adeel-sohail

@compwright Hi all,

I am also having same problem and downgrading to chart.js to 2.5.0 made my annotations totally disappear.

dinogit avatar Dec 29 '17 14:12 dinogit

I'm not actively using this at this time with any of my projects and would welcome help if anyone wants to submit a pull request to update this plugin to work with 2.7. I'll get to it when I can, but I can't tell you when that will be.

compwright avatar Dec 29 '17 14:12 compwright

@compwright i am quite new to the javascript and did not know much, so that i can go through the source files. I would love to help in this regard. If you can tell what problem should actually be in this problem may be i can dig in and can help you. I am a regular user of these plugins and chartjs and would love to help in this regard.

adeel-sohail avatar Dec 29 '17 14:12 adeel-sohail

Hi all,

I have able to make it work by destroying and creating chart again.

dinogit avatar Dec 29 '17 16:12 dinogit

I was able to work around this problem. Upon inspecting the Charts options, it looks like something has changed in Chart.js where it is coping the annotations array making it inaccessible from the annotations plugin. Aka, you try to add line into the annotations array and it doesn't appear in the charts options.annotations array.

To work around this I had to use something like this. . .

Chart.helpers.each(Chart.instances,(instance) => { if(chartId == instance.canvas.id){ instance.options.annotation.annotations.push({ [annotation configuration here]}); } }

rather than doing something like this. . .

annotations = [{. . .}] options.annotation.annotations = annotations

It seems that this pass by reference approach doesn't work and you have to modify the property directly on the chart instance.

I tried to look at the annotations code to fix this but it's beyond me. Hopefully this work around at least helps people use chart.js 2.7.x until the author of this library gets a patch in place.

bsmith-zoll avatar Jan 04 '18 16:01 bsmith-zoll

Quick update here. I believe I have tracked the problem to chartjs and some work that was done leading up to 2.7.x release. At this point I'm not convinced it's a problem with this plugin, but rather something that needs to be fixed/weighed in on by the chartjs team. For details of my findings please refer to this link. https://github.com/chartjs/Chart.js/issues/5111

bsmith-zoll avatar Jan 05 '18 18:01 bsmith-zoll

@compwright Per the thread I have opened with the chart.js team, I think some of the sample files need to be updated. I also think that you should put a section on the front page of the annotation as well as the draggable plugins github page stating that if someone is upgrading from 2.x to 2.7.x that they will likely need to change how/where they are populating the annotation data and getting it onto the chart object. I'm happy to update the sample files, but I'm not sure what kind of process I would need to follow to update the front page. Is this something you can do for me if I type up the verbiage?

bsmith-zoll avatar Jan 08 '18 15:01 bsmith-zoll

@nullpointer77 you can update it by making a pull request with your changes to README.md. Thanks!

compwright avatar Jan 09 '18 03:01 compwright

I'm trying to understand if this issue is what I'm experiencing. I've created a chart and added a vertical line annotation. Now after updating some data I want to update the value of my vertical line. chartObject.config.options.annotation.annotations[1].value = newValue; chartObject.update()

chartObject.options.annotation.annotations[1].value = newValue; didn't work either.

But the line doesn't move, just stays at the original value.

Is this issue representative of what I'm trying to do and it not working?

DeanDavis avatar Mar 15 '18 20:03 DeanDavis