gpt4 book ai didi

javascript - 可排序条形图不工作

转载 作者:行者123 更新时间:2023-11-30 20:41:28 25 4
gpt4 key购买 nike

刚开始学习 D3,所以这应该是一个基本修复。无论如何,我正在尝试通过单击制作一个简单的条形图排序。经过数小时的搜索似乎无法弄清楚。

我的猜测是它与过渡或尺度有关,但我当然是新手。

这是代码。

<script src="https://d3js.org/d3.v4.min.js"></script>

<body>
<script>
var w = 600;
var h = 250;

var dataset = [{
"namePlayer": "Player1",
"mean": 37.4375
},
{
"namePlayer": "Player2",
"mean": 13.6937
},
{
"namePlayer": "Player3",
"mean": 26.7
},
{
"namePlayer": "Player5",
"mean": 20.0804
},
{
"namePlayer": "Player6",
"mean": 27.9235
}
];

var xScale = d3.scaleBand()
.domain(dataset.map(function(d, i) {
return d.namePlayer;
}))
.range([w, 0])
.padding(0.05);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) {
return d.mean;
})])
.range([h, 0]);
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);

var xAxis = d3.axisBottom()
.scale(xScale);



svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d) {
return xScale(d.namePlayer);
})
.attr("width", xScale.bandwidth())
.attr("y", function(d) {
return yScale(d.mean);
})
.attr("height", function(d) {
return h - yScale(d.mean);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + Math.round(d.mean * 10) + ")";
})
.on("click", function() {
sortBars();
})

var sortBars = function() {
svg.selectAll("rect")
.sort(function(a, b) {
return d3.ascending(a.mean, b.mean);
})
.transition()
.delay(function(d) {
return d.mean * 50;
})
.duration(1000)
.attr("x", function(d) {
return xScale(d.mean);
});
};
</script>
</body>

感谢您的帮助!

最佳答案

现在您只是对选择进行排序(并将错误的属性传递给 xScale 比例尺)。

除此之外,您应该对数据进行排序...

dataset.sort(function(a, b) {
return d3.ascending(a.mean, b.mean);
});

... 并将其传递给 xScale:

xScale.domain(dataset.map(function(d, i) {
return d.namePlayer;
}));

然后,您可以使用您的过渡。

这是更新后的代码:

<script src="https://d3js.org/d3.v4.min.js"></script>

<body>
<script>
var w = 600;
var h = 250;

var dataset = [{
"namePlayer": "Player1",
"mean": 37.4375
},
{
"namePlayer": "Player2",
"mean": 13.6937
},
{
"namePlayer": "Player3",
"mean": 26.7
},
{
"namePlayer": "Player5",
"mean": 20.0804
},
{
"namePlayer": "Player6",
"mean": 27.9235
}
];

var xScale = d3.scaleBand()
.domain(dataset.map(function(d, i) {
return d.namePlayer;
}))
.range([w, 0])
.padding(0.05);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) {
return d.mean;
})])
.range([h, 0]);
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);

var xAxis = d3.axisBottom()
.scale(xScale);

svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d) {
return xScale(d.namePlayer);
})
.attr("width", xScale.bandwidth())
.attr("y", function(d) {
return yScale(d.mean);
})
.attr("height", function(d) {
return h - yScale(d.mean);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + Math.round(d.mean * 10) + ")";
})
.on("click", function() {
sortBars();
})

var sortBars = function() {
dataset.sort(function(a, b) {
return d3.ascending(a.mean, b.mean);
});
xScale.domain(dataset.map(function(d, i) {
return d.namePlayer;
}));
svg.selectAll("rect")
.transition()
.delay(function(d) {
return d.mean * 50;
})
.duration(1000)
.attr("x", function(d) {
return xScale(d.namePlayer);
});
};

</script>
</body>

关于javascript - 可排序条形图不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205206/

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