我正在使用饼图来显示数据,这是我的代码,我添加了以下脚本

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <script type="text/javascript">
        google.charts.load('current', { 'packages': ['corechart', 'gauge'] });


和我用来显示聊天的功能:-

self.DrawServiceGraph = function (obj) {
                var dataArray = new Array();
                dataArray.push(['Services', 'Total']);
                var subCat = ko.utils.arrayFirst(self.SelectedServiceSubCategoryList(), function (o) { return o.Id === obj.Id; });
                if (subCat !== undefined) {
                    $.each(subCat.SSC_List(), function (index, item) {
                        var value = parseInt(item.Value());
                        dataArray.push([item.SSC_Name, value]);
                    });

                    var data = google.visualization.arrayToDataTable(dataArray);

                    var options = {
                        title: '', height: 230, width: 330,
                        chartArea: { left: 0, width: '100%' },
                        legend: { position: 'right', alignment: 'start', maxLines: 5, textStyle: { fontSize: 10 } }
                    };

                    var chart = new google.visualization.PieChart(document.getElementById('Services-chart' + obj.Id));
                    chart.draw(data, options);
                }
            }


然后我从行中得到错误:

google.visualization.Pie Chart不是构造函数,并且
google.visualization.arrayToDataTable不是构造函数

避免这种错误的可能解决方案是什么?

最佳答案

您是否在等待load语句完成?

使用callback知道何时可以开始绘图...

google.charts.load('current', {
  callback: function () {
    // begin drawing
  },
  packages: ['corechart', 'gauge']
});


- 要么 -

google.charts.load('current', {
  packages: ['corechart', 'gauge']
});
google.charts.setOnLoadCallback(function () {
  // begin drawing
});

关于javascript - google.visualization.PieChart不是构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42714876/

10-12 06:28