我對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
。如何使用兩個數組變量(如圖所示)或單個嵌套數組來繪製它們(頂部行的父級矩形,最底部的母級矩形)?
我編輯了你的標題。請參見「[應的問題包括‘標籤’,在他們的頭銜?(http://meta.stackexchange.com/questions/19190/)」,這裏的共識是「不,他們不應該」。 –