加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – d3-ng2-service error“属性链接不存在类型force()”

发布时间:2020-12-17 17:32:34 所属栏目:安全 来源:网络整理
导读:我刚刚开始使用 Angularjs 2和d3-ng2-service,我正在尝试使用网络图表. 我用例子 https://bl.ocks.org/mbostock/2675ff61ea5e063ede2b5d63c08020c7 并将其转换为d3-ng2-service. 我收到以下错误 “属性链接不存在类型force()” 使用以下代码 simulation.forc
我刚刚开始使用 Angularjs 2和d3-ng2-service,我正在尝试使用网络图表.

我用例子

https://bl.ocks.org/mbostock/2675ff61ea5e063ede2b5d63c08020c7

并将其转换为d3-ng2-service.

我收到以下错误

“属性链接不存在类型force()”

使用以下代码

simulation.force("link")
    .links(links);

完整代码如下

import { Component,ElementRef,NgZone,OnDestroy,OnInit } from     '@angular/core';

import { links,nodes} from './networkData.component';

import {
  D3Service,D3,Axis,ScaleLinear,ScaleOrdinal,Selection,Simulation,Transition
} from 'd3-ng2-service';


@Component({
  selector: 'network1',template: '<svg width="100%" height="100%"></svg>',styleUrls: ['./network.component.css']
})
export class Network1Component implements OnInit,OnDestroy {
  private d3: D3;
  private parentNativeElement: any;
  private d3Svg: Selection<SVGSVGElement,any,null,undefined>;


  constructor(element: ElementRef,private ngZone: NgZone,d3Service: D3Service)    {
    this.d3 = d3Service.getD3();
    this.parentNativeElement = element.nativeElement;
  }

  ngOnDestroy() {
    if (this.d3Svg.empty && !this.d3Svg.empty()) {
      this.d3Svg.selectAll('*').remove();
    }
  }

  ngOnInit() {
        let self = this;
    let d3 = this.d3;
    let width: number;
    let height: number;
    let d3ParentElement: Selection<HTMLElement,undefined>;
    let d3Svg: Selection<SVGSVGElement,undefined>;




    if (this.parentNativeElement !== null) {

      d3ParentElement = d3.select(this.parentNativeElement);
      d3Svg = this.d3Svg = d3ParentElement.select<SVGSVGElement>('svg');

      width = +d3Svg.attr('width');
      height = +d3Svg.attr('height');

      var color = d3.scaleOrdinal(d3.schemeCategory20);

      var simulation = d3.forceSimulation()
        .force("link",d3.forceLink().id(function(d) { return d[0].id; }))
        .force("charge",d3.forceManyBody())
        .force("center",d3.forceCenter(width / 2,height / 2));

      var link = d3Svg.append<SVGGElement>('g')
        .attr("class","links")
        .selectAll("line")
        .data(links)
        .enter().append("line")
        .attr("stroke-width",function(d) { return Math.sqrt(d.value); });

      var node = d3Svg.append<SVGGElement>('g')
        .attr("class","nodes")
        .selectAll("circle")
        .data(nodes)
        .enter().append("circle")
        .attr("r",5)
        .attr("fill",function(d) { return color(d.id); });


      node.append("title")
        .text(function(d) { return d.id; });

      simulation
        .nodes(nodes)
        .on("tick",ticked);

        simulation.force("link")
        .links(links);


    }

    function ticked() {
      link
        .attr("x1",function(d) { return d.source; })
        .attr("y1",function(d) { return d.source; })
        .attr("x2",function(d) { return d.target; })
        .attr("y2",function(d) { return d.target; });

      node
        .attr("cx",function(d) { return d.id; })
        .attr("cy",function(d) { return d.id; });
    }

    function dragstarted(d) {
      if (!d3.event.active) simulation.alphaTarget(0.3).restart();
      d.fx = d.x;
      d.fy = d.y;
    }

    function dragged(d) {
      d.fx = d3.event.x;
      d.fy = d3.event.y;
    }

    function dragended(d) {
      if (!d3.event.active) simulation.alphaTarget(0);
      d.fx = null;
      d.fy = null;
    }

  }

}

解决方法

在将bl.ock移植到TypeScript时,需要进行一些调整以确保节点和链接的数据类型以及正确处理不同的强制类型.

forceSimulation在调用它时接受两个泛型.让我们假设,您的节点类型为YourNodeType,它扩展了SimulationNodeDatum,链接类型为YourLinkType,它扩展了SimulationLinkDatum< YourNodeType> .SimulationNodeDatum和SimulationLinkDatum是d3-force定义中定义的接口.

(见d3-force definition).

话虽如此,您应该定义let simulation = d3.forceSimulation< YourNodeType,YourLinkType>()这样,节点和您的情况重要的是链接类型在整个模拟过程中强制执行.

现在,对于您遇到的错误的核心,为了检索具有除最小Force接口之外的属性的力,有必要将力转换为事前已知类型.在您的情况下,链接强制在定义中具有预定义的接口.

因此,simulation.force< ForceLink< YourNodeType,YourLinkType>>(‘link’)将返回具有已定义的links()方法的链接力.

由于您使用的是d3-ng2-service,因此可以直接从d3-ng2-service将接口SimulationNodeDatum,SimulationLinkDatum和ForceLink导入到包含角度组件的文件中.

如果您使用strictNullChecks,您可能需要simulation.force< ForceLink< YourNodeType,YourLinkType>>(‘link’)!.links(),其中!解决了一般情况下返回力可能未定义的情况.事实并非如此.

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读