Attaching Data to Pre-Rendered SVG Elements

by Nivaldo

HTML

<div class="graph">
    <svg>
        <rect class="bar" id="01" height="187" width="60" y="262" x="355"></rect>
        <rect class="bar" id="02" height="187" width="60" y="262" x="455"></rect>
    </svg>
</div>

JavaScript

var data = [150, 450];

//d3.csv("file.csv", function (data) {
    var svg1 = d3.select(".graph").selectAll("svg");
    svg1.selectAll("rect")
        .data(data)
        .style("fill", function (d, i) {
            return "rgb(0, 0, " + d + ")";
        });
//});