谷歌图表不接受正确格式化数组作为行数据 [英] Google Charts not accepting correctly formatted array as row data

查看:129
本文介绍了谷歌图表不接受正确格式化数组作为行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问之前,我只是想在我已经试过点<一个href=\"http://stackoverflow.com/questions/14174331/google-charts-not-accepting-well-formed-array\">this回答,并没有为我工作。这不是一个重复的问题。

我插入阵列到谷歌图表为行数据,但是我收到错误未捕获的错误:给定的每一行必须是null或阵列中。控制台。

在登录我的数组中的控制台,它看起来像这样: [1,1.96,1.96,1.96] 这是完全一样的格式中显示正常阵列< A HREF =htt​​ps://developers.google.com/chart/interactive/docs/gallery/linechart相对=nofollow>的文档,但它仍然被拒绝。

这是为什么?我试过很多东西,我无法得到它的工作。

下面是我的code:

 的google.load('可视化','1.1',{包:'行']});
google.setOnLoadCallback(drawChart);功能drawChart(){    VAR数据=新google.visualization.DataTable();
    变种chartData = [];
    VAR JSON = $ .getJSON(myjsonlink'功能(数据){
        对于(i = 0; I&LT; data.length;我++)
        {
            chartData [I] = [];
            chartData [I] [0] = parseInt函数(数据[I]。销售);
            chartData [I] [1] = parseFloat(数据[I] .profit);
            chartData [I] [2] = parseFloat(数据[I] .profit);
            chartData [Ⅰ] [3] = parseFloat(数据[I] .profit);
        }
        的console.log(chartData [0]); //这个打印出[1,1.96,1.96,1.96]
    });    data.addColumn('号','一小时');
    data.addColumn('号','浏览量');
    data.addColumn('号','销售');
    data.addColumn('号','在$澳元收益');    data.addRows([
        chartData [0]
    ]);    VAR的选择= {
        宽度:450,
        身高:250
    };    VAR图=新google.charts.Line(的document.getElementById('今天与总销售'));    chart.draw(数据,选项);
}


解决方案

<击>你搞乱的东西与你的 data.addRows([chartData [0]); 。由于 addRows的完整的API文档中说( numOrArray)应给予一个数字或数组来填充行,但你给它一个数组的数组中,因此这是行不通的。
       卸下括号 [] 围绕 chartData [0]

坦白说,我想我明白我在说什么,但开始打字后,我显然不。我做了一个小提琴在那里工作,无论是与填充行的 for循环和填充按行的一切行,或者通过完整的变量 chartData 而不是 chartData [0] 。检查出来在这个小提琴

有人谁拥有更好的理解是pciated增加,并在回复一个更好的解释非常AP $ P $。

Before I ask, I would just like to point at that I've tried this answer, and it didn't work for me. This is not a duplicate question.

I am inserting an array into Google Charts as row data, however I am receiving the error Uncaught Error: Every row given must be either null or an array. in the console.

When logging my array in the console, it looks like this: [1, 1.96, 1.96, 1.96] This is exactly the same format as normal arrays shown in the docs, yet it is still being rejected.

Why is this? I've tried many things and I can't get it to work.

Here's my code:

google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(drawChart);

function drawChart() {

    var data = new google.visualization.DataTable();
    var chartData = [];
    var json = $.getJSON('myjsonlink', function(data) {
        for (i = 0; i < data.length; i++)
        {
            chartData[i] = [];
            chartData[i][0] = parseInt(data[i].sales);
            chartData[i][1] = parseFloat(data[i].profit);
            chartData[i][2] = parseFloat(data[i].profit);
            chartData[i][3] = parseFloat(data[i].profit);
        }
        console.log(chartData[0]); // This prints out [1, 1.96, 1.96, 1.96]
    });

    data.addColumn('number', 'Hour');
    data.addColumn('number', 'Pageviews');
    data.addColumn('number', 'Sales');
    data.addColumn('number', 'Earnings in $AUD');

    data.addRows([
        chartData[0]
    ]);

    var options = {
        width: 450,
        height: 250
    };

    var chart = new google.charts.Line(document.getElementById('todays-total-sales'));

    chart.draw(data, options);
}

解决方案

You're messing something up with your data.addRows([chartData[0]]);. As said in the full API documentation addRows(numOrArray) should be given either a number or an array to populate your rows, but you're giving it an array within an array, therefore it doesn't work. Removing the brackets [] around chartData[0]

Frankly I thought I understood what I was talking about, but after starting typing I clearly don't. I made a fiddle where it is working, either populating the rows with a for-loop and populating everything row by row, or by passing the complete variable chartData instead of chartData[0]. Check it out at this fiddle.

Someone who have a better understanding would be very appreciated to add a reply with a better explanation.

这篇关于谷歌图表不接受正确格式化数组作为行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆