将图形从svg(d3.js)导出到jpg时的黑色区域

Mar*_*ios 1 svg d3.js todataurl

我试图使用javascript将图形从SVG(通过d3.js获得)导出到JPG图像.

事实是最终图像没有正确显示图像,但它绘制了一个黑色区域,包围图形的线条.这是他们的两张照片.在页面的顶部表示SVG,而描绘最终的JPG图像.

我写的代码如下:

    root_node = d3.select("svg")
      .attr("version", 1.1)
      .attr("xmlns", "http://www.w3.org/2000/svg")
      .node();

    s_xml = (new XMLSerializer).serializeToString(root_node);

    var imgsrc = 'data:image/svg+xml,'+ s_xml;
    var img = '<img src="'+imgsrc+'">';

    var canvas = document.createElement("canvas");

    canvas.width = 1600;
    canvas.height = 600;

    context = canvas.getContext("2d");

    var image = new Image;
    image.src = imgsrc;
    image.onload = function() {
        context.drawImage(image, 0, 0);
        context.fillStyle = 'white';
        context.globalCompositeOperation = "destination-over";
        context.fillRect(0, 0, canvas.width, canvas.height);

        var canvasdata = canvas.toDataURL("image/jpeg");
        var jpgimg = '<img src="'+canvasdata+'">';
        d3.select("body").append("svgdataurl").html(jpgimg);

    });
Run Code Online (Sandbox Code Playgroud)

谁知道颜色转换错误的原因?提前致谢!

Dee*_*kar 5

您必须直接为D3代码中的折线图提供CSS.

lineChart.append("path")
.attr("class", "lineChart")
.attr("stroke-width", 1)
.attr("fill","none")
.attr("d", valueline(lineChartData));
Run Code Online (Sandbox Code Playgroud)