将Google图表保存为图片 [英] Save google chart as image

查看:111
本文介绍了将Google图表保存为图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的代码不能将我的图表保存为图像?我在某个地方有错吗?
虽然我跟踪了源代码(在网上找到),但我仍然无法解决我的问题。

Why can't my code save my chart as an image? Do I have a mistake somewhere? Although I followed the source (found online), I still can't solve my problem.

我可以显示图表,但只有问题是无法保存作为图片。

I can show the chart but only the problem is cannot save as an image.

<script type="text/javascript" src="js/jsapi.js"></script>
    <script type="text/javascript">
    function saveAsImg(chartContainer, pngfilename) {
    var chartArea = chartContainer.getElementsByTagName('svg')[0].parentNode;
    var svg = chartArea.innerHTML;
    var doc = chartContainer.ownerDocument;
    var canvas = doc.createElement('canvas');
    canvas.setAttribute('width', chartArea.offsetWidth);
    canvas.setAttribute('height', chartArea.offsetHeight);
    canvas.setAttribute(
    'style',
    'position: absolute; ' +
    'top: ' + (-chartArea.offsetHeight * 2) + 'px;' +
    'left: ' + (-chartArea.offsetWidth * 2) + 'px;');
    doc.body.appendChild(canvas);
     canvg(canvas, svg);
    var data = canvas.toDataURL("image/png");
    canvas.parentNode.removeChild(canvas);
    data = data.substr(data.indexOf(',') + 1).toString();

    var dataInput = document.createElement("input") ;
    dataInput.setAttribute("name", 'imgdata') ;
    dataInput.setAttribute("value", data);

    var nameInput = document.createElement("input") ;
    nameInput.setAttribute("name", 'name') ;
    nameInput.setAttribute("value", pngfilename + '.png');

    var myForm = document.createElement("form");
    myForm.method = 'post';
    myForm.action = 'saveme.php';
    myForm.appendChild(dataInput);
    myForm.appendChild(nameInput);

    document.body.appendChild(myForm) ;
    myForm.submit() ;
    document.body.removeChild(myForm) ;

}


</script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
    </script>

    <script type="text/javascript">


      function drawVisualizationDaily() {
        // Create and populate the data table.
        Price1=document.getElementById('Price1').value;
        Price2=document.getElementById('Price2').value;
        Price3=document.getElementById('Price3').value;
        Price4=document.getElementById('Price4').value;
        Price5=document.getElementById('Price5').value;
        Price6=document.getElementById('Price6').value;
        Price7=document.getElementById('Price7').value;
        var data = google.visualization.arrayToDataTable([
          ['Daily', 'Sales'],
          ['Mon', parseInt(Price1) ],
          ['Tue', parseInt(Price2) ],
          ['Wed', parseInt(Price3) ],
          ['Thu', parseInt(Price4) ],
          ['Fri', parseInt(Price5) ],
          ['Sat', parseInt(Price6) ],
          ['Sun', parseInt(Price7) ]
        ]);

        // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('visualization')).
            draw(data,
                 {title:"Daily Sales",
                  width:500, height:400,
                  hAxis: {title: "Daily"}}
            );
       }  


      google.setOnLoadCallback(drawVisualizationDaily);
    </script>

    <div id="visualization" style="width: 600px; height: 400px;"></div>
    <script>
    <a href='#' onClick="saveAsImg(document.getElementById('visualization'), 'test');">Test</a>
</script>


推荐答案

该方法不再需要获取图像图表。您可以调用图表的 getImageURI 方法来获取生成图像的URL字符串:

That method is no longer necessary to get an image of the chart. You can call the chart's getImageURI method instead to get a URL string for generating the image:

var chart = new google.visualization.ColumnChart(document.getElementById('visualization'));
google.visualization.events.addListener(chart, 'ready', function () {
    var imgUri = chart.getImageURI();
    // do something with the image URI, like:
    window.open(imgUri);
});
chart.draw(data, {
    title:"Daily Sales",
    width:500,
    height:400,
    hAxis: {title: "Daily"}
});

如果您打开图片URI,您可以右键点击图片进行保存。

If you open the image URI, you can right-click the image to save it.

这篇关于将Google图表保存为图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆