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

时间:2018-04-26 07:53:15

标签: javascript d3.js selection

我有一个方法可以将d3选择作为参数:

function foo(selection){

}

在该函数内部我需要d3实例。我没有将d3实例作为额外的参数传递,而是希望从选择中获取它。这可能吗?

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

修改

选择提供的方法不包括“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

1 个答案:

答案 0 :(得分:-1)

您可以通过将pod 'react-native-onesignal', :podspec => "../node_modules/react-native-onesignal/react-native-onesignal.podspec", :path => '../node_modules/react-native-onesignal' 对象

添加到d3来添加对selection的引用
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
}