I use ChartJS to render some data parsed from CSV. CSV is well versed - I can verify this using console logs and other methods. Then I create an array for programming ChartJS. This array also looks good to me in the console, but apparently it doesn't work, since ChartJS gives me this error:
Uncaught TypeError: Cannot read property 'call' of undefined
Here is my code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script src="https://localspace/js/papaparse.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.3.1/randomColor.min.js" type="text/javascript"></script>
<script type="text/javascript">
var doughnutChartData = [];
Chart.defaults.global = {
responsive: false
}
$(document).ready(function(){
var parseResults;
Papa.parse("localspace/csv/06/Referring-Domains.csv", {
download: true,
comments: "#",
complete: function(results) {
parseResults = results.data;
console.log(parseResults);
for(i=0;i<parseResults.length;i++){
if(i!=0&&i<6){
$( "#referringDomainsTable tbody" ).append( "<tr><td>"+parseResults[i][1]+"</td><td>"+parseResults[i][2]+"</td><td>"+parseResults[i][3]+"</td></tr>" );
doughnutChartData.push({
value: parseResults[i][3].slice(0,-1),
color: randomColor(),
label: parseResults[i][1]
});
}
}
console.log(doughnutChartData);
setTimeout(function(){
var ctx = $("#referringDomainsChart").get(0).getContext("2d");
var myNewChart = new Chart(ctx).Doughnut(doughnutChartData);
},3000);
}
});
});
</script>
<h2>Referring Domains</h2>
<canvas id="referringDomainsChart" width="400" height="400"></canvas>
<table id="referringDomainsTable">
<thead>
<tr>
<th>Referring Domains</th>
<th>Instances</th>
<th>Percent</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
The setTimeout function was added to ensure that the data was loaded after the array was filled. This does not matter for the error; there is an error, regardless of whether the code is complete in setTimeout.
: , , ( , ) . , - , .