json 轴 #0 的数据列不能是谷歌图表中的字符串错误类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9195025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data column(s) for axis #0 cannot be of type string error in google chart
提问by Ramprasad
I tried to populate google chart datatable in server side using PHP.I got JSON file properply, but the Chart not display in client Application. I got error-Data column(s) for axis #0 cannot be of type string. My coding is below here.
我尝试使用 PHP 在服务器端填充谷歌图表数据表。我正确地获得了 JSON 文件,但图表未显示在客户端应用程序中。我收到错误 -轴 #0 的数据列不能是 string 类型。我的代码在下面。
After fetching data from database,
从数据库中获取数据后,
$colarray=array(array("id"=>"","label"=>"userid","pattern"=>"","type"=>"number"),array("id"=>"","label"=>"name","pattern"=>"","type"=>"string"));
$final=array();
for($i=0;$i<$rows;$i++)
{
$id[$i]=pg_fetch_result($res1,$i,'id');
$name[$i]=pg_fetch_result($res1,$i,'name');
$prefinal[$i]=array("c"=>array(array("v"=>$name[$i]),array("v"=>$name[$i])));
array_push($final,$prefinal[$i]);
}
$table['cols']=$colarray;
$table['rows']=$final;
echo json_encode($table);
My Output Json:
我的输出JSON:
{
"cols":[
{"id":"","label":"userid","pattern":"","type":"number"},
{"id":"","label":"name","pattern":"","type":"string"}
],
"rows":[
{"c":[{"v":"101"},{"v":"Aircel"}]},
{"c":[{"v":"102"},{"v":"Srini"}]},
{"c":[{"v":"103"},{"v":"Tamil"}]},
{"c":[{"v":"104"},{"v":"Thiyagu"}]},
{"c":[{"v":"105"},{"v":"Vasan"}]},
{"c":[{"v":"107"},{"v":"Senthil"}]},
{"c":[{"v":"108"},{"v":"Sri"}]},
{"c":[{"v":"109"},{"v":"Docomo"}]},
{"c":[{"v":"106"},{"v":"Innodea"}]}
]
}
How to solve this issue?
如何解决这个问题?
回答by Bernhard Wagner
To extend on @sajal's accurate answer: Change the last line of your code from:
要扩展@sajal 的准确答案:将代码的最后一行更改为:
echo json_encode($table);
to:
到:
echo json_encode($table, JSON_NUMERIC_CHECK);
This will tell json_encode to recognize numbers and abstain from wrapping them in quotes (Available since PHP 5.3.3.). http://php.net/manual/en/json.constants.php#constant.json-numeric-check
这将告诉 json_encode 识别数字并避免将它们用引号括起来(自 PHP 5.3.3 起可用。)。 http://php.net/manual/en/json.constants.php#constant.json-numeric-check
回答by sajal
You specify type of userid as number... but pass string.. thats causing the problem.
您将用户 ID 的类型指定为数字……但传递字符串……这就是问题所在。
I just wasted 30 mins with the opposite problem ...
我只是在相反的问题上浪费了 30 分钟......
Your output json should look like :-
您的输出 json 应如下所示:-
{
"cols":[
{"id":"","label":"userid","pattern":"","type":"number"},
{"id":"","label":"name","pattern":"","type":"string"}
],
"rows":[
{"c":[{"v":101},{"v":"Aircel"}]},
{"c":[{"v":102},{"v":"Srini"}]},
{"c":[{"v":103},{"v":"Tamil"}]},
{"c":[{"v":104},{"v":"Thiyagu"}]},
{"c":[{"v":105},{"v":"Vasan"}]},
{"c":[{"v":107},{"v":"Senthil"}]},
{"c":[{"v":108},{"v":"Sri"}]},
{"c":[{"v":109},{"v":"Docomo"}]},
{"c":[{"v":106},{"v":"Innodea"}]}
]
}
回答by Noumenon
On a BarChart, one of the columns (the second one) has to be a number. That can cause this error message.
在条形图上,其中一列(第二列)必须是数字。这可能会导致此错误消息。
回答by Ken.Fukizi
In your drawChart() function, you are probably using google.visualization.arrayToDataTable, and this does not allow any nulls. Please use addColumn function explicitly
在您的 drawChart() 函数中,您可能正在使用 google.visualization.arrayToDataTable,并且这不允许任何空值。请明确使用 addColumn 函数
回答by Gaurav Paliwal
If the Data format should be like:
如果数据格式应该是这样的:
data: [
["string", "string"], //first Column
["string1", number],
["string2", number],
["string3", number],
]
then you can overcome this error.
那么你可以克服这个错误。

