【问题标题】:Angular2 Renderer: Svg rect is rendered but not showing in pageAngular2渲染器:渲染了Svg rect但未在页面中显示
【发布时间】:2018-03-28 23:33:05
【问题描述】:

我想使用 SVG 创建一个条形图,矩形为条形。

相关代码如下:

barchart-one.html

   <svg #svgone width="400" height="250" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250">
    <g #abcd></g>
</svg>

barchart-one.ts

 import { Component, Renderer2, ViewChild, ElementRef } from '@angular/core';

    @Component({
    selector: 'barchart-one',
    templateUrl: 'barchart-one.html'
    })
    export class BarchartOneComponent {
    @ViewChild('abcd') 
    private abcd: ElementRef;  
    constructor(private renderer: Renderer2) {}

    ngAfterViewInit() {
        for (var i = 1; i < 8; i++) {
            let height = Math.floor(Math.random() * (140 - 110)) + 110;
            const rect = this.renderer.createElement('rect');
            this.renderer.setAttribute(rect, 'height', height);
            this.renderer.setAttribute(rect, 'rx', '6');
            this.renderer.setAttribute(rect, 'ry', '6');
            this.renderer.setAttribute(rect, 'width', '12');
            this.renderer.setAttribute(rect, 'x', (i*50)+20);
            this.renderer.setAttribute(rect, 'y', (220-height));
            this.renderer.appendChild(this.abcd.nativeElement, rect);
            console.log(rect);
        };
    }
    }

svg 渲染结果:

<g>


<rect height="126" rx="6" ry="6" width="12" x="70" y="94"></rect>
<rect height="122" rx="6" ry="6" width="12" x="120" y="98"></rect>
<rect height="124" rx="6" ry="6" width="12" x="170" y="96"></rect>
<rect height="116" rx="6" ry="6" width="12" x="220" y="104"></rect>
<rect height="139" rx="6" ry="6" width="12" x="270" y="81"></rect>
<rect height="123" rx="6" ry="6" width="12" x="320" y="97"></rect>
<rect height="137" rx="6" ry="6" width="12" x="370" y="83"></rect>
</g>

即使 rect 的代码正确呈现,预期的结果也没有显示在页面中。

【问题讨论】:

    标签: javascript angular svg ionic3


    【解决方案1】:

    您不能使用 createElement 创建 SVG 元素,您必须使用 createElementNS 并传递 SVG 命名空间,即 http://www.w3.org/2000/svg 作为第一个参数。

    this.renderer.createElementNS('http://www.w3.org/2000/svg', 'rect');
    

    【讨论】:

    • 你能举个例子吗?
    【解决方案2】:

    这是一个老问题,但是,也许将来有人会发现它有用。有一个名为 ngx-svg 的 Angular 模块,它允许轻松创建多个 SVG 元素并将它们绘制在网站上。还可以与这些元素进行许多不同的交互。

    您的 html 代码将如下所示 -

    <svg-container containerId="barChart" height="500">
      <svg-rect height="126" width="12" color="#000" x="70" y="94"></svg-rect>
      <svg-rect height="122" width="12" color="#000" x="120" y="98"></svg-rect>
      <svg-rect height="124" width="12" color="#000" x="170" y="96"></svg-rect>
      <svg-rect height="116" width="12" color="#000" x="220" y="104"></svg-rect>
      <svg-rect height="139" width="12" color="#000" x="270" y="81"></svg-rect>
      <svg-rect height="123" width="12" color="#000" x="320" y="97"></svg-rect>
      <svg-rect height="137" width="12" color="#000" x="370" y="83"></svg-rect>
    </svg-container>
    

    上面的代码会自动创建和绘制你想要的图表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 2013-07-05
      • 2012-05-15
      相关资源
      最近更新 更多