admin管理员组

文章数量:1323015

I am following the Chartjs documentation and have two arrays (labels & datasets) within my data object and it appears that when I try to define properties within the dataset array, I am running into an error message, Uncaught TypeError: Cannot read property 'datasets' of undefined, and haven't been able to figure out why. The information accurately matches the documentation, but for some reason it isn't working.

HTML:

<!doctype html>
<html>

    <head>
        <title>Google Super Proxy Test</title>
        <script src="Chart.min.js"></script>
        <script src="chart-options.js"></script>
    </head>

<body>

    <div style="width: 50%">
        <canvas id="sessions-graph" height="450" width="600"></canvas>
    </div>


</body>

</html>

charts-options.js

window.onload = function(){

    // Get the context of the canvas element
    var ctx = document.getElementById("sessions-graph").getContext("2d");
    var sessionsGraph = new Chart(ctx).Bar(data); //Create a chart with "data" array

    var data = {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
            {
                label: "My First dataset",
                fillColor: "rgba(220,220,220,0.5)",
                strokeColor: "rgba(220,220,220,0.8)",
                highlightFill: "rgba(220,220,220,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data: [65, 59, 80, 81, 56, 55, 40]
            }
        ]
    };


    Chart.defaults.global = {

        //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
        scaleBeginAtZero : true,

        //Boolean - Whether grid lines are shown across the chart
        scaleShowGridLines : true,

        //String - Colour of the grid lines
        scaleGridLineColor : "rgba(0,0,0,.05)",

        //Number - Width of the grid lines
        scaleGridLineWidth : 1,

        //Boolean - If there is a stroke on each bar
        barShowStroke : true,

        //Number - Pixel width of the bar stroke
        barStrokeWidth : 2,

        //Number - Spacing between each of the X value sets
        barValueSpacing : 5,

        //Number - Spacing between data sets within X values
        barDatasetSpacing : 1

    }

}

I am following the Chartjs documentation and have two arrays (labels & datasets) within my data object and it appears that when I try to define properties within the dataset array, I am running into an error message, Uncaught TypeError: Cannot read property 'datasets' of undefined, and haven't been able to figure out why. The information accurately matches the documentation, but for some reason it isn't working.

HTML:

<!doctype html>
<html>

    <head>
        <title>Google Super Proxy Test</title>
        <script src="Chart.min.js"></script>
        <script src="chart-options.js"></script>
    </head>

<body>

    <div style="width: 50%">
        <canvas id="sessions-graph" height="450" width="600"></canvas>
    </div>


</body>

</html>

charts-options.js

window.onload = function(){

    // Get the context of the canvas element
    var ctx = document.getElementById("sessions-graph").getContext("2d");
    var sessionsGraph = new Chart(ctx).Bar(data); //Create a chart with "data" array

    var data = {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
            {
                label: "My First dataset",
                fillColor: "rgba(220,220,220,0.5)",
                strokeColor: "rgba(220,220,220,0.8)",
                highlightFill: "rgba(220,220,220,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data: [65, 59, 80, 81, 56, 55, 40]
            }
        ]
    };


    Chart.defaults.global = {

        //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
        scaleBeginAtZero : true,

        //Boolean - Whether grid lines are shown across the chart
        scaleShowGridLines : true,

        //String - Colour of the grid lines
        scaleGridLineColor : "rgba(0,0,0,.05)",

        //Number - Width of the grid lines
        scaleGridLineWidth : 1,

        //Boolean - If there is a stroke on each bar
        barShowStroke : true,

        //Number - Pixel width of the bar stroke
        barStrokeWidth : 2,

        //Number - Spacing between each of the X value sets
        barValueSpacing : 5,

        //Number - Spacing between data sets within X values
        barDatasetSpacing : 1

    }

}
Share Improve this question asked Aug 26, 2014 at 16:44 cphillcphill 5,92418 gold badges102 silver badges194 bronze badges 1
  • 2 You're using data before you assign it! – Barmar Commented Aug 26, 2014 at 16:47
Add a ment  | 

1 Answer 1

Reset to default 7

You have to assign data before you pass it to the function:

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        }
    ]
};

var sessionsGraph = new Chart(ctx).Bar(data); //Create a chart with "data" array

You probably should also assign Chart.default.global before calling it, but I'm not sure when that's used.

本文标签: javascriptTypeError Not reading property correctlyStack Overflow