CSV数据加载给出没有足够的列来绘制所请求的图表 [英] CSV data loading give Not enough columns given to draw the requested chart
问题描述
请参阅我已经看到相关的线程,但他们没有帮助。
Please see I already saw related threads but they did not help.
我创建的折线图格式与: https://developers.google.com/chart/interactive/docs/gallery/linechart#Example
I am creating line chart which is exactly same format as : https://developers.google.com/chart/interactive/docs/gallery/linechart#Example
差异是我使用csv数据,这是从csv读取的正确方法。
Difference is I am using csv data, which is correct way to read from csv.
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script src="http://jquery-csv.googlecode.com/files/jquery.csv-0.71.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
$.get("Chart3-data1.csv", function(csvString) {
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
var data = new google.visualization.arrayToDataTable(arrayData);
var options =
{
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('gauge'));
chart.draw(data, options);
});
}
</script>
</head>
<body>
<div id="gauge" style="width: 900px; height: 500px;"></div>
</body>
</html>
csv数据采用以下格式:
csv data is in this form:
"Date,Term,Segment,Visitor"
"2014/01/01,2014/01/01 - 2014/01/31,All visitors,2"
"2014/01/01,2014/01/01 - 2014/01/31,New user,0"
"2014/01/01,2014/01/01 - 2014/01/31,Mobile traffic,0"
"2014/01/02,2014/01/01 - 2014/01/31,All visitors,7"
"2014/01/02,2014/01/01 - 2014/01/31,New user,1"
"2014/01/02,2014/01/01 - 2014/01/31,Mobile traffic,0"
"2014/01/03,2014/01/01 - 2014/01/31,All visitors,5"
"2014/01/03,2014/01/01 - 2014/01/31,New user,0"
"2014/01/03,2014/01/01 - 2014/01/31,Mobile traffic,0"
"2014/01/04,2014/01/01 - 2014/01/31,All visitors,3"
"2014/01/04,2014/01/01 - 2014/01/31,New user,1"
"2014/01/04,2014/01/01 - 2014/01/31,Mobile traffic,0"
错误:
Not enough columns given to draw the requested chart
推荐答案
不要在CSV文件中的每一行引用引号。在CSV中,当单个数据元素包含分隔符(例如,以允许包含逗号的值。所以你写的是使每一行都是一个值,而不是4个值。
Don't put quotes around each line in the CSV file. In CSV, you put quotes around an individual data element when it contains a delimiter, e.g. to allow values that contain comma. So what you've written makes each line a single value, not 4 values.
如果你不能改变输入数据,你可以删除字符串,然后将其作为CSV处理:
If you can't change the input data, you can remove the extraneous quotes from the string before processing it as CSV:
csvstring = csvstring.replace(/^"|"$/gm, '');
这篇关于CSV数据加载给出没有足够的列来绘制所请求的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!