Custom event for 'drawEnd'
Looking through the source and the examples, I'm trying to establish how to create a custom event handler via addEventHandler when the draw of the chart has finished. I can see how to create the event handler and emit it, but how exactly would your trigger the emit within the source creation of the svg?
Hi there. Can you give an example of what you'd like to achieve? This would make it easier to imagine what you're looking for. Do you want an event when the whole drawing is done? Or after each element is drawn?
I would like an event when the whole drawing is complete. I am trying to append a defs element inside the drawn svg in order to create a lineargradient, but the draw event will trigger the append when each element is drawn. Essentially, I'm hacking it together with this:
var appended = false;
chart.on('draw', function(data) {
if (!appended) {
// Do something once...
appended = !appended;
}
});
Obviously, it would be much nicer/cleaner to have an event that is triggered once the drawing is done in order to append the element inside the svg.
what about an event created right after the chart was created? The chart updates currently cause a re-creation anyway. In the future we'll probably be able to recycle some of the DOM elements on certain updates but for now a re-creation is fine. In the line chart this would go here: https://github.com/gionkunz/chartist-js/blob/develop/source/scripts/chartist.line.js#L175
What do you think?
Cheers Gion
Yeah, a created event there would be perfect. I just need to manipulate the svg after the entire chart is drawn. If you know of a better way to alter the svg that is created internally, that would work for my current situation as well.
Thanks so much for the quick reply.
If you like you could create a pull request for this. I think a created event wouldn't hurt :smile: . I can see this can be helpful. For creating gradients, patterns or filters too. We would need to add it to all three chart modules. Here are examples of the other events: https://github.com/gionkunz/chartist-js/blob/develop/source/scripts/chartist.line.js#L114-L122 or https://github.com/gionkunz/chartist-js/blob/develop/source/scripts/chartist.core.js#L538-L548
Hey, @gionkunz - just want to say I work with @jordanwade and have been working on this issue with him. We're happy to help out and take a stab at creating the event in a Pull Request. I'll work on getting that for you to review. Thanks again for the help!
Great stuff guys! :+1:
I'm closing this since it's merged and thanks again!
Hey guys, your created event PR is published now in release 0.4.0 :smile: And I'm using it for the live example of the new animation API!
Cheers Gion
Awesome! Thanks, Gion.
Hey guys, kudos for this solution! The created event works perfectly!