Thakane Selebalo
Thakane Selebalo

Reputation: 21

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>

Upvotes: 1

Views: 2227

Answers (2)

ShaileshKumarMPatel
ShaileshKumarMPatel

Reputation: 153

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.

Upvotes: 0

Vishwas R
Vishwas R

Reputation: 3420

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.

Upvotes: 4

Related Questions