简体   繁体   English

角(6)和d3(v5):一个svg中作为子元素的组件,并将svg元素传递给每个组件

[英]Angular (6) and d3 (v5): components as subelements in one svg and passing svg element to each component

I am using Angular 6 and d3 v5 . 我正在使用Angular 6d3 v5 I have created a Glyph component with html template 我用html模板创建了一个字形组件

<svg:g #glyph></svg:g>

and in order to add svg elements using d3 , in the .ts file I have selector: '[glyph]' and 为了使用d3添加svg元素,在.ts文件中,我有selector: '[glyph]'

@ViewChild('glyph') private glyphContainer : ElementRef;
...
const glyphElement = this.glyphContainer.nativeElement;
d3.select(glyphElement).append("svg:rect")
...

In the parent component I can use <svg glyph></svg> to show this. 在父组件中,我可以使用<svg glyph></svg>进行显示。


I want to add multiple instances of this Glyph component into the same svg, for instance, in a GlyphList component. 我想将此Glyph组件的多个实例添加到同一svg中,例如,在GlyphList组件中。 In order to manage d3 selections, transitions, and interaction, I want each individual component to be part of the same svg (or better yet, part of the same svg:g element). 为了管理d3选择,过渡和交互,我希望每个单独的组件都成为同一svg一部分(或者更好的是,成为同一svg:g元素的一部分)。

As an example, imagine that I am creating a visualization on a file system and Glyph is a custom svg:g UI element visualizing a particular file or folder, and I have a directory of files that I want visualized with a GlyphList component, all in the same svg . 举例来说,假设我正在文件系统上创建可视化对象,而Glyph是一个自定义svg:g UI元素,用于可视化特定文件或文件夹,并且我有一个要使用GlyphList组件可视化的文件目录,所有这些相同的svg

How can I do this in Angular? 如何在Angular中做到这一点? Namely, how do I structure the template and initiate each Glyph component and pass in a shared svg element in a GlyphList component (or service?) that I can select with d3 in each of the child components? 也就是说,如何构造模板并启动每个Glyph组件,并在GlyphList组件(或服务)中传递一个共享的svg元素,我可以在每个子组件中使用d3进行选择?

Check this tutorial, in the section SVG Components with Angular, there is a section of how to do exactly what you are asking for. 查看本教程,在“带有Angular的SVG组件”部分中,有一节专门介绍如何准确地执行您的要求。

https://medium.com/netscape/visualizing-data-with-angular-and-d3-209dde784aeb https://medium.com/netscape/visualizing-data-with-angular-and-d3-209dde784aeb

<svg>
    <g [lineExample]></g>
</svg>

And your svg component: import { Component } from '@angular/core'; 还有您的svg组件:从'@ angular / core'导入{Component};

@Component({
    selector: '[lineExample]',
    template: `<svg:line x1="0" y1="0" x2="100" y2="100"></svg:line>`
})
export class LineExampleComponent {
    constructor() {}
}

However, I end up here because I can't make it work, Angular gives me this error 但是,我最终因为无法正常工作而来到这里,Angular给了我这个错误

compiler.js:1021 Uncaught Error: Template parse errors:
Can't bind to 'lineExample' since it isn't a known property of ':svg:g'. ("<svg>
    <g [ERROR ->][lineExample]></g>
</svg>"): ng:///AppModule/TrackComponent.html@1:7

Let me know if you make it work! 让我知道您是否可以使用它!

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

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