简体   繁体   English

如何从d3选择中获取d3实例?

[英]How to get d3 instance from d3 selection?

I have a method that gets a d3 selection as an argument: 我有一个方法将d3选择作为参数:

function foo(selection){

}

Inside that function I need the d3 instance. 在该函数内部,我需要d3实例。 Instead of passing the d3 instance as an extra argument I would like to get it from the selection. 与其将d3实例作为额外的参数传递,不如从选择中获取它。 Is that possible? 那可能吗?

function foo(selection){
    var d3 = selection.getD3();
    var element = document.createElement('div');
    d3.select(element);
}

Edit 编辑

The methods provided by the selection do not include something like "getD3": 选择提供的方法不包括“ getD3”之类的东西:

append
attr
call
classed
clone
constructor
data
datum
dispatch
each
empty
enter
exit
filter
html
insert
interrupt
lower
merge
node
nodes
on
order
property
raise
remove
select
selectAll
size
sort
style
text
transition

You can add a reference to d3 by adding it to the selection object 您可以通过将其添加到selection对象中来添加对d3的引用

var mySelection = d3.selectAll('your_selection_criteria');

mySelection.d3_reference = d3;  //mySelection is a JSON object, so
                                // you can attach anything to it

foo(mySelection);


function foo(selection) {
    var myD3 = selection.d3_reference;
    // now myD3 is d3
}

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

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