jquery load with chart.js -


i'm loading parts of page jquery load , want use chart.js plugin build chart part of page. load html loads fine , chart build if refresh page, sometimes, never builds chart on first page load. need defer or promise here?

yes, if loading html canvas element using jquery .load(), need put chart initialization in complete callback, so

$("#result").load("canvas.html", function () {     var data = {         labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g'],         datasets: [             {                 data: [12, 23, 23, 43, 45, 12, 33]             }         ]     };      var ctx = document.getelementbyid("mychart").getcontext("2d");     var mylinechart = new chart(ctx).line(data); }); 

where canvas.html is

<canvas id="mychart" height="300" width="500"></canvas> 

the canvas element needs have non-zero render size when data loaded, otherwise chart won't show up.

in cases, putting new chart... in right place (for e.g. in callback, after tab visible, etc.) enough. when it's not possible identify such point, using settimeout enough delay guarantee canvas element rendered work.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -