gpt4 book ai didi

javascript - 在 d3 条形图上过滤后未重新绘制数据

转载 作者:行者123 更新时间:2023-11-30 00:06:08 24 4
gpt4 key购买 nike

我一直在尝试向条形图添加过滤器。当我点击图例时,该栏被删除,但当我尝试重新激活时,该栏没有被重新绘制。

这是一个plnk;

http://plnkr.co/edit/GZtErHGdq8GbM2ZawQSD?p=preview

我似乎无法解决这个问题 - 如果有人可以伸出援手?

谢谢


JS代码;

// load the data
d3.json("data.json", function(error, data) {
var group = [];

function updateData() {
group = [];

var organization = data.organizations.indexOf("Org1");
var dateS = $("#selectMonth").text()

for (var country = 0; country < data.countries.length; country++) {

var date = data.dates.indexOf(dateS);
group.push({
question: data.organizations[organization],
label: data.countries[country],
value: data.values[organization][country][date]

});

}
}

function draw(create) {

updateData();


x.domain(group.map(function(d) {
return d.label;
}));
y.domain([0, 100]);

// add axis




// Add bar chart
var bar = svg.selectAll("rect")
.data(group);
if (create) {
bar
.enter().append("rect")
.attr('x', function(d) {
return x(d.label);
})
.attr('y', function(d) {
return y(d.value);
})
.attr('width', x.rangeBand())
.attr('height', function(d) {
return height - y(d.value);
});


svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)");


svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 5)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value");
}



bar
.transition()
.duration(750)
.attr("y", function(d) {
return y(d.value);
})
.attr("height", function(d) {
return height - y(d.value);
});


// Existing code to draw y-axis:
var legendGroups = d3.select("#legend")
.selectAll(".legendGroup")
.data(group, function(d) {
return d.label; // always try and use a key function to uniquely identify
});

var enterGroups = legendGroups
.enter()
.append("g")
.attr("class", "legendGroup");

legendGroups
.exit()
.remove();

legendGroups
.attr("transform", function(d, i) {
return "translate(10," + (10 + i * 15) + ")"; // position the whole group
});

enterGroups.append("text")
.text(function(d) {
return d.label;
})
.attr("x", 15)
.attr("y", 10);

enterGroups
.append("rect")
.attr("width", 10)
.attr("height", 10)
.attr("fill", function(d) {
return "#bfe9bc";
})
.attr("class", function(d, i) {
return "legendcheckbox " + d.label.replace(/\s|\(|\)|\'|\,|\.+/g, '')
})

.on("click", function(d) {
d.active = !d.active;
d3.select(this).attr("fill", function(d) {
if (d3.select(this).attr("fill") == "#cccccc") {
return "#bfe9bc";
} else {
return "#cccccc";
}
})
var result = group.filter(function(d) {
return $("." + d.label.replace(/\s|\(|\)|\'|\,+/g, '')).attr("fill") != "#cccccc"
})

x.domain(result.map(function(d) {
return d.label;
}));

bar
.select(".x.axis")
.transition()
.call(xAxis);

bar
.data(result, function(d) {
return d.label.replace(/\s|\(|\)|\'|\,|\.+/g, '')
})
.enter()
.append("rect")
.attr("class", "bar")


bar
.transition()
.attr("x", function(d) {
return x(d.label);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.value);
})
.attr("height", function(d) {
return height - y(d.value);
});

bar
.data(result, function(d) {
return d.label.replace(/\s|\(|\)|\'|\,|\.+/g, '')
})
.exit()
.remove()
});

}

最佳答案

栏消失了,因为你用

完全删除了它
            .remove()

你可以做的是在没有像那样选择时隐藏元素:

       d3.selectAll('.graph').selectAll("rect").attr("visibility", function(e) {
return e.active ? "hidden" : "";
})

参见 http://plnkr.co/edit/2UWaZOsffkw1vkdIJuSA?p=preview

关于javascript - 在 d3 条形图上过滤后未重新绘制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482262/

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