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

Dynamically create radio buttons using d3 js

本文关键字:单选按钮 创建 动态 d3-js 使用      更新时间:2023-09-26

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

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;});

点击此处查看演示