简体   繁体   English

从Google图表的输入中获取值

[英]Getting values from input for Google Charts

I have a page listing in table format some sales data. 我有一个以表格形式列出一些销售数据的页面。 I want to include a chart. 我想附一张图表。 Since I already have to retrieve the data for the table I figured I would also build the array for google and put it in a hidden input to retrieve it with javascript. 由于我已经必须检索该表的数据,因此我也想为Google构建数组并将其放入隐藏的输入中以使用javascript进行检索。 So this is the javascript 这就是javascript

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


google.setOnLoadCallback(drawChart);

function drawChart() {
    var chartD = document.getElementById('chartD').value;
    var data = google.visualization.arrayToDataTable([chartD]);
    var options = {'title':'Sales'};
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

This is the input I get the data from 这是我从中获取数据的输入

<input type="hidden" id="chartD" value="['Date', 'Units Sold'], 
    ['03-01', 12.00], ['03-04', 32.00], ['03-06', 6.00],
    ['03-08', 19.00], ['03-11', 10.00], ['03-13', 5.00], ['03-15', 0]">

But when I run this I get an error Not a valid 2D array I then copied the value straight from the view page source like this 但是当我运行它时,我得到一个错误信息, 不是有效的2D数组 ,然后我像这样直接从视图页面源中复制了值

var data = google.visualization.arrayToDataTable([['Date', 'Units Sold'], 
   ['03-01', 12.00], ['03-04', 32.00], ['03-06', 6.00], ['03-08', 19.00],
   ['03-11',    10.00], ['03-13', 5.00], ['03-15', 0]]);

And that worked just fine. 而且效果很好。 Does anyone have any idea what the problem is? 有谁知道这是什么问题吗?

This is normal javascript functionality and nothing unique to Google or otherwise odd. 这是正常的javascript功能,没有Google独有的功能或其他奇怪的功能。

Take your HTML hidden div element and toss it in a document: 将您的HTML隐藏的div元素放入文档中:

<input type="hidden" id="chartD" value="['Date', 'Units Sold'],
 ['03-01', 12.00], ['03-04', 32.00], ['03-06', 6.00],
 ['03-08', 19.00], ['03-11', 10.00], ['03-13', 5.00], ['03-15', 0]">

Now test this javascript: 现在测试这个JavaScript:

    var chartD = document.getElementById('chartD').value;
    var array = new Array(['Date', 'Units Sold'],      ['03-01',  12.00], ['03-04', 32.00], ['03-06', 6.00],     ['03-08', 19.00],  ['03-11', 10.00], ['03-13', 5.00], ['03-15', 0]);
    var array2 = new Array(chartD);
    alert(array.length);
    alert(array2.length);

This is the exact same behavior with standard javascript. 这与标准javascript完全相同。

When you define the object with copy-paste, you get an array with 8 rows, 2 columns (hence length 8). 使用复制粘贴定义对象时,将得到一个包含8行2列的数组(因此长度为8)。

When you define the object with a string, you get an array that has one row and one column (containing a really long string). 当用字符串定义对象时,将得到一个包含一行和一列的数组(包含一个很长的字符串)。

Why would you expect Google to act any differently? 您为什么期望Google采取不同的行动?

You either need to: 您要么需要:

  1. Pass in a fully formed 2D array to google 将完整的2D数组传递给Google
  2. Pass in an array-defining string in by hand 手动传入数组定义字符串

This is all covered in the documentation . 所有这些都包含在文档中

Expecting Google to have some funky behavior different from javascript probably isn't the best approach. 期望Google具有不同于javascript的时髦行为可能不是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM