简体   繁体   English

将 Canvas 导出到 SVG 文件

[英]Export Canvas to SVG file

I'm trying to transform a canvas, which contains a chart created with Chart.js, to SVG in order to be able to download a SVG file from the page which contains the chart. I'm trying to transform a canvas, which contains a chart created with Chart.js, to SVG in order to be able to download a SVG file from the page which contains the chart. I have been googling for 5 hours, I tried multiple scripts and the best one i got is this one but the problem is that the transformed SVG contains only a quarter of the chart, I think is has something to do with the option from Chart.js for responsive ( which should be false in order to transform to SVG).我已经用谷歌搜索了 5 个小时,我尝试了多个脚本,我得到的最好的是这个,但问题是转换后的 SVG 只包含图表的四分之一,我认为这与 Chart.js 的选项有关响应式(为了转换为 SVG 应该为 false)。

This is the HTML file.这是 HTML 文件。

<html>
<body>
<div class="chartContainer">
    <canvas id="radarCanvas" style="height: 600px;width: 600px;"></canvas>
</div>
<a id="down">
<button type="button" onclick="download()">Download</button>
</a>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
<script type="text/javascript" src="canvas2svg.js"></script>
<script type="text/javascript">
   var graphData = {
        type: 'pie',
        data: {
            labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
            datasets: [{
                label: "Population (millions)",
                backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
                data: [2478,5267,734,784,433]
            }]
        },
        options: {
            title: {
                display: true,
                text: 'Predicted world population (millions) in 2050'
            },
            animation: false,
            responsive:false,
            maintainAspectRatio: false
        }
    }


    function tweakLib(){
        C2S.prototype.getContext = function (contextId) {
            if (contextId=="2d" || contextId=="2D") {
                return this;
            }
            return null;
        }

        C2S.prototype.style = function () {
            return this.__canvas.style
        }

        C2S.prototype.getAttribute = function (name) {
            return this[name];
        }

        C2S.prototype.addEventListener =  function(type, listener, eventListenerOptions) {
            console.log("canvas2svg.addEventListener() not implemented.")
        }
    }


    var context = document.getElementById("radarCanvas").getContext("2d");

    var radarChart = new Chart(context, graphData); // Works fine

    tweakLib();
    // deactivate responsiveness and animation

    // canvas2svg 'mock' context
    var svgContext = C2S(600,600);

    // new chart on 'mock' context fails:
    var mySvg = new Chart(svgContext, graphData);
    console.log(svgContext.getSvg());
    function download() {
        var dl = document.createElement("a");
        document.body.appendChild(dl); // This line makes it work in Firefox.
        var svg= svgContext.getSvg();
        if (window.ActiveXObject) {
            svgString = svg.xml;
        } else {
            var oSerializer = new XMLSerializer();
            svgString = oSerializer.serializeToString(svg);
        }
        dl.download = "Chart.svg";
        //   return "data:image/svg+xml," + encodeURIComponent(svgAsXML);
        dl.href='data:image/svg+xml;utf8,' + encodeURIComponent(svgString);
        dl.click();
    }

</script>

</html>

Stupid me..... All was needed to add was...愚蠢的我......所有需要添加的是......

mySvg.width=600;
mySvg.height=600; 
//or any size

These lines need to be after these the chart was created on the C2S object这些线需要在这些图表是在 C2S object 上创建之后

...
// deactivate responsiveness and animation

// canvas2svg 'mock' context
var svgContext = C2S(600,600);

// new chart on 'mock' context fails:

var mySvg = new Chart(svgContext, graphData);
//  insert here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM