2

我正在使用 Google Charts 并不断收到错误消息:轴 #0 的数据列不能是字符串类型。我相信这是因为在我传入的数组中,应该有数字值的地方有字符串值。我的数组是 n 列 x 8 行。第一行的所有值都应该是数字,第一列中的所有值都应该是字符串,其余的都是数字。我正在从查询字符串中获取所有值,当我拆分它们时,我相信它们会以字符串的形式出现。

这是我的 URL 查询字符串:1=Day,04/01/2013,04/08/2013,04/15/2013,04/22/2013,04/29/2013,05/06/2013&2=Monday,100,100,100,100,100,100&3=Tuesday,100,100,100,100,100,100&4=Wednesday,100,100,100,100,100,100&5=Thursday,100,100,100,100,100,100&6=Friday,100,100,100,100,100,100&7=Saturday,100,100,100,100,100,100&8=Sunday,100,100,100,100,100,100

这是我的javascript:

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var row1 = getUrlVars()["1"];
var row2 = getUrlVars()["2"];
var row3 = getUrlVars()["3"];
var row4 = getUrlVars()["4"];
var row5 = getUrlVars()["5"];
var row6 = getUrlVars()["6"];
var row7 = getUrlVars()["7"];
var row8 = getUrlVars()["8"];

var row1x=row1.split(","); 
var row2x=row2.split(","); 
var row3x=row3.split(","); 
var row4x=row4.split(","); 
var row5x=row5.split(","); 
var row6x=row6.split(","); 
var row7x=row7.split(","); 
var row8x=row8.split(","); 

var items = [row1x, row2x, row3x,row4x,row5x,row6x,row7x,row8x];



      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable(items);

        var options = {
          title: 'Members AM Open Rate'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }

关于如何将不在第一行或第一列中的所有值强制为数字而不是字符串的任何想法?

4

1 回答 1

0

关于如何将不在第一行或第一列中的所有值强制为数字而不是字符串的任何想法?

使用单独的数组:

var headers = [row1x];
var items = [row2x, row3x, row4x, row5x, row6x, row7x, row8x];

并对数组进行字符串化和解析以将数字字符串转换为数字:

/* Take the string, create an array, create a string, replace the quotes, return array */
var row3x = JSON.parse(JSON.stringify("3=Tuesday,100,100,100,100,100".split(",")).replace(/"(\d+)"/g,"$1"))

然后连接两个数组:

var data = google.visualization.arrayToDataTable(header.concat(items) );
于 2014-10-28T22:11:57.793 回答