Chartjs-Chart.js building dynamic charts, JSON data parsing issue

0👍

You could use Array#map.

var chartData = "30 99";
var res = chartData.split(" ");
console.log("Before:");
console.log(res);

console.log("After:");
console.log(res.map(function(x) {return x * 1;}));
.as-console-wrapper {
  position: relative;
  top: 0!important;
}
var chartData = "30, 99";
var res = chartData.split(", ");
console.log("Before:");
console.log(res);

console.log("After:");
console.log(res.map(function(x) {return x * 1;}));
.as-console-wrapper {
  position: relative;
  top: 0!important;
}

You need to use:

var kChart = new Chart(ctx, {
    type: 'pie',
    data: {
      labels: [chartLabels],
      datasets: [{
        label: 'Techs  ',
        data: data: res.map(function(x) {return x * 1;}),

0👍

The point of data formats such as JSON, XML, etc is that you don’t have to write so much custom code for parsing/serialising data. In that sense you’re not using JSON fully. I don’t know exactly what your situation is and whether you have control of the JSON structure. If not, you’ll just have to use Danny’s answer or something equivalent. If you do, change it to this:

"chartData": [30, 99]

0👍

data: res.map(function(x) {return x * 1;})

did the trick ! Thanks Danny !

Leave a comment