Export Canvas to SVG file

Viewed 2831

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

This is the HTML file.

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

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

...
// 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
Related