Is it possible to save a google chart as an image?

2019-03-11 04:43发布

I am making an application in which I would need to save the google chart as an image . All I am using is tomcat, servlets and javascript. Is there a way to save the following generated chart as an image? (refer to code at the end of post). The idea is that user would see this chart and then would have the option of uploading it to his facebook profile. I am not sure if this will be uploadable to facebook in its native format or will be needed to be saved as a jpg.

<html>    
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();

    data.addColumn('string', 'Year');
    data.addColumn('number', 'Sales');
    data.addRows(4);
    data.setValue(0, 0, ''+2004);
    data.setValue(0, 1, 1000);
    data.setValue(1, 0, '2005');
    data.setValue(1, 1, 1170);
    data.setValue(2, 0, '2006');
    data.setValue(2, 1, 860);
    data.setValue(3, 0, '2007');
    data.setValue(3, 1, 1030.5);

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance',hAxis: {title: "X", titleTextStyle: {color: "green"}}});
  }

</script>
  </head>

  <body>
    <div id="chart_div"></div>
  </body>
</html>

6条回答
混吃等死
2楼-- · 2019-03-11 04:55

Your simplest option is to regenerate a static image version of the chart using the Google Chart API

查看更多
在下西门庆
3楼-- · 2019-03-11 04:59

Use the grChartImg library. It's a cross browser solution from George Risvas. Supports all the browsers including older versions of IE and provides you with many auto procedures like convert a chart to image,download,show a chart to a dialog,upload it to a server etc.

For more info look at www.chartstoimage.

I hope help you.

查看更多
何必那么认真
4楼-- · 2019-03-11 05:01

It looks like this feature was recently1 added as:

chart.getImageURI()

See the documentation.

1It appears to have been added in the Jan 29, 2014 release.

查看更多
虎瘦雄心在
5楼-- · 2019-03-11 05:02

This is what i do

http://danml.com/#/download.html

 download(chart.getImageURI(), 'fileName.png', "image/png");

if 3kb script is too much use http://www.closure-compiler.appspot.com/home

查看更多
劫难
6楼-- · 2019-03-11 05:05

First, download this library :

http://danml.com/download.html

and after you have just to add an events listener inside your chart

function: 
 google.charts.setOnLoadCallback(drawVisualization);

  function drawVisualization() {

var chart = new google.visualization.ComboChart(document.getElementById('chart_tickets'));
    google.visualization.events.addListener(chart, 'ready', function () {

    $("#GraphDownloadTickets").click(function() {
         download(chart.getImageURI(), 'fileName.png', "image/png")

    });
}
查看更多
Juvenile、少年°
7楼-- · 2019-03-11 05:12

Example solution from Riccardo Govoni as seen on the issues page of google charts. The idea is to convert the SVG to Canvas element.

Links:

Tutorial : http://www.battlehorse.net/page/topics/charts/save_google_charts_as_image.html

Example page: http://www.battlehorse.net/attach/topics/charts/google_charts_to_image.html

查看更多
登录 后发表回答