D3の結合に問題があります。バブルグラフ上にいくつかの円を作成しました。ユーザーが[地図として表示]ボタンをクリックしたときに、円を地図に変換してから、それぞれにラベルを追加します。
現在、クリックハンドラーは円をOKに移動し、ラベルを追加するのではなく、JavaScriptエラーを表示しますUncaught TypeError: Object [object SVGCircleElement]... has no method 'append'
。これは結合構文が正しくないためですが、どうすれば修正できますか?
新しいD3要素を追加するために私が見つけることができるすべての例は、新しいデータをバインドしている場合のためのものです-データがすでにバインドされている既存の要素がすでにある場合ではありません。
これは、サークルを作成するための私のコードです。
var circle = g.selectAll('.city')
.data(data).enter()
.append('circle')
.attr('class', 'city')
.attr('r', rfunc).attr("cy", 0)
.attr("cx", function(d, i) {
return rdist(d['dist']);
}).attr("transform", function(d, i) {
return "rotate(" + d.radial_pos + " 0 0)";
});
そしてこれは私のクリックハンドラーです:
d3.select("#layout_geo").on("click", function() {
// Move the circles - this works OK.
var circles = d3.selectAll(".city")
.transition()
.duration(1300)
.attr('cx', function(d) {
return merc([d['lon'], d['lat']])[0] - 350;
}).attr('cy', function(d) {
return merc([d['lon'], d['lat']])[1] - 280;
});
// How to add text to each circle?
circles.append('text')
.attr('class', 'background')
.text(function(d) {
console.log(d.name + ', ' + d.city);
return d.name + ', ' + d.city;
})
.attr('cx', function() {
return ???;
}).attr('cy', function() {
return ???;
});
});