简体   繁体   English

是否可以使用鼠标在d3.js中的条形图上选择多个条形

[英]Is it possible to select multiple bars on bar chart in d3.js with mouse

I was trying to google for this, but no luck. 我试图谷歌这个,但没有运气。

What i want is to take standard bar chart in d3.js, for example like this 我想要的是在d3.js中采用标准条形图,例如像这样

http://bl.ocks.org/1218567 http://bl.ocks.org/1218567

and select with my mouse middle 4 bars, and somehow get value of selected values only. 并用我的鼠标中间选择4个条,并以某种方式获取所选值的值。

Is such thing possible with d3? d3有可能这样吗?


EDIT: I think my question might be bit misleading, I don't want to do multiple clicks to select objects, i want to click and drag once and select underlying objects, see sencha example in my comment. 编辑:我认为我的问题可能有点误导,我不想多次点击选择对象,我想点击并拖动一次并选择底层对象,请参阅我的评论中的sencha示例。

SOLVED: 解决了:

d3.brush is the answer, you can see the working demo here http://mbostock.github.com/d3/ex/splom.html d3.brush是答案,你可以在这里看到工作演示http://mbostock.github.com/d3/ex/splom.html

You can implement this using the .on() function and the click event. 您可以使用.on()函数和click事件来实现它。 See the documentation . 请参阅文档 To be able to select multiple bars, you would need to keep track of the current selection in a global variable, for example an array that is added to and removed from as you click on unselected/selected bars. 为了能够选择多个条形,您需要跟踪全局变量中的当前选择,例如,在单击未选择/选定条形时添加和删除的数组。 The code might look something like 代码可能看起来像

var selection = [];
...
var bars = vis.selectAll("g.bar")
    .data(data)
    .enter()
    .append("svg:g")
    ...
    .on("click", function(d) {
        if(selection.indexOf(d) == -1) { selection.push(d); }
        else { selection.splice(selection.indexOf(d), 1); }
        updateSelectionDisplay();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM