javascript - 来自 Json 的 Google 图表 "undefined is not a function"

标签 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}

这对我来说看起来不错,我无法弄清楚我缺少什么,因为我只能在图表中看到一个错误(“未定义不是函数”)。我的 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);
            }
        }
    });
}

最佳答案

在DataTable JSON结构中使用日期时,必须省略new关键字;您正在构造一个字符串,可视化 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
}

关于javascript - 来自 Json 的 Google 图表 "undefined is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24996127/

相关文章:

javascript - 抛出错误但 Jest 的 `toThrow()` 没有捕获错误

javascript - 在 React 中使用按钮过滤数据

javascript - 如何通过 JSON 代码运行循环以仅查找 javascript 中的特定字符串?

javascript - 从 JSON 制作数组

javascript - Google 图表工具 - 重叠标签

sorting - 如何在谷歌图表中将首次点击排序设置为降序

javascript - Facebook 无限循环,setTimeout 没有帮助 =(

javascript - 有没有一种方法可以使用 Javascript 或 jQuery 隐藏列表中与元素和相邻列的高度相关的元素?

javascript - 从 JSON 重新填充复杂的 javascript 对象

javascript - 我如何在 Json 文件中编写此数据的结构?