简体   繁体   English

如何在 ngx-cytoscape 中获得 cy 对象的引用?

[英]How can I get a reference of the cy object in ngx-cytoscape?

I'm trying to use ngx-cytoscape .我正在尝试使用ngx-cytoscape

I've managed to get it work and the graph appears, but the next step would require getting a reference to the cy object.我已经设法让它工作并且图形出现了,但是下一步需要获取对 cy 对象的引用。

There is an example on ngx-cytoscape's GitHub page for adding plugins, but it doesn't say in which file that code snippet should be added to or what else is required to get it work. ngx-cytoscape 的 GitHub 页面上有一个用于添加插件的示例,但它没有说明应该将代码片段添加到哪个文件中,或者还需要什么才能使其工作。

Could anyone help with this?有人可以帮忙吗?

Demo Component演示组件

There is full version of demo component here - https://github.com/calvinvette/ngx-cytoscape/blob/master/cytodemo/cytodemo.component.ts这里有完整版的演示组件 - https://github.com/calvinvette/ngx-cytoscape/blob/master/cytodemo/cytodemo.component.ts

getting a reference to the cy object.获得对 cy 对象的引用。

You can get the reference by cy Object by using the @ViewChild in your component and you can get the access of cy object from this reference component in ngAfterViewInit function.您可以通过在组件中使用@ViewChild来获取cy对象的引用,并且您可以在ngAfterViewInit函数中从该引用组件中获取cy对象的访问权限。

@ViewChild(CytoscapeComponent)
private cytograph: CytoscapeComponent;

ngAfterViewInit() {
      if (this.cytograph.cy) {
        const cyLayer = this.cytograph.cy.cyCanvas();
        const cnv: HTMLCanvasElement = cyLayer.getCanvas();
        const ctx: CanvasRenderingContext2D = cnv.getContext("2d");
        // ...
          this.cytograph.cy.on("render cyCanvas.resize", function(evt, src) {
              // "this" is now "cy" inside this callback function
              cyLayer.resetTransform(ctx);
              cyLayer.clear(ctx);
              ctx.fillStyle = "#ff00ff";
              //ctx.fillRect(0, 0, 100, 100); // Top left corner
              cyLayer.setTransform(ctx);

              const width = cnv.width;
              const height = cnv.height;
              const data = Array(width * height);

              // Draw model elements
              this.nodes().forEach(function(node) {
                  const pos = node.position();
                  // Do something with canvas at or around the node's position
                  ctx.fillRect(pos.x - 25, pos.y - 25, 50, 50); // At node position (bisection point of 50x50 rectangle)
              });
          });
      }
      // ...
  }

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

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