【问题标题】:how to render svg text to the dom element in angular 8如何以角度8将svg文本呈现给dom元素
【发布时间】:2021-10-09 14:26:53
【问题描述】:

我有一个 fusionchart 图表,当用户单击获取 svg 字符串按钮时,我想从中获取图像并显示在同一个 html 页面中。

我正在从 this.chart.getSVGString() 获取 svg 文本,但是如何在 dom 中呈现它。

    import { Component } from "@angular/core";
`  `import { dataSource } from "./dataSource";
    @Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
})
export class AppComponent {
  chart: any;
  svgPath:any;
  dataSource: dataSource = {
    chart: {
      caption: "Countries With Most Oil Reserves [2017-18]",
      subCaption: "In MMbbl = One Million barrels",
      xAxisName: "Country",
      yAxisName: "Reserves (MMbbl)",
      numberSuffix: "K",
      theme: "fusion"
    },
    data: [
      { label: "Venezuela", value: "290" },
      { label: "Saudi", value: "260" },
      { label: "Canada", value: "180" },
      { label: "Iran", value: "140" },
      { label: "Russia", value: "115" },
      { label: "UAE", value: "100" },
      { label: "US", value: "30" },
      { label: "China", value: "30" }
    ]
  };
  initialized($event) {
    this.chart = $event.chart; // Storing the chart instance
  }

  getSVG() {
    console.log(this.chart.getSVGString());
    this.svgPath = this.chart.getSVGString()
  }
}

这是 app.component

 <fusioncharts
  width="500"
  height="400"
  type="column2d"
  dataFormat="json"
  [dataSource]="dataSource"
  (initialized)="initialized($event)"
>
  >
</fusioncharts>
<div [innerHTML] = "svgPath"></div>
<button (click)="getSVG()">Get SVG String</button>

这是 app.html

https://codesandbox.io/s/angular-forked-b27eg?file=/src/app/app.component.html:0-256

【问题讨论】:

    标签: javascript angular fusioncharts


    【解决方案1】:

    您需要绕过默认的 DOM 清理才能使其正常工作,请参阅下面的代码

    const data = this.chart.getSVGString();
    this.svgPath = this.domSanitizer.bypassSecurityTrustHtml(data);
    

    像这样导入 DomSanitizer:

    import { DomSanitizer } from "@angular/platform-browser";
    

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 2023-01-30
      • 2019-04-08
      • 1970-01-01
      • 2023-03-09
      • 2011-12-08
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多