google.visualization.PieChart 不是构造函数

google.visualization.PieChart is not a constructor

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

<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
});