Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

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

I am using Google visualization pie chart for showing transaction state. I want to export to csv, there is functionality ToolBar to export csv,html,iGoogle but I want to specific to csv only without select options.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can use static method --> dataTableToCsv

google.visualization.dataTableToCsv

this will create a csv string of the data in a data table.

it will not export the column headings, but those can be added manually...

see following working snippet...

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 += '
';

    // 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...