简体   繁体   中英

CanvasJS chart not showing

I am trying to create a bar chart with data for years 2009 to 2015 of enrolled students in given majors at universities in Mauritius. I am using the Javascript below and console.log(data) shows that the chart has been rendered. However, it does not display.

var dataPoints = [];
var myChart = document.getElementById('myChart').getContext('2d');

$('#all').click(function(){
$.get("http://api.worldbank.org/v2/countries/mus/indicators/SE.TER.ENRL?date=2009:2015", function(data) {
    console.log(data);

    $(data).find("wb:data").each(function () {
        var $dataPoint = $(this);
        var x = $dataPoint.find("wb:date").text;
        console.log(x);
        var y = $dataPoint.find("wb:value").text();
        dataPoints.push({x: parseFloat(x), y: parseFloat(y)});
    });

    var chart = new CanvasJS.Chart("myChart", {
        title: {
            text: $(data).find("wb:indicator").text(),
        },
        data: [{
             type: "column",
             dataPoints: dataPoints,
          }]
    });

    chart.render();
    document.getElementById('myChart').style.display = "block";
});
});

This is the html. The canvas is initially set to display:none .

<canvas id="myChart">
</canvas>

Chart container should be a DOM and not Canvas. Please take a look at CanvasJS Documentation Page for more info. Changing <canvas id="myChart"> </canvas> to <div id="myChart"></div> and passing it to chart while creating it should work fine in your case.

You can also refer tutorial on Rendering Chart from XML Data from docs.

There are two different charting javascripts

  1. chart.js
  2. canvasjs

When someone transit from 1 to 2, this mistake occur.
I had difficulty using chart.js offline
So, I started with canvasjs
causing the mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM