简体   繁体   English

在angular 4中实现d3.js

[英]Implementing d3.js in angular 4

I am drawing polygon by using mouse. 我正在使用鼠标绘制多边形。 I managed to do the code using normal javascript file. 我设法使用普通的javascript文件执行代码。 Now I want to implement the same thing in my typescript file 现在我想在我的打字稿文件中实现相同的功能

This is my d3.js file 这是我的d3.js文件

//D3.JS VERSION 3
//---------------------------
var dragger = d3.behavior.drag()
.on('drag', handleDrag)
.on('dragend', function(d){
    dragging = false;
});


function handleDrag() {
    if(drawing) return;
    var dragCircle = d3.select(this), newPoints = [], circle;
    dragging = true;
    var poly = d3.select(this.parentNode).select('polygon');
    var circles = d3.select(this.parentNode).selectAll('circle');

    for (var i = 0; i < circles[0].length; i++) {
        circle = d3.select(circles[0][i]);
        newPoints.push([circle.attr('cx'), circle.attr('cy')]);
    }
      poly.attr('points', newPoints);

          console.log('newPoints', newPoints);
    dragArea = d3.polygonArea(newPoints);
    console.log('dragArea',dragArea);
     d3.select("#toolTipArea").text('Area = '+dragArea+' sqft');

    dragCircle
    .attr('cx', d3.event.x)
    .attr('cy', d3.event.y)
        .on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px").text(""+dragArea+"sqft")})
        .on("mouseout", function(){return tooltip.style("visibility", "visible").text(""+dragArea+"sqft")})
        .on("mouseover", function(){return tooltip.style("visibility", "visible").text(""+dragArea+"sqft")});
        //         .on("mousemove", tooltipMaster(1))
        // .on("mouseout", tooltipMaster(2))
        // .on("mouseover", tooltipMaster(3));

}

This code work fine in normal html n js file. 此代码在普通的html n js文件中可以正常工作。
Now I want to implement this code in my angular4 .But, I got few problems in my typescript file.I have installed the latest d3.js version, version 4. 现在我想在我的angular4中实现此代码,但是我的打字稿文件中遇到了几个问题,我安装了最新的d3.js版本,即版本4。

1)First problem is 1)第一个问题是

I need to change my d3.behavior.drag() function because, it is not supported in version 4. So I changed to:- 我需要更改d3.behavior.drag()函数,因为它在版本4中不受支持。因此,我更改为:-

     this.dragger = d3.drag()
         .on("drag", this.handleDrag())
         .on("end", this.endDrag()); 

2)and this is the handleDrag() (which lies the second problem) 2)这是handleDrag()(这是第二个问题) 在此处输入图片说明

Like it shows, I got problem at .this, this.parentNode etc 就像它显示的那样,我在.this,this.parentNode等处遇到问题
so how should I call .this/this.parentNode in angular(in normal js file it works). 所以我应该如何在angular中调用.this / this.parentNode (在正常的js文件中,它可以工作)。

3)Showing this error at:- 3)在以下位置显示此错误: 在此处输入图片说明


So , I got all this error when I want to implement the normal d3.js function into my ts file , angular 4 . 因此,当我想在我的ts文件(角度为4)中实现普通的d3.js函数时,遇到了所有这些错误。 I need to know the proper way to call in angular. 我需要知道调用角度的正确方法。
Thank you. 谢谢。

Make sure you pass the actual function and not the return value of the function, by eliminating the () : 通过消除() ,确保传递实际函数而不是函数的返回值:

 this.dragger = d3.drag()
     .on("drag", this.handleDrag)
     .on("end", this.endDrag); 

For your other issue, use (this as any).parentNode to allow this to be treated as any object to avoid typescript issues. 对于你的另一个问题,使用(this as any).parentNode ,让this被任何物体,以免打字稿问题处理。

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

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