我有一个将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发布于 2018-04-26 08:10:49
通过将引用添加到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
}https://stackoverflow.com/questions/50037632
复制相似问题