gpt4 book ai didi

d3.js - 根据下拉菜单返回数据?

转载 作者:行者123 更新时间:2023-12-02 05:01:58 25 4
gpt4 key购买 nike

我正在构建一个基本的散点图,我想根据下拉选择突出显示图中的特定点。我的代码如下所示:

fill_arr = fill.range();
labels = ["A", "B", "C", "D"];
options = [0, 1, 2, 3];

// Build the dropdown menu
d3.select("#drop")
.append("select")
.selectAll("option")
.data(options)
.enter()
.append("option")
// Provide available text for the dropdown options
.text(function(d) {return labels[d];})

d3.select('select')
.on("change", function() {
// HOW CAN I GET THE OPTION THAT THE USER HAS SELECTED FROM THE DROPDOWN?
key = 0 // <- I can do this manually, but I want to get the label the user has selected
d3.selectAll('circle')
.transition()
.duration(300)
.ease("quad")
.attr( 'r', 5)
.attr('cx', function(d) {return d.x;})
.attr('cy', function(d) {return d.y;})
// if a data point is selected highlight other
// data points of the same color
.style('fill', function(d, i) {
if (d.label == key)
{return fill_arr[key]}
else {return "#ccc"}
;})
});

我的问题是我不知道如何确定用户从下拉列表中选择的内容。如何确定用户选择了哪个选项,["A", "B", "C", "D"]

最佳答案

使用this.selectedIndex访问它:

d3.select('select')
.on("change", function() {

key = this.selectedIndex;

d3.selectAll('circle')
.transition()
.duration(300)
.ease("quad")
.attr( 'r', 5)
.attr('cx', function(d) {return d.x;})
.attr('cy', function(d) {return d.y;})
// if a data point is selected highlight other
// data points of the same color
.style('fill', function(d, i) {
if (d.label == key)
{return fill_arr[key]}
else {return "#ccc"}
;})
});

关于d3.js - 根据下拉菜单返回数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16611541/

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