如何使用谷歌图表API的csv文件?

时间:2017-07-03 15:06:49

标签: javascript html csv charts google-visualization

我有一个csv文件:

网站,维耶,FEVRIER,火星,艾薇,麦,朱安,JUILLET,AOUT,Septembre,OCTOBRE,十一月四日,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

我希望在柱形图中看到它的数据。

但我不知道如何使用API​​读取csv的数据。 以下是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文件?

2 个答案:

答案 0 :(得分:2)

您可以使用jquery.csv来解析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']
});

请参阅以下带静态数据的工作代码段



google.charts.load('current', {
  callback: function () {
    csvString = 'Site,Janvier,Février,Mars,Avril,Mai,Juin,Juillet,Août,Septembre,Octobre,Novembre,Décembre\nCITROEN VILLEFRANCHE CARROSSERIE,0,0,14,0,18,21,0,0,0,0,0,0\nCITROEN VILLEFRANCHE ,240,237,230,264,219,285,219,130,4,0,0,0\nNISSAN 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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

webpack为模块管理提供了很好的功能。 包括资产管理,您可以使用csv-loader导入csv并解析。

<强> webpack.config.js

var path = require('path');

module.exports = {
  entry: './src/yourscript.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }, 
       {
        test: /\.(csv|tsv)$/,
        use: [
          'csv-loader'
        ]
      },
      ]
  }
};

如果您是第一次来webpack

  • $ sudo npm install -g webpack
  • $ npm install webpack --save-dev
  • $ cat>webpack.config.js
  • $ npm install style-loader css-loader csv-loader --save-dev
  • 复制代码并粘贴到webpack.config.js
  • $ webpack

<强> /src/yourscript.js

import data form "./asset/yourcsv.csv";

component = ()=>{
  console.log(data);
}