我有以下谷歌图表代码,该代码从我的网络服务器上的本地CSV文件中读取。
这很棒&效果很好。
但是,我将在“实时”使用的文件将自动生成并有一个10行的标题,我想忽略它(不应该读取数据)。
有没有办法改变这个脚本以忽略CSV文件的前10行?
<script type='text/javascript'>
// load the visualization library from Google and set a listener
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChartfromCSV);
function drawChartfromCSV(){
// grab the CSV
$.get("EDU_INST_SCHOOL_EXPENDTR_29_1.csv", function(csvString) {
// transform the CSV string into a 2-dimensional array
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
// this new DataTable object holds all the data
var data = new google.visualization.arrayToDataTable(arrayData);
// this view can select a subset of the data at a time
var view = new google.visualization.DataView(data);
view.setColumns([0,2]);
var options = {
title: "EDUCATIONAL INSTITUTIONS, SCHOOLS AND EXPENDITURE - INDIA",
hAxis: {title: data.getColumnLabel(0), minValue: data.getColumnRange(0).min, maxValue: data.getColumnRange(0).max},
vAxis: {title: data.getColumnLabel(2), minValue: data.getColumnRange(2).min, maxValue: data.getColumnRange(2).max},
legend: 'none'
};
var chart = new google.visualization.LineChart(document.getElementById('csv2chart'));
chart.draw(view, options);
});
}
</script>
答案 0 :(得分:0)
你可以在创建DataTable之前从数组中删除元素......
Array.prototype.shift()删除数组中的第一个元素
// transform the CSV string into a 2-dimensional array
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
// remove unwanted headers
var removeRows = 10;
while (removeRows--) {
arrayData.shift();
}
// this new DataTable object holds all the data
var data = new google.visualization.arrayToDataTable(arrayData);