简体   繁体   English

使用d3 js时,Renderer2不渲染SVG元素 - Angular 4

[英]Renderer2 does not render SVG element while using d3 js - Angular 4

After going through the below link 通过以下链接后

Using D3.js with Angular 2 将D3.js与Angular 2一起使用

I understood that we should not be manipulating DOM directly. 我明白我们不应该直接操纵DOM。 So I started exploring to use Renderer2 of angular 4. 所以我开始探索使用角度4的Renderer2。

So I started to write some code to add svg to DOM. 所以我开始编写一些代码来将svg添加到DOM中。 Below are the snippets of my code. 以下是我的代码的片段。

HTML code HTML代码

<div class="row">
 <div class="col-12">
   <div #myBarGraph id="host"></div>
 </div>
</div>

Component.ts Component.ts

export class BarGraphComponent implements OnInit {
 host:any;
 svg:any;
 @ViewChild('myBarGraph') myBarGraph:ElementRef;

 constructor(private renderer:Renderer2) { }

 ngOnInit() {
  //some logic
 }

 // called on some drop down selection
 teamSelection(){
  //some logic
   this.host = this.mybarGraph.nativeElement;
   buildSVG();
 }

 buildSVG():void {
   this.svg = this.renderer.createElement('svg');
   this.renderer.setAttribute(this.svg,'width','600');
   this.renderer.setAttribute(this.svg,'height','400');
   this.renderer.setAttribute(this.svg,'background-color','blue');
   this.renderer.appendChild(this.host,this.svg); 
 }
}

The above code is able to add the svg element to the DOM. 上面的代码能够将svg元素添加到DOM。 But to my surprise, it actually not displaying the svg element!. 但令我惊讶的是,它实际上没有显示svg元素!

I did refer to the following questions 我确实参考了以下问题

  1. Angular2 Renderer: Svg rect is rendered but not showing in page Angular2渲染器:渲染了Svg rect但未在页面中显示
  2. Angular2 does not render SVG in runtime Angular2不会在运行时呈现SVG

But I couldn't find the proper answer so far. 但到目前为止我找不到合适的答案。

Below is a screenshot of the problem 以下是问题的屏幕截图 问题

As you can see above, svg is present in DOM, but it is not displayed in web page. 如您所见,svg存在于DOM中,但它不会显示在网页中。 Any help would be much appreciated. 任何帮助将非常感激。

this.renderer.createElement('svg') creates HTML element instead of SVGElement because Angular doesn't know you wanted svg:svg. this.renderer.createElement('svg')创建HTML元素而不是SVGElement,因为Angular不知道你想要svg:svg。

Try using this.renderer.createElement('svg', 'svg') , where second parameter is the Namespace. 尝试使用this.renderer.createElement('svg', 'svg') ,其中第二个参数是命名空间。 Every svg element must be created in the same way, eg: 必须以相同的方式创建每个svg元素,例如:

let rect = this.renderer.createElement('rect', 'svg')
this.renderer.appendChild(svg, rect)

You have correctly added an svg element with a height and width to the view. 您已正确地向视图添加了具有高度和宽度的svg元素。 But an svg element does not have a background-color attribute. 但是svg元素没有background-color属性。 You can think of it as the canvas on top of which you layer other elements that have shape and colour. 您可以将其视为画布,在其上层叠其他具有形状和颜色的元素。

You need to add another element to this view to see anything. 您需要在此视图中添加另一个元素才能看到任何内容。 Remove the background-color attribute from the svg, then, as you have added the svg element to the view, you can use d3 to add, say, a rect to the view: 从svg中删除background-color属性,然后,当你将svg元素添加到视图中时,你可以使用d3为视图添加一个rect:

d3.select(this.svg)
  .append('rect')
  .attr('x', 0)
  .attr('y', 0)
  .attr('width', 600)
  .attr('height', 400)
  .attr('fill', 'blue');

alternatively, you could just do the following: 或者,您可以执行以下操作:

@Component({
  selector: 'bar-chart',
  template: `
    <div class='bar-chart' #barChart></div>
  `
})
export class BarChartComponent implements OnInit {

  @ViewChild('barChart')
  public chart: ElementRef;

  public svg: any;

  private width: number = 600;
  private height: number = 400;

  ngOnInit() {
    this.svg = d3.select(this.chart.nativeElement)
      .append('svg')
      .attr('width', this.width)
      .attr('height', this.height);

    this.svg
      .append('rect')
      .attr('x', 0)
      .attr('y', 0)
      .attr('width', this.width)
      .attr('height', this.height)
      .attr('fill', 'blue');
  }
}

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

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