7

いくつかの svg 要素のスタイルを変更しようとしています。私がこれを行うとき:

var circleSelected = d3.select("#circleid_2");
        circleSelected.style("fill", "purple");
        circleSelected.style("stroke-width", 5);
        circleSelected.style("stroke", "red");

サークルはそのスタイルを変えています。

しかし、私がこれを行うとき:

 var allCircles = d3.selectAll(".circle");
        allCircles.forEach(function (circle) {
            circle.style("fill", "green"); //function(d) { return getNodeColor(d); }
        });

エラーで動作しません: オブジェクト [object SVGCircleElement] has no method 'style'

これが私の「サークル」宣言です(注:クラスとIDの両方があります):

node.append("circle")
            .attr("id", function (d) { return "circleid_" + d.id; })
            .attr("class", "circle")
            .attr("cx", function (d) { return 0; })
            .attr("cy", function (d) { return 0; })
            .attr("r", function (d) { return getNodeSize(d); })
            .style("fill", function (d) { return getNodeColor(d); })
            .style("stroke", function (d) { return getNodeStrokeColor(d); })
            .style("stroke-width", function (d) { return getNodeStrokeWidth(d); });

ここで何が間違っていますか?助けてくれてありがとう!

4

1 に答える 1

12

試す:

d3.selectAll("circle").style("fill", "green");

または:

allCircles.style("fill", "PaleGoldenRod");

説明:d3.selectAllこの API で説明されている関数を使用して実行できる選択を返します: https://github.com/d3/d3/blob/master/API.md#selections-d3-selection

ただし、 を行うとすぐに、forEach毎回返される内部変数 ascircleが実際の DOM 要素になります。選択ではなくなるため、style関数は関連付けられません。

于 2013-01-17T08:12:18.280 に答える