Closed drnic closed 3 years ago
Thanks for sharing @drnic!
hey @drnic, thanks so much for this!!
I'm using stimulus in my project as well, and was searching for how to set it up with chartjs, and your post is really helpful!
The reason for the
setTimeout(.., 1000)
is that the chart.js'sudpate()
method is called multiple times internally, which can override mychartConfig
. So I just wait a second and then invokeupdate()
and hope that's good enough. I'd look for a better way to inject myconfig.options.onClick
handler in future.
Any updates on this? Did you find a better solution since posting? Did you run into any problems with just using setTimout
?
(I know this is closed, but this is exactly my issue!)
It seems to me that the timeout is needed to wait for the data to be loaded from the remote URL. If you don't have remote data, but instead have inline, you can get rid of the setTimeout
. It would be nice if there were a way to just do this on a callback or event after the data has been loaded.
This is a pro-tip for #64 which I'm creating as a fresh issue (and then closing) because comments are locked on #64.
I wanted to register an
onClick
handler on Chart.js without giving up on chartkick/chartkick.js. Here's what I did to get something working:Whilst
defer: false
is the default, I saw in #489 that it might becomedefer: true
by default in future; so making it explicit here for future proofing. The chart needs to exist asap so that the stimulus controller can find it.The reason for the
setTimeout(.., 1000)
is that the chart.js'sudpate()
method is called multiple times internally, which can override mychartConfig
. So I just wait a second and then invokeupdate()
and hope that's good enough. I'd look for a better way to inject myconfig.options.onClick
handler in future.I am using grouped/stacked column charts, and alas the event handler is not given enough information to discern which feature/column/stack/series was clicked, only that "something in x-axis 3 was clicked"