douyao3895 2012-12-11 16:19
浏览 153
已采纳

将Google图表另存为图片

So after hours of websearching, googling and overflowing i can't find the solution to my problem.

I got a linechart from Google charts. I want to convert it to PNG, save it on the server en insert it into a MySQL database.

Sounds simple, but i cant get it to work. The script from this website isnt working anymore (atleast not here) http://www.battlehorse.net/page/topics/charts/save_google_charts_as_image.html -> Not working.

Second option is the old option:

$imageData =     file_get_contents('http://chart.apis.google.com/chart... etc');

I cant use that because its not supported anymore and cant get some decent quality out of it.

Is there anybody here that can give a good tutorial or help for my problem?

EDIT:

I used the code from Battlehorse combined with the code from EriC.

So now i got this working to show the chart as an image in a DIV i want to save this image on the server and update the mysql to use it in the future to use it in PDF files.

  • 写回答

4条回答 默认 最新

  • dtvam48220 2012-12-11 16:40
    关注

    When you visit the site, paste this in the console (overwriting the malfunctioning function).

      function getImgData(chartContainer) {
        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 imgData = canvas.toDataURL("image/png");
        canvas.parentNode.removeChild(canvas);
        return imgData;
      }
    

    In JS it was searching for an iframe bla bla to get the svg.


    To automatically save the image, you can just let the method being invoked programmatically.

    document.body.addEventListener("load", function() {
    
            saveAsImg( document.getElementById("pie_div")); // or your ID
    
        }, false );
    


    For saving images serverside, this post could be helpful save a PNG image server-side

    Update
    Posting images to PHP (index.js)

    function saveToPHP( imgdata ) {
    
        var script = document.createElement("SCRIPT");
    
        script.setAttribute( 'type', 'text/javascript' );
        script.setAttribute( 'src', 'save.php?data=' + imgdata );
    
        document.head.appendChild( script );
    }
    
    function save() {
    
        var canvas = document.getElementById("canvas"), // Get your canvas
            imgdata = canvas.toDataURL();
    
        saveToPHP( imgdata );
    }
    
    function drawOnCanvas() {
    
        var canvas = document.getElementById("canvas"), // Get your canvas
            ctx = canvas.getContext("2d");
    
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#FFFF00";
        ctx.beginPath();
        ctx.arc(100,99,50,0,Math.PI*2,true);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
    
    drawOnCanvas(); // Test
    save();
    

    save.php

    <?php
        // Get the request
        $data = $_GET['data'];
    
        // Save to your DB.
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿