javascript - 未捕获的类型错误 : string is not a function - on google Chart

标签 javascript ajax json google-visualization

嗨,我有 2 个 google 图表(1 个日历和另一个 LineChart),我收到 Uncaught TypeError: string is not a functionUncaught TypeError: undefined is not a function >。昨天它工作正常,我可以看到这两个,但今天我收到此错误,我只能显示字符或日历之一。在单独的文件中,我没有收到任何错误,我修改了 json,它显示了我想要检索的数据。

<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

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

google.load("visualization", "1.1", {packages:["calendar"]});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawCalendar);


function drawCalendar() {

$.ajax({
    url: 'getCalendario.php',
    dataType: 'json',
    success: function (jsonData) {
   var options = {
     title: "Calendario",
     height: 200,

    daysOfWeek: 'LMMJVSD'
   };


    // Create our data table out of JSON data loaded from server.
    var data = new google.visualization.DataTable(jsonData);
      var chart = new google.visualization.Calendar(document.getElementById('calendar'));



        // Instantiate and draw our chart, passing in some options.

        chart.draw(data, options);
    }
});
$.ajax({
    url: 'getChart.php',
    dataType: 'json',
    success: function (jsonData) {
        // Create our data table out of JSON data loaded from server.
    var options = {
        title: 'Estado de Bateria',
        width: 800,
        height: 400,
        vAxis: {
        minValue: 0, 
        maxValue: 100,

        }

    };
                var data = new google.visualization.DataTable(jsonData);

                // Instantiate and draw our chart, passing in some options.
         var chartBat = new google.visualization.LineChart(document.getElementById('battery'));


                chartBat.draw(data, options);
            }
        });
}
</script>
</head>

<body>
<!--Div that will hold the pie chart-->
<div id="calendar" ></div>
<div id="battery"></div>
</body>
</html>

控制台图像

Image

最佳答案

此代码正在运行并绘制两个图表,问题是您忘记加载折线图依赖项。

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

google.load("visualization", "1.1", {packages:["calendar"]});
google.load("visualization", "1.1", {packages:["linechart"]});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawCalendar);


function drawCalendar() {

var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
      data.addColumn('number', 'X');
      data.addRows([
        [new Date(2008, 0, 1), 1],
        [new Date(2008, 1, 14), 2],
        [new Date(2008, 9, 14), 12],
        [new Date(2008, 10, 14), 22],
        [new Date(2009, 1, 1), 30]
      ]);
    // Create our data table out of JSON data loaded from server.
      var chart = new google.visualization.Calendar(document.getElementById('calendar'));



        // Instantiate and draw our chart, passing in some options.

        chart.draw(data, options);

var data2 = new google.visualization.DataTable();
        data2.addColumn('date', 'Date');
      data2.addColumn('number', 'X');

      data2.addRows([
        [new Date(2008, 0, 1), 1],
        [new Date(2008, 1, 14), 2],
        [new Date(2008, 9, 14), 12],
        [new Date(2008, 10, 14), 22],
        [new Date(2009, 1, 1), 30]
      ]);
    var options = {
        title: 'Estado de Bateria',
        width: 800,
        height: 400,
        vAxis: {
        minValue: 0, 
        maxValue: 100
        }

    };

                // Instantiate and draw our chart, passing in some options.
         var chartBat = new google.visualization.LineChart(document.getElementById('battery'));


                chartBat.draw(data2, options);
}

关于javascript - 未捕获的类型错误 : string is not a function - on google Chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717721/

相关文章:

javascript - OnClick 函数无法正常工作以重新定位内容

javascript - 正则表达式 : How to find only one match (or not match pattern)

jquery - jquery中变量的问题

c# - json 网络前导零(禁用基础转换)

python - 如何将 json 加载到 pandas 数据框中?

java - 从JAVA中的sql查询中获取JSON的所有行

javascript - 在 JavaScript 中制作全局变量的本地副本

javascript - jQuery 提交表单以获取文件下载吗?

javascript - jquery 失败不触发

php - AJAX 仅在数据库更改时更新 - Codeigniter