[英]dynamically naming the axis in scatterplot d3.js
如何在散點圖中為x和y軸動態分配變量名稱? 這是我從此站點獲取的示例之一
該軸已命名為Date和value,分別來自jsfiddle的行號109和121。
.text("Date");
.text("Value");
如果使用不同的數據集,我們如何根據用戶選擇動態更改這些值。 我的意思是說,我正在開發一個應用程序,其中用戶選擇數據並由散點圖自行更新。
您可以做的是在軸的text元素上提供一個id,如下所示:
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.attr("id","my-axis-name")
.text("Value");
因此,例如,一旦使用按鈕更改了數據集,就可以在控制序列中添加:
function myCoolUpdateFunction() {
// Do stuff, e.g.
svg.selectAll('circle').style("fill","red");
//change the text of the axis
svg.select('#my-axis-name').text("New Description")
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.