javascript - Cannot initialize a pie chart with Chart.js -
i copying example chart.js , stuck error making is.
here code (taken examples):
<canvas id="mychart" width="400" height="400"></canvas> <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <script src=" https://cdnjs.cloudflare.com/ajax/libs/chart.js/1.0.2/chart.min.js" charset="utf-8"></script> <script type="text/javascript"> // context of canvas element want select var ctx = document.getelementbyid("mychart").getcontext("2d"); var mynewchart = new chart(ctx[0]).pie(data); var data = [{ value: 300, color: "#f7464a", highlight: "#ff5a5e", label: "red" }, { value: 50, color: "#46bfbd", highlight: "#5ad3d1", label: "green" }, { value: 100, color: "#fdb45c", highlight: "#ffc870", label: "yellow" }] </script>
this code gives me error:
uncaught typeerror: cannot read property 'canvas' of undefined
now if change mynewchart
var to:
var mynewchart = new chart(ctx).pie(data);
i no error , no chart.
what missing? feel it's obvious...
thanks
two issues code.
first, assigning properties data
after it's passed chart()
, pie chart appear empty since not passing data.
second, .getcontext('2d')
not return array singular rendering context (or null). ctx[0]
undefined.
modify code follows , should work.
var ctx = document.getelementbyid("mychart").getcontext("2d"); var data = [{ value: 300, color: "#f7464a", highlight: "#ff5a5e", label: "red" }, { value: 50, color: "#46bfbd", highlight: "#5ad3d1", label: "green" }, { value: 100, color: "#fdb45c", highlight: "#ffc870", label: "yellow" }]; var mynewchart = new chart(ctx).pie(data);
Comments
Post a Comment