如何在 google 可视化饼图中将导出 csv 选项设置为按钮

How to set export csv option to button in google visualization Pie chart

我正在使用 Google 可视化饼图来显示交易状态。 我想导出到 csv,工具栏有导出 csv 的功能,html,iGoogle 但我只想在没有 select 选项的情况下特定于 csv。

Google 将为您提供您需要在其中传递 API 密钥

<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart']});
var visualization;

function draw() {
  drawVisualization();
  drawToolbar();
}

function drawVisualization() {
  var container = document.getElementById('visualization_div');
  visualization = new google.visualization.PieChart(container);
  new google.visualization.Query('https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA').
      send(queryCallback);
}

function queryCallback(response) {
  visualization.draw(response.getDataTable(), {is3D: true});
}

function drawToolbar() {
  var components = [{type: 'csv', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA'},
  ];

  var container = document.getElementById('toolbar_div');
  google.visualization.drawToolbar(container, components);
};

google.charts.setOnLoadCallback(draw);
</script>
</head>
<body>
<div id="visualization_div" style="width: 270px; height: 200px;"></div>
<div id="toolbar_div"></div>
</body>
</html>

更多: https://developers.google.com/chart/interactive/docs/gallery/toolbar

你可以使用静态方法 --> dataTableToCsv

google.visualization.dataTableToCsv

这将在数据 table 中创建数据的 csv 字符串。

它不会导出列标题,但可以手动添加...

请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Label', 'Value'],
    ['A', 10],
    ['B', 20],
    ['C', 30],
    ['D', 40],
    ['E', 50],
    ['F', 60]
  ]);

  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data);

  $('.csv-button').on('click', function () {
    var browserIsIE;
    var csvColumns;
    var csvContent;
    var downloadLink;
    var fileName;

    // build column headings
    csvColumns = '';
    for (var i = 0; i < data.getNumberOfColumns(); i++) {
      csvColumns += data.getColumnLabel(i);
      if (i < (data.getNumberOfColumns() - 1)) {
        csvColumns += ',';
      }
    }
    csvColumns += '\n';

    // build data rows
    csvContent = csvColumns + google.visualization.dataTableToCsv(data);

    // download file
    browserIsIE = false || !!document.documentMode;
    fileName = 'data.csv';
    if (browserIsIE) {
      window.navigator.msSaveBlob(new Blob([csvContent], {type: 'data:text/csv'}), fileName);
    } else {
      downloadLink = document.createElement('a');
      downloadLink.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent);
      downloadLink.download = fileName;
      raiseEvent(downloadLink, 'click');
      downloadLink = null;
    }
  });

  function raiseEvent(element, eventType) {
    var eventRaised;
    if (document.createEvent) {
      eventRaised = document.createEvent('MouseEvents');
      eventRaised.initEvent(eventType, true, false);
      element.dispatchEvent(eventRaised);
    } else if (document.createEventObject) {
      eventRaised = document.createEventObject();
      element.fireEvent('on' + eventType, eventRaised);
    }
  }
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<div>
  <button class="csv-button ui-button ui-widget ui-corner-all">
    <span class="ui-icon ui-icon-circle-triangle-s"></span><span>&nbsp;Download CSV</span>
  </button>
</div>
<div id="chart_div"></div>