来自Json的Google Chart“未定义不是函数”

时间:2014-07-28 13:14:23

标签: javascript json google-visualization

我第一次尝试使用谷歌图表。我的Json如下

{\"cols\":[{\"id\":\"Date\",\"label\":\"Date\",\"type\":\"date\"},{\"id\":\"KeywordCount\",\"label\":\"count\",\"type\":\"number\"}],\"rows\":[{\"c\":
[{\"v\":\"new Date(2014725)\",\"f\":\"25 July 2014\"},{\"v\":\"77\",\"f\":\"77\"}]},{\"c\":
[{\"v\":\"new Date(2014724)\",\"f\":\"24 July 2014\"},{\"v\":\"101\",\"f\":\"101\"}]},{\"c\":
[{\"v\":\"new Date(2014723)\",\"f\":\"23 July 2014\"},{\"v\":\"100\",\"f\":\"100\"}]},{\"c\":
[{\"v\":\"new Date(2014722)\",\"f\":\"22 July 2014\"},
{\"v\":\"130\",\"f\":\"130\"}]}],\"p\":null}

这对我来说很好,我无法弄清楚我错过了什么,因为我只能看到图表中的错误(“undefined不是函数”)。我的Google图表的javascript文件是

google.load('visualization', '1', { 'packages': ['corechart'] });
var postDate = $('#ReportingWall').serialize();

function drawChartAll() {
    var jsonData = $.ajax({
        url: '/ReportingWall/analyseStats/',
        type: 'POST',
        data: postDate,
        dataType: 'json',
        async: false,
        success: function (response) {
        }
    }).responseText;

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

    console.debug(jsonData);
    console.debug(data);
    var chart = new google.visualization.LineChart(document.getElementById('charts_all'));
    chart.draw(data, options);

    var columns = [];
    var series = {};
    for (var i = 0; i < data.getNumberOfColumns() ; i++) {
        columns.push(i);
        if (i > 0) {
            series[i - 1] = {};
        }
    }

    var options = {
        title: 'Keywords:',
        width: 908,
        legend: {
            position: 'right'
        },
        legendFontSize: 14,
        chartArea: {
            left: 50,
            width: '80%'
        },
        series: series
    }

    google.visualization.events.addListener(chart, 'select', function () {
        var sel = chart.getSelection();
        // if selection length is 0, we deselected an element
        if (sel.length > 0) {
            // if row is undefined, we clicked on the legend
            if (sel[0].row === null) {
                var col = sel[0].column;
                if (columns[col] == col) {
                    // hide the data series
                    columns[col] = {
                        label: data.getColumnLabel(col),
                        type: data.getColumnType(col),
                        calc: function () {
                            return null;
                        }
                    };

                    // grey out the legend entry
                    series[col - 1].color = '#CCCCCC';
                } else {
                    // show the data series
                    columns[col] = col;
                    series[col - 1].color = null;
                }
                var view = new google.visualization.DataView(data);
                view.setColumns(columns);
                chart.draw(view, options);
            }
        }
    });
}

2 个答案:

答案 0 :(得分:2)

在DataTable JSON结构中使用日期时,必须省略new关键字;您正在构造一个字符串,Visualization API将解析为Date对象,而不是构建Date对象。

{
    "cols":[
        {"id":"Date","label":"Date","type":"date"},
        {"id":"KeywordCount","label":"count","type":"number"}
    ],
    "rows":[
        {"c":[{"v":"Date(2014725)","f":"25 July 2014"},{"v":77,"f":"77"}]},
        {"c":[{"v":"Date(2014724)","f":"24 July 2014"},{"v":101,"f":"101"}]},
        {"c":[{"v":"Date(2014723)","f":"23 July 2014"},{"v":100,"f":"100"}]},
        {"c":[{"v":"Date(2014722)","f":"22 July 2014"},{"v":130,"f":"130"}]}
    ],
    "p":null
}

答案 1 :(得分:1)

如果你清理一下,你可以在最后一行看到你有一个无用的&#34;作为最后一个字符

{"cols":[
    {"id":"Date","label":"Date","type":"date"},
    {"id":"KeywordCount","label":"count","type":"number"}],
"rows":[
    {"c":[{"v":"new Date(2014725)","f":"25 July 2014"},{"v":"77","f":"77"}]},
    {"c":[{"v":"new Date(2014724)","f":"24 July 2014"},{"v":"101","f":"101"}]},
    {"c":[{"v":"new Date(2014723)","f":"23 July 2014"},{"v":"100","f":"100"}]},
    {"c":[{"v":"new Date(2014722)","f":"22 July 2014"},{"v":"130","f":"130"}]}],
"p":null}"

也从类似PHP的示例开始

或者这个ajax http://www.santarosa.edu/~jperetz/projects/ajax-json/