How to set the background-color of a D3.js svg?

后端 未结 2 407
北海茫月
北海茫月 2020-12-28 13:23

I\'m not very familiar in styling D3.js SVG\'s. I create a collapsible tree and will provide an option to download this tree as SVG/PDF/PNG. This works great but the backgro

相关标签:
2条回答
  • 2020-12-28 14:08

    You can use the SVG as another HTML component, you can set its CSS properties:

    For instance, on creating the svg, you set a class:

    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.right + margin.left)
        .attr("height", height + margin.top + margin.bottom)
        .attr("class", "graph-svg-component");
    

    In your CSS you can define the property:

    .graph-svg-component {
        background-color: AliceBlue;
    }
    
    0 讨论(0)
  • 2020-12-28 14:13

    Just add a <rect> as the first painting order item that displays the colour you want.

    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.right + margin.left)
        .attr("height", height + margin.top + margin.bottom);
    
    svg.append("rect")
        .attr("width", "100%")
        .attr("height", "100%")
        .attr("fill", "pink");
    
    svg.append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
    0 讨论(0)
提交回复
热议问题