Annotation event not updating with Chart.js 2.7
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.
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.
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.
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 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?
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)?
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 Can you try downgrading to Chart.js 2.5?
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.
You can turn off the line, see the Chart.js documentation.
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. :)
Bump! This is forcing me to use 2.5 while I would need some features from the 2.7 as well.
@compwright any update on this issue? because i am having a problem. in updating.
@compwright Hi all,
I am also having same problem and downgrading to chart.js to 2.5.0 made my annotations totally disappear.
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 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.
Hi all,
I have able to make it work by destroying and creating chart again.
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.
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
@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?
@nullpointer77 you can update it by making a pull request with your changes to README.md. Thanks!
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?