Skip to content

When drawn

hanzo2001 edited this page May 16, 2016 · 3 revisions

Introduction

Once the chart is drawn it may be of interest to attach some events or functionality to other elements on the page, but when the chart is refreshed (refinalized or rebuilt, not redrawn), you may find yourself with a broken control. The function attached to the whenDrawn option will be run every time the wrap has to make sweeping internal changes.

$('#id').CreateChart({
    ...
    whenDrawn: function () {
        var wrap = this;
    }
});

Control multiple invocations

In the likely event that the whenDrawn method contains code that must only be run once, use of a IIFE allows the generation of some static variables that can be checked as follows:

{
    whenDrawn: (function(){
        var hasBeenInvoked = false;
        ...
        return function () {
            if (hasBeenInvoked) {return;}
            hasBeenInvoked = true;
            ...
        };
    }())
}

Internals

The last method call that fires, if configured, is the whenDrawn method from the main configuration object. This method is called when the following have been accomplished

  • Initialization has finished
  • Wrap object is built
  • The chart is drawn and is processing a hardwired ready event
  • All features have been added

An example

The following setup attaches an event to a button that references a method on a Wrap. Because the event is only aware of the Wrap object (which will not change) we have to make sure that this function does not continue attaching the method over and over every time the Wrap has to be refinalized.

$('#<id>').CreateChart({
	...
	whenDrawn : (function(){
		var done = false;
		return function () {
			var wrap = this;
			if (done) {return;}
			done = true;
			$('#refresh').click(function(){
				wrap.refreshData({action:'randomData'});
			});
		};
	}())
});

Clone this wiki locally