Chartjs-JavaScript ignore commas inside ChartJS data field pulled from csv that data has commas and is enclosed by double quotes

1👍

The culprit is this line:

var numbersData = alerts.map(function(d) {return +d["Number of Times"]});

If you log the value of numbersData to the console you’ll see something like that:

Array(6) [ NaN, 877, NaN, 878, NaN, NaN ]

As you already figured it has to do with the comma inside your number. As you’re prepending a + before returning d["Number of Times"], it ultimately tries to convert the string e.g. 1,377 into a number, fails (because of the comma) and thus returns NaN – which means Not a Number.

One easy fix is replacing all the commas of numbers inside your .csv file by a dot (.).

1,377 -> 1.377

If you don’t want to modify the .csv file itself, you can replace all occurences of a comma by a dot using javascript:

var numbersData = alerts.map(function(d) {let num=d["Number of Times"]; num=num.replaceAll(",","."); return Number(num);});

Leave a comment