Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

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

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

    // download file
    browserIsIE = false || !!document.documentMode;
    fileName = 'data.csv';
    if (browserIsIE) {
      window.navigator.msSaveBlob(newBlob([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;
    }
  });

  functionraiseEvent(element, eventType) {
    var eventRaised;
    if (document.createEvent) {
      eventRaised = document.createEvent('MouseEvents');
      eventRaised.initEvent(eventType, true, false);
      element.dispatchEvent(eventRaised);
    } elseif (document.createEventObject) {
      eventRaised = document.createEventObject();
      element.fireEvent('on' + eventType, eventRaised);
    }
  }
});
<scriptsrc="https://code.jquery.com/jquery-1.12.4.js"></script><scriptsrc="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script><scriptsrc="https://www.gstatic.com/charts/loader.js"></script><linkhref="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"><div><buttonclass="csv-button ui-button ui-widget ui-corner-all"><spanclass="ui-icon ui-icon-circle-triangle-s"></span><span>&nbsp;Download CSV</span></button></div><divid="chart_div"></div>

Solution 2:

Google will provide you u need to pass your API key inside it

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

functiondraw() {
  drawVisualization();
  drawToolbar();
}

functiondrawVisualization() {
  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);
}

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

functiondrawToolbar() {
  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><divid="visualization_div"style="width: 270px; height: 200px;"></div><divid="toolbar_div"></div></body></html>

fore more : https://developers.google.com/chart/interactive/docs/gallery/toolbar

Post a Comment for "How To Set Export Csv Option To Button In Google Visualization Pie Chart"