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

Solution 2:

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

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

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"