svg
变量是包含DOM元素的d3选择对象。
你可以设置
datum()
然后检索它
function createChart(chartID, data) {
// creating the SVG and appending it to the body
var svg = d3.select('body').append('svg')
.attr('width',400)
.attr('height',400)
.attr('id',chartID);
// instantiating an instance of MyClass to be attached to the SVG
var anObject = new MyClass();
svg.datum(anObject);
// code to actually build the chart + math to be done on passed data
// finally, attach a click event to the svg so that data can be manipulated later on
svg.on('click',function(){
var object = d3.select(this).datum();
object.doSomething();
});
}
或者利用这个事实
this
在里面
click
处理程序是一个DOM元素
function createChart(chartID, data) {
// creating the SVG and appending it to the body
var svg = d3.select('body').append('svg')
.attr('width',400)
.attr('height',400)
.attr('id',chartID);
// instantiating an instance of MyClass to be attached to the SVG
var anObject = new MyClass();
svg.node().myObject = anObject;
// code to actually build the chart + math to be done on passed data
// finally, attach a click event to the svg so that data can be manipulated later on
svg.on('click',function(){
var object = this.myObject;
object.doSomething();
});
}