【问题标题】:angular2 d3: update d3 svg mouse pos to component propertyangular2 d3:将 d3 svg 鼠标位置更新为组件属性
【发布时间】:2017-01-14 00:22:35
【问题描述】:

D3 用于在angular2 组件中生成svg。如何从 svg 事件 mousemove 更新组件中的属性 xy

export class AxisComponent implements OnInit {
    x:number;
    y:number;     

    ngOnInit() {
        var svgWidth=400;
        var svgHeight=400;
        var margin = {top:25, right:25, bottom:50, left:50};
        var width = svgWidth - margin.left - margin.right;
        var height = svgHeight - margin.top - margin.bottom;

        var svg = d3.select('#container').append('svg')
            .attr('width', svgWidth)
            .attr('height',svgHeight)
            .style('border', '2px solid');

        svg.on("mousemove", function(){
            var xy = d3.mouse(this);

            this.x = xy[0]; 
            this.y = xy[0];
        });
}

mousemove事件访问时出错:

【问题讨论】:

    标签: d3.js svg angular


    【解决方案1】:

    我怀疑应该是:

    svg.on("mousemove", () => {
       var xy = d3.mouse(svg); // or d3.mouse(d3.event.currentTarget);
       this.x = xy[0]; 
       this.y = xy[0];
    

    或者这样:

    let self = this;
    svg.on("mousemove", function(){
      var xy = d3.mouse(this);
    
      self.x = xy[0]; 
      self.y = xy[0];
    });
    

    【讨论】:

    • d3.mouse(this) 是正确的。但是由于某种原因,在角度类型脚本引用中,这将返回 null。在嵌入在 typescript ref 中的 commonjs 中调用它仍然有效.. 仍在调查中。
    猜你喜欢
    • 2013-05-22
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多