我正在使用Google Charts并不断收到错误:#0号轴的数据列不能是字符串类型。我相信这是因为在我传入的数组中,存在字符串值,其中应该有数字值。我的数组是n列8行。第一行中的所有值都应该是数字,并且第一列中的所有值都应该是字符串,其余值应该是其余数字。我正在抓取查询字符串中的所有值,当我拆分它们时,它们会以字符串的形式出现,我相信。
这是我的网址查询字符串: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);
}
是否考虑过如何将不在第一行或第一列中的所有值强制转换为数字而不是字符串?
答案 0 :(得分: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) );