我正在开发 D3(Rickshaw 框架),我必须使用名称过滤数据。
这是人力车用于创建圆圈的代码。
var nodes = graph.vis.selectAll("path").data(series.stack.filter(function(d) {
return d.y !== null
})).enter().append("svg:circle").attr("cx", function(d) {
return graph.x(d.x)
}).attr("cy", function(d) {
return graph.y(d.y)
}).attr("fill-opacity", 1.0).attr('transform', 'translate(22,0)').attr("r", function(d) {
return ("r" in d) ? d.r : graph.renderer.dotSize
});
我尝试过滤数据,例如
$('#BestBrands input').on("change", function () {
console.log("called")
var selected = $(this).attr('name'),
display = this.checked ? "inline" : "none";
console.log(selected)
graph.vis.selectAll(".filter")
.data(series.stack.filter(function(d) {
return series.name[0] == selected
}))
.attr("display", display);
});
//series.name 等于 d3.js 中的 d.name,因此 series[0] 是第一个坐标名称 它不起作用。我需要在类(class)中添加任何类(class)吗?我对此不太清楚。这里应该怎么做才能根据名称过滤数据呢?
最佳答案
如果我理解你的代码:
graph.vis.selectAll("path")
.data(series.stack.filter(function(d){
//should return a boolean
return selected === 'something';
}))
.attr("display", display);
});
编辑:
$('#BestBrands input').on("change", function () {
console.log("called")
var selected = $(this).attr('name'),
display = this.checked ? "inline" : "none";
console.log(selected)
graph.vis.selectAll(".filter")
.data(series.stack.filter(function(d) {
return series[0].name == selected //assuming series[] is an array
}))
.attr("display", display);
});
关于javascript - D3 中的过滤选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17184345/