首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngx-charts:通过使用属性绑定更新dom元素来更改图表

ngx-charts:通过使用属性绑定更新dom元素来更改图表
EN

Stack Overflow用户
提问于 2017-05-30 17:50:55
回答 1查看 1.9K关注 0票数 0

我想通过从Dom更改ngx图表并在属性绑定中插入新的图表来更改ngx图表,但是更改Dom是由innerHTML=stringVar完成的;但是如果我的stringVar内部是ngx-chart元素,它不会呈现它。

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { NgxChartsModule } from '@swimlane/ngx-charts';

import { single, multi } from '../../shared/data';
import { CHARTS } from '../../shared/mock-charts';

@Component({
 selector: 'app-chart-graph',
 template: '<div [innerHTML]="ChartGraph"></div>',
styleUrls: ['./chart-graph.component.css']
})
export class ChartGraphComponent implements OnInit {
 ChartGraph = `<ngx-charts-bar-vertical
  [view]="view"
  [scheme]="colorScheme"
  [results]="single"
  [gradient]="gradient"
  [xAxis]="showXAxis"
  [yAxis]="showYAxis"
  [legend]="showLegend"
  [showXAxisLabel]="showXAxisLabel"
  [showYAxisLabel]="showYAxisLabel"
  [xAxisLabel]="xAxisLabel"
  [yAxisLabel]="yAxisLabel"
  (select)="onSelect($event)">
</ngx-charts-bar-vertical>`;

 single: any[];
 multi: any[];
 CHARTS: any[];

 // options
 showXAxis = true;
 showYAxis = true;
 gradient = false;
 showLegend = true;
 showXAxisLabel = true;
 xAxisLabel = 'Country';
 showYAxisLabel = true;
 yAxisLabel = 'Population';

 colorScheme = {
 domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']
 };

constructor() {
Object.assign(this, { single, multi });
}

 onSelect(event) {
  console.log(event);
}

ngOnInit() {

}

}
EN

回答 1

Stack Overflow用户

发布于 2017-05-31 15:05:24

代码语言:javascript
复制
It's not work with property  binding just do like add that in template for good practice.


    @Component({
        selector: 'app-chart-graph',
        styleUrls: ['./chart-graph.component.css'],
        template: '<ngx-charts-bar-vertical
                     [view]="view"
                     [scheme]="colorScheme"
                     [results]="single"
                     [gradient]="gradient"
                     [xAxis]="showXAxis"
                     [yAxis]="showYAxis"
                     [legend]="showLegend"
                     [showXAxisLabel]="showXAxisLabel"
                     [showYAxisLabel]="showYAxisLabel"
                     [xAxisLabel]="xAxisLabel"
                     [yAxisLabel]="yAxisLabel"
                    (select)="onSelect($event)">
               </ngx-charts-bar-vertical>'
    })

    export class ChartGraphComponent implements OnInit {
      single: any[];
      multi: any[];
      CHARTS: any[];

    // options
    showXAxis = true;
    showYAxis = true;
    gradient = false;
    showLegend = true;
    showXAxisLabel = true;
    xAxisLabel = 'Country';
    showYAxisLabel = true;
    yAxisLabel = 'Population';

    //remaining code
    }

将此添加到您的模板中

代码语言:javascript
复制
 //you can also add @input() in graph component options pass them in like
     <app-chart-graph [width]="" [height]="" [data]=""..></app-chart-graph>
   </div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44258922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档