javascript - Chart.js zeros handling -


i'm working chart.js , render doughnut chart. want set initial chart total value 0 can render full " empty" chart. when instatiate chart zeros not render. cannot find how handle zeros in developer documentation.

var kpoints = 000; var mpoints = 000; var tpoints = 000; var cpoints = 000; var doughnutdata = [ {     value : kpoints,     color : "#ff8000" }, {     value : mpoints,     color : "#99cc00" }, {     value : tpoints,     color : "#0099cc" }, {     value : cpoints,     color : "#333333" }, ]; var ctx = $("#profilechart").get(0).getcontext("2d"); var mydoughnut = new chart(ctx).doughnut(doughnutdata); 

from reading source code chart.js i've found tries sum each of value fields in datasource before rendering chart (see use of segmenttotal here).

to workaround this, use null values , set 1 (or more) of data points small, near 0 value. i've used float notation here 1 of values:

var kpoints = null; var mpoints = null; var tpoints = null; var cpoints = 1e-10; 

after that, example below re-renders chart (after 3 second delay) different data values show case of values updating default "dark" chart filled out version:

settimeout(function () {      // generate new, random value each of data points     doughnutdata.foreach(function (item) {         item.value = math.floor((math.random() * 10) + 1);     });      var ctx = $("#profilechart").get(0).getcontext("2d");     var mydoughnut = new chart(ctx).doughnut(doughnutdata); }, 3000); 



jsfiddle example: http://jsfiddle.net/masterxen/6s9db/3/


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -