gpt4 book ai didi

d3.js - D3 转换不起作用

转载 作者:行者123 更新时间:2023-12-01 10:34:57 27 4
gpt4 key购买 nike

我在让我的转换在数据集之间工作时遇到问题。我希望数字、矩形和名称在单击按钮时能够在彼此之间平滑过渡,但目前它们只能跳跃。

我是这样用的

players.select('.saves')
.transition().duration(200)
.attr('y', function(d, i) {
return i * (h / data.length);
})
.attr('width', function(d) {
return Scale(d['saves']) ;});

我的选择有问题吗?

https://jsfiddle.net/8voee4fm/1/

<button class="opts champ" value="championsleague">Champions League</button>
<button class="opts prem" value="premierleague">Premier League</button>

<div id="chart"></div>

var w = 300, h = 300;
var barHeight = 20;

var data =

championsleague = [{
"name": "Hart",
"saves": "9",
"total": "15",
}, {
"name": "Subasic",
"saves": "6",
"total": "10"
}, {
"name": "Neuer",
"saves": "5",
"total": "9"
},{
"name": "Olsen",
"saves": "9",
"total": "18"
}, ];

premierleague = [{
"name": "Hart",
"saves": "12",
"total": "27"
}, {
"name": "Forster",
"saves": "13",
"total": "22"
}, {
"name": "De Gea",
"saves": "17",
"total": "29"
}, {
"name": "Green",
"saves": "21",
"total": "39"
},];

data.forEach(function(d){
d.saves = +d.saves;
d.total = +d.total;
});

var canvas = d3.select('#chart')
.append('svg')
.attr('width', w)
.attr('height', h)
.append('g')
.attr("transform", "translate(10,30)");

var Scale = d3.scale.linear().domain([0, 39]).range([0, w]);

function updateLegend(data) {

var players = canvas
.selectAll(".player")
.data(data, function(d){
// always create a unique key for your data-binding
return d.name + d.saves + d.total;
});

var playersEnter = players
.enter()
.append("g")
.attr("class", "player");

playersEnter.append('rect')
.attr('class','total')
.style("fill", "#abcbc0")
.attr('x', 0)
.attr('height', barHeight);
playersEnter.append('text')
.attr('class','totaln')
.style("fill", "black")
.style("text-anchor", "right");
playersEnter.append('rect')
.attr('class','saves')
.style("fill", "#008c6c")
.attr('x', 0)
.attr('height', barHeight);
playersEnter.append('text')
.attr('class','savesn')
.style("text-anchor", "right")
.style("fill", "white");
playersEnter.append('text')
.attr('class','name')
.attr('x', 0);

players.select('.total').transition()
.duration(200)
.attr('y', function(d, i) {
return i * (h / data.length)
})
.attr('width', function(d) {
return Scale(d['total'])
})
.attr('height', barHeight);

players.select('.totaln').transition()
.duration(200).text(function(d) {
return d.total;
})
.attr('x', function(d) {
return Scale(d['total']) -20})
.attr('y', function(d,i) {
return i * (h / data.length ) +15
});

players.select('.saves')
.transition().duration(200)
.attr('y', function(d, i) {
return i * (h / data.length);
})
.attr('width', function(d) {
return Scale(d['saves']) ;});

players.select('.savesn')
.transition()
.duration(200).text(function(d) {
return d.saves;
})
.attr('x', function(d) {
return Scale(d['saves']) -18})
.attr('y', function(d,i) {
return i * (h / data.length ) +15
});

players.select('.name')
.transition().duration(200)
.text(function(d) {
return d.name;
})
.attr('y', function(d, i) {
return i * (h / data.length) -10
});

// remove old elements
players.exit().remove();

};

updateLegend(data);

d3.selectAll('.opts')
.on('click', function() {
var data = eval(d3.select(this).property('value'));
console.log(data)
updateLegend(data);
})

最佳答案

您只需修改一行代码:

代替

 var players = canvas
.selectAll(".player")
.data(data, function(d){
// always create a unique key for your data-binding
return d.name + d.saves + d.total;
});

你写:

 var players = canvas
.selectAll(".player")
.data(data);

现在可以了。问题如下:您在输入选择中创建的 DOM 元素不是有效的 SVG 元素,因为至少缺少一个属性(例如,矩形缺少宽度)。结果,转换创建了对象(来自未指定的 SVG 对象)。

因此,由于上述原因,现在除了初始显示之外,所有内容都有转换。

这是工作的 JSFiddle: https://jsfiddle.net/ee2todev/xjf0k2oy/

关于d3.js - D3 转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35103322/

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