Google 图表未呈现并显示“数据 table 未定义”

Google Chart is not rendering and display " Data table is not defined " instead

我不是为什么我的 Google 图表没有呈现而是显示

我还注意到我在控制台上收到了这条错误消息。

Uncaught TypeError: Cannot read property 'danger' of undefined

然后,我检查了我的JSON档案,危险在那里。它指的是哪种危险?


JS

'use strict';

define(['jquery'], function($) {

  $(function() {

    var danger       = $('#pc-danger');
    var warning      = $('#pc-warning');
    var success      = $('#pc-success');
    var danger_list  = $('#pc-danger-list');
    var warning_list = $('#pc-warning-list');
    var success_list = $('#pc-success-list');

    var basePath = "/BIM/resources/js/reports/student/section-exercise/";

    $.ajax({

      url: basePath + "data.json",
      type: "GET",
      dataType : "json",

      success: function( objects ) {


        function updateInfo(x) {

          danger.html(objects[x].danger);
          warning.html(objects[x].warning);
          success.html(objects[x].success);

          danger_list.html(objects[x].danger_list);
          warning_list.html(objects[x].warning_list);
          success_list.html(objects[x].success_list);

        }

         // Load the Visualization API and the piechart package.
         google.load("visualization", "1", {packages:["corechart"]});
         google.setOnLoadCallback(drawChart());

         function drawChart() {

           var options = {
            width: 160,
            height: 160,
            chartArea: {
              left: 10,
              top: 20,
              width: "100%",
              height: "100%"
            },


            colors: ['#F46E4E',  '#F9C262' , '#ADB55E',],
            legend: 'none',
            enableInteractivity: false,
            pieSliceText: 'none',

          };

          var data = {};
          var chart = {};

          for (var object in objects) {

            var total = objects[object].danger + objects[object].warning + objects[object].success ;

            data[object] = google.visualization.arrayToDataTable([

              ['Piechart' , 'Number of Skills'],
              ['danger'   , ( objects[object].danger/total )  * 100  ],
              ['warning'  , ( objects[object].warning/total ) * 100  ],
              ['success'  , ( objects[object].success/total ) * 100  ],


              ]);

            object = object.toLowerCase();

            // piechart object
            var $el = $('#sa-piechart-' + object  );

            // button
            var button = $('.sa-report-btn-'+ object );

            // append
            var $el = $('#sa-piechart-' + object ).length ? $('#sa-piechart-' + object ) : $('<div id="sa-piechart-' + object +'"></div>').appendTo($('#sa-piechart-'+ object ));


            chart[object] = new google.visualization.PieChart($el[0]);

            chart[object].draw(data[object]);

            // attach click handler to the button
            button.click(function() {
              updateInfo(object);
              chart[object].draw(data[object]);

            });

          }

        }

      },

      error: function( xhr, status, errorThrown ) {
        alert( "Sorry, there was a problem!" );

        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
      },


    });

});

});

JSON

{

    "A": {

        "report_type": "chapter test",
        "section_num": "2",
        "assignment": "algebra 1",
        "date": "2/10/2015",
        "correct_num": "15",
        "avg_score": "89",
        "danger": "5",
        "danger_list": "5,10,15,19,23",
        "warning": "8",
        "warning_list": "3,7,11,13,14,16,21,22",
        "success": "12",
        "success_list": "1,2,4,6,8,9,12,17,18,20,24,25"
    },

    "B": {

        "report_type": "section exercise",
        "section_num": "2.2",
        "assignment": "algebra 1",
        "date": "2/09/2015",
        "correct_num": "11",
        "avg_score": "44",
        "danger": "12",
        "danger_list": "11,12,13,14,15,16,17,18,19,20,3,4",
        "warning": "2",
        "warning_list": "1,2",
        "success": "11",
        "success_list": "21,21,23,24,25,5,6,7,8,9,10"
    },

    "C": {

        "report_type": "test",
        "section_num": "1",
        "assignment": "algebra 1",
        "date": "1/10/2015",
        "correct_num": "15",
        "avg_score": "75",
        "danger": "0",
        "danger_list": "",
        "warning": "10",
        "warning_list": "1,2,3,4,5,6,7,8,9,10",
        "success": "15",
        "success_list": "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
    },

    "D": {

        "report_type": "practice test",
        "section_num": "1",
        "assignment": "algebra 1",
        "date": "1/09/2015",
        "correct_num": "15",
        "avg_score": "79",
        "danger": "5",
        "danger_list": "1,2,3,4,5",
        "warning": "0",
        "warning_list": "",
        "success": "20",
        "success_list": "6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
    }
}

任何帮助/提示/建议对我来说意义重大。

谢谢!

您正在将 DataTable 推到 data[object],即 A,B,C..,然后通过此行将其转换为小写:-

 object = object.toLowerCase();

所以在那之后当你得到那个对象时它在这一行中 return null:-

chart[object].draw(data[object]);

所以你只需要删除这一行:-

object = object.toLowerCase();

这里是演示(我只用了一个div来演示) Demo

希望对您有所帮助。