1 回答

TA貢獻1809條經驗 獲得超8個贊
第一個代碼塊不起作用,因為x_axis它不包含您認為的內容。
var x_axis = svg.append("g") // returns a selection of a g element
.attr("class", "axis") // returns the same selection of a g
...
.call(d3.axisBottom(ordinalScale)) // returns the same selection of a g
.selectAll("text") // returns a new selection of text elements
...
.style("fill", "#102040"); // returns the same selection of text elements
x_axis由鏈返回的最后一個值定義。因此, x_axis上面是文本元素的選擇,文本元素不能(在本例中不)包含任何子路徑或行元素,因此x_axis.selectAll('line, path')將返回空選擇。因此,為空選擇設置任何屬性都不會改變任何內容。
第二個代碼塊之所以有效,是因為x_axis它仍然是 ag 的選擇 - 返回鏈接到的相同selection.call()內容,例如or ,以及其他方法。而 和,以及其他方法,返回新的選擇。selection.call().attr().style()selectAll()select()
添加回答
舉報