0
是否有可能在一個div中包裝五個svg,並且每個五個新創建的div都用javascript(而不是jquery)包裝在另一個div中?用D3.js包裝div中的每五個svg元素
這裏是我的代碼
d3.csv("example.svg", function(data) {
data.forEach(function(d){
var svgContainer = d3.select(".parentclass").append("svg")
.attr("width",55)
.attr("height",35);
var rectangle = svgContainer.append("rect")
.attr("x",0)
.attr("y",0)
.attr("width",55)
.attr("height",35)
.attr("fill", "rgb(" + d.r + "," + d.g + "," + d.b + ")");
})
});
我期望的輸出是
<div class="column1">
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
</div>
<div class="column2">
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
<div class="line">
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg>
<svg width="55" height="35">....</svg></div>
</div>
可能的解決思路1:
伊夫添加以下代碼變種svgContainer 但所有的SVG是前在第一個孩子補充。
if ((i % 5 == 0) || (i == 0)) {
var body = d3.select('.parentclass')
.append('div')
.attr('class','childclass');
}
i = i + 1;
可能的解決思路2:
var svgContainer = d3.select(".milk").filter(function(i){ return ((i % 5 === 0) || (i === 1)); }).append("div")
.append("svg")
.attr("width",55)
.attr("height",35);
var test = function (i) {
i = i + 1;
return i;
}
如何使他的工作任何想法?
謝謝你的回答,即時試圖使這項工作。你的例子中的 有關如何依次顯示數字的任何想法? – evnartabt
@evnartabt我已經更新了我的答案,以顯示如何完成這項工作 – thedude
我在這方面掙扎了好幾天,而且我還沒有接近! 謝謝你的答案! – evnartabt