ChartJS: Uncaught TypeError: Cannot read property 'call' of undefined
Asked Answered
B

1

5

I am using ChartJS to render some data parsed from a CSV. The CSV parses fine--I can verify this with console logs and other methods. I then generate the array to feed ChartJS programmatically. This array also looks fine to me in the console, but apparently it's not working, 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
    // animation: true
}
$(document).ready(function(){
    var parseResults;
    Papa.parse("localspace/csv/06/Referring-Domains.csv", {
        download: true,
        comments: "#",
        // header: true,
        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(){
                // Get context with jQuery - using jQuery's .get() method.
                var ctx = $("#referringDomainsChart").get(0).getContext("2d");
                // This will get the first returned node in the jQuery collection.
                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 populated. It does not make a difference to the error, the error exists whether or not the code is wrapped in setTimeout.

Note, the chart actually appears and has color and correct values--but I still get this error and labels (or animations, when I had that line uncommented) do not work. Hope someone has an answer, thanks for reading.

Balthasar answered 15/7, 2015 at 15:34 Comment(0)
S
7

Replace

Chart.defaults.global = {
    responsive: false
    // animation: true
}

with

Chart.defaults.global.responsive = false;
// Chart.defaults.global.animation = true;

You don't want to replace all the defaults, just the ones you want.

Soutache answered 15/7, 2015 at 16:28 Comment(2)
Thanks a lot, very quick and (of course) accurate answer!Balthasar
I cannot see global. I used ChartJS.defaults.responsiveArvell

© 2022 - 2024 — McMap. All rights reserved.