我对d3很陌生。我有这个并且它有效:
<script type="text/javascript">
var w = 620;
var h = 30;
var father = [ true, true, false, false, false ];
//Create SVG element
var svg = d3.select("#parentsmed")
.append("svg")
.attr("height", h)
.attr("width", w);
var fatherrects = svg.selectAll("rect")
.data(father)
.enter()
.append("rect");
fatherrects.attr("x", function(d, i) {
return (i * 31) + 93;
})
.attr("width", 30)
.attr("height",30)
.attr("fill", function(d, i) {
if(father[i] == true) {
return "#89CFF0";
} else {
return "#efefef";
}
});
</script>
我想做的是拥有另一个数组 var 或嵌套数组,并绘制“母亲”值......像这样:
<script type="text/javascript">
var w = 620;
var h = 30;
var father = [ true, true, false, false, false ];
var mother = [ false, true, false, false, true ];
//Create SVG element
var svg = d3.select("#parentsmed")
.append("svg")
.attr("height", h)
.attr("width", w);
var fatherrects = svg.selectAll("rect")
.data(father)
.enter()
.append("rect");
fatherrects.attr("x", function(d, i) {
return (i * 31) + 93;
})
.attr("width", 30)
.attr("height",30)
.attr("fill", function(d, i) {
if(father[i] == true) {
return "#89CFF0";
} else {
return "#efefef";
}
});
var motherrects = svg.selectAll("rect")
.data(mother)
.enter()
.append("rect");
motherrects.attr("x", function(d, i) {
return (i * 31) + 93;
})
.attr("y", 31)
.attr("width", 30)
.attr("height",30)
.attr("fill", function(d, i) {
if(mother[i] == true) {
return "#89CFF0";
} else {
return "#efefef";
}
});
</script>
这会绘制fatherrects
,但不会绘制motherrects
。如何使用两个数组变量(如图所示)或单个var data = [father,mother];
嵌套数组来绘制两者(顶行上的父矩形,底行上的母矩形)?