问题描述
我有一个 csv 文件:
I have a csv file with that :
Site,Janvier,Février,Mars,Avril,Mai,Juin,Juillet,Août,Septembre,Octobre,Novembre,Décembre雪铁龙 VILLEFRANCHE CARROSSERIE,0,0,14,0,18,21,0,0,0,0,0,0雪铁龙自由城,240,237,230,264,219,285,219,130,4,0,0,0日产自由行,174,202,215,181,196,244,203,107,10,1,0,0
Site,Janvier,Février,Mars,Avril,Mai,Juin,Juillet,Août,Septembre,Octobre,Novembre,DécembreCITROEN VILLEFRANCHE CARROSSERIE,0,0,14,0,18,21,0,0,0,0,0,0CITROEN VILLEFRANCHE ,240,237,230,264,219,285,219,130,4,0,0,0NISSAN VILLEFRANCHE ,174,202,215,181,196,244,203,107,10,1,0,0
我想在柱形图中查看其数据.
I would like to see its data in a column chart.
但我不知道如何使用 API 读取 csv 的数据.这是带有 API 的柱形图示例:
But i do not know how to read the data of a csv with the API.Here is an example of a column chart with the API :
google.charts.load("current", {packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
["Element", "Density", { role: "style" } ],
["Copper", 8.94, "#b87333"],
["Silver", 10.49, "silver"],
["Gold", 19.30, "gold"],
["Platinum", 21.45, "color: #e5e4e2"]
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{ calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation" },
2]);
var options = {
title: "Density of Precious Metals, in g/cm^3",
width: 600,
height: 400,
bar: {groupWidth: "95%"},
legend: { position: "none" },
};
var chart = new google.visualization.ColumnChart(document.getElementById("columnchart_values"));
chart.draw(view, options);
如何更改数据加载以使用 csv 文件?
How to change the data load to use a csv file ?
推荐答案
你可以使用 jquery.csv
来解析 csv
you can use jquery.csv
to parse the csv
google.charts.load('current', {
callback: function () {
$.get("file_name.csv", function(csvString) {
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
var data = new google.visualization.arrayToDataTable(arrayData);
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data);
});
},
packages: ['corechart']
});
查看以下带有静态数据的工作片段
see following working snippet with static data
google.charts.load('current', {
callback: function () {
csvString = 'Site,Janvier,Février,Mars,Avril,Mai,Juin,Juillet,Août,Septembre,Octobre,Novembre,Décembre
CITROEN VILLEFRANCHE CARROSSERIE,0,0,14,0,18,21,0,0,0,0,0,0
CITROEN VILLEFRANCHE ,240,237,230,264,219,285,219,130,4,0,0,0
NISSAN VILLEFRANCHE ,174,202,215,181,196,244,203,107,10,1,0,0';
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
var data = new google.visualization.arrayToDataTable(arrayData);
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data);
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script>
<div id="chart_div"></div>
这篇关于如何通过谷歌图表 API 使用 csv 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!