使用d3 js动态创建单选按钮

时间:2014-10-22 03:24:14

标签: javascript d3.js radio-button

我知道如何创建单选按钮以在不使用d3 js的情况下动态显示。但是如何使用d3创建单选按钮的标签文本?创建后,如何将它们添加到单选按钮?

1 个答案:

答案 0 :(得分:2)

var shapeData = ["Triangle", "Circle", "Square", "Rectangle"], 
    j = 3;  // Choose the rectangle as default

// Create the shape selectors
var form = d3.select("body").append("form");

labels = form.selectAll("label")
    .data(shapeData)
    .enter()
    .append("label")
    .text(function(d) {return d;})
    .insert("input")
    .attr({
        type: "radio",
        class: "shape",
        name: "mode",
        value: function(d, i) {return i;}
    })
    .property("checked", function(d, i) {return i===j;});

SEE DEMO HERE

相关问题