简体   繁体   English

在Angular2中动态着色图形

[英]Dynamically Colouring graphic in Angular2

I have an angular2 app, and in one of my components, I want to display the performance of various elements of a system. 我有一个angular2应用,在我的一个组件中,我想显示系统各个元素的性能。 So I have the following graphic: 所以我有以下图形:

在此处输入图片说明

Which is an svg, so I had thought that I would manually tag the boxes with a #box1 , so I could do something like @ViewChild('box1') stage1: ElementRef; 这是一个svg,因此我以为我可以手动使用#box1标记框,因此可以执行类似@ViewChild('box1') stage1: ElementRef; , however, any angulr2 additions seem to stop the svg rendering. ,但是,任何angulr2添加似乎都停止了svg渲染。

My template currently has <object id="img" data="assets/img/big_layout.svg" width="292" height="164"></object> in it. 我的模板当前包含<object id="img" data="assets/img/big_layout.svg" width="292" height="164"></object>

How can I create an image like this and programmatically change parts of its colour? 如何创建这样的图像并以编程方式更改其颜色的一部分?

I thought maybe this could be drawn in divs and use css, but that would be crazy difficult to get it all to scale properly, but if there is some way to generate this, that could be an option? 我认为也许可以在divs中绘制它并使用css,但这很难使所有内容正确缩放,但是如果有某种方法可以生成它,那可以选择吗?

Update: 更新:

Here is a plunk to show the rendering issues if I put any angular specific code into the svg file. 这里是一个普拉克显示渲染的问题,如果我把任何角度特定代码到SVG文件。

You can't get reference to element inside <object> via ViewChild 您无法通过ViewChild引用<object> ViewChild

As workaround you i can offer you the following way: 作为解决方法,我可以为您提供以下方式:

view: 视图:

<object ... #svg (load)="load(svg.contentDocument)"></object>

component 零件

load(svgDoc) {
  let box1 = svgDoc.getElementById("station1");
}

See also this in action Plunker 另请参阅此动作Plunker

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

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