chartist icon indicating copy to clipboard operation
chartist copied to clipboard

Custom event for 'drawEnd'

Open jordanwade opened this issue 12 years ago • 11 comments

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?

jordanwade avatar Nov 10 '14 19:11 jordanwade

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?

gionkunz avatar Nov 10 '14 20:11 gionkunz

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.

jordanwade avatar Nov 10 '14 20:11 jordanwade

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

gionkunz avatar Nov 10 '14 20:11 gionkunz

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.

jordanwade avatar Nov 10 '14 20:11 jordanwade

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

gionkunz avatar Nov 10 '14 21:11 gionkunz

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!

drewbarontini avatar Nov 10 '14 21:11 drewbarontini

Great stuff guys! :+1:

gionkunz avatar Nov 10 '14 21:11 gionkunz

I'm closing this since it's merged and thanks again!

gionkunz avatar Nov 13 '14 14:11 gionkunz

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

gionkunz avatar Nov 16 '14 22:11 gionkunz

Awesome! Thanks, Gion.

drewbarontini avatar Nov 16 '14 22:11 drewbarontini

Hey guys, kudos for this solution! The created event works perfectly!

morettimatheus avatar Jun 24 '15 16:06 morettimatheus