gpt4 book ai didi

javascript - 基本 D3.js : create or update element?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:33 24 4
gpt4 key购买 nike

基本 D3.js 问题,掌握语法!

我正在使用 D3,我想创建一个轴(如果它不存在),或者更新它(如果它已经存在)并带有过渡。

我当前的代码如下,但这段代码每次都会重新创建轴 - 它不会转换。如何将其更改为过渡?

var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(4).tickSize(6, 3, 0);

updateGraphAndAxes(initialdata);

$('#button').click(function() {
updateGraphAndAxes(newdata);
});

function updateGraphAndAxes(newdata) {
// update x.domain here using newdata, then...
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
}

最佳答案

正在关注 this example .方法是这样的:

var xAxisGroup = null;

function updateGraphAndAxes(newdata) {

var t = null;

t = svg.transition().duration(1000); // Set up transition

// update x.domain using newdata...

if (!xAxisGroup) {
xAxisGroup = svg.append("g")
.attr("class", "xTick")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
} else {
t.select('.xTick').call(xAxis); // Call xAxis on transition
}
}

关于javascript - 基本 D3.js : create or update element?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12185463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com