在我剛纔的問題後,我問我怎麼能生成產生矩形here在D3中添加具有唯一ID和鏈接的大矩形內的小矩形?一個特定ID
唯一ID現在,我一直在試圖吸引那些矩形內的小矩形。然而,id="rectup0"
只能有4個小矩形與唯一的xlink:href
屬性 其餘的可以有2個矩形裏面。
<body>
<div id="chart">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.js"></script>
<script type="text/javascript">
var width = 1000,
height = 250,
margin = 7,
nRect = 11,
rectWidth = (width - (nRect - 1) * margin)/nRect,
svgContainer = d3.select('#chart').append('svg')
.attr('width', width)
.attr('height', height),
svgWalkway = d3.select('#chart').append('svg')
.attr('width', '1000')
.attr('height', '150'),
svgContainer2 = d3.select('#chart').append('svg')
.attr('width', width)
.attr('height', height);
// Rectangles scaling
var firstRow = d3.range(nRect),
posxScale = d3.scaleLinear()
.domain(d3.extent(firstRow))
.range([0, width - rectWidth]);
var middleRow = d3.range(1),
posxScale3 = d3.scaleLinear()
.domain(d3.extent(middleRow))
.range([0, width - rectWidth]);
var secondRow = d3.range(nRect),
posxScale2 = d3.scaleLinear()
.domain(d3.extent(secondRow))
.range([0, width - rectWidth]);
//Drawing the actual objects HTML rects
svgContainer.selectAll('rect.up')
.data(firstRow)
.enter()
.append('rect')
.attr("id", function(d,i){ return "rectup" + i })
.attr('x', posxScale)
.attr('width', rectWidth)
.attr('height', height)
.attr('class','up');
svgWalkway.selectAll('rect.middle')
.data(middleRow)
.enter()
.append('rect')
.attr('x', posxScale3)
.attr('width','1000')
.attr('height','150')
.attr('class', 'middle');
svgContainer2.selectAll('rect.down')
.data(secondRow)
.enter()
.append('rect')
.attr('id', function (d,i) { return "rectdown" + i })
.attr('x', posxScale2)
.attr('width', rectWidth)
.attr('height', height)
.attr('class', 'down')
.attr('y', 0);
</script>
</div>
</body>
我試圖繪製矩形類似像以前的(使用.append()
),但到目前爲止,僅僅把他們旁邊svgContainer
。我一直在閱讀一些關於使用分組的帖子,嘗試過但沒有成功。在此先感謝
UPDATE
所以基本上,整個問題就可以恢復,添加一個rect
標籤右側後rect id="rectup0"
所以我可以有
<rect id="rectup0"></rect>
<rect id="redrect" x="0" width="20" height="40"></rect>
我知道JQuery
讓你增加class屬性到其他類,但不知道D3,這將像編程添加一個新的rect
標籤。這甚至有可能嗎?
更新2
我想這是可能的d3.insert
,具有以下:
d3.select('#rectup0').insert('rect')
.attr('id', 'redrect')
.attr('x', 0)
.attr('width', '20')
.attr('height', '40');
但是它得到它在一開始的中間和結束rect
標籤這樣
<rect id="rectup0">
<rect id="redrect" x="0" width="20" height="40"></rect>
</rect>
不用說這是因爲關閉而不起作用rect
t ag
您的問題現在有點混淆。你想要:1.在另一個元素之後追加一個元素? (這沒有視覺差異,順便說一句)2.添加類而不刪除以前的類? 3.在另一個矩形內繪製一個矩形? –
在另一個矩形內繪製一個矩形...其餘的是我試圖解決初始問題 – brohymn
我認爲整個問題都依賴於inisde矩形必須在確定的id之後繪製的事實#rectup0 – brohymn