angular2图表和事件

Noa*_*Noa 2 events charts zingchart angular

我在angular2应用程序中使用zingchart并且遇到了这个问题.我的图表数据已经改变,我需要重新渲染图表,我只是不知道该怎么做?这是zing chart团队提供的rerender按钮的傻瓜.我不知道如何重新渲染图表. https://plnkr.co/edit/OjqvVPiyKBUJbKgcLi6A?p=preview

  import {bootstrap} from 'angular2/platform/browser';
import {Component, NgZone, AfterView, OnDestroy} from 'angular2/core'

class Chart { 
  id: String;
  data: Object;
  height: any;
  width: any;
  constructor(config: Object) {
    this.id = config['id'];
    this.data = config['data'];
    this.height = config['height'] || 300;
    this.width = config['width'] || 600;
  }
}

@Component({
  selector : 'zingchart',
  inputs : ['chart'], 
  template : `
   <div id='{{chart.id}}'></div>
  `
})
class ZingChart implements AfterView, OnDestroy {
  chart : Chart;
  constructor(private zone:NgZone) {
  }

  ngAfterViewInit() {
      this.zone.runOutsideAngular(() => {
          zingchart.render({
              id : this.chart['id'],
              data : this.chart['data'],
              width : this.chart['width'],
              height: this.chart['height']
          });
      });
  }
  ngOnDestroy() {
      zingchart.exec(this.chart['id'], 'destroy');
  }
}

//Root Component
@Component({
  selector: 'my-app',
  directives: [ZingChart]
  template: `
    <zingchart *ngFor="#chartObj of charts" [chart]='chartObj'></zingchart>
     <button type="button" class="btn btn-default" (click)="rerender()">re-render</button>
  `,
})
export class App {
  charts : Chart[];
  constructor() {
    this.name = 'Angular2'
    this.charts = [{
      id : 'chart-1',
      data : {
        type : 'line',
        series : [{
          values :[2,3,4,5,3,3,2]
        }],
      },
      height : 400,
      width : 600
    }]
  }

  rerender(){
    alert("please help me don't know  what to write here");
  }
}


bootstrap(App, [])
  .catch(err => console.error(err));
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 5

我将使用@ViewChild装饰器从父级引用图表组件:

@Component({
  selector: 'my-app',
  directives: [ZingChart]
  template: `
    <zingchart *ngFor="#chartObj of charts" [chart]='chartObj'></zingchart>
    <button type="button" class="btn btn-default" (click)="rerender()">re-render</button>
  `,
})
export class App {
  charts : Chart[];

  @ViewChild(ZingChart)
  chart: ZingChart;

  (...)
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以在图表组件本身上调用方法.例如渲染一个:

@Component({
  (...)
})
export class App {
  (...)
  rerender(){
    this.chart.render();
  }
}
Run Code Online (Sandbox Code Playgroud)

看到这个plunkr:https://plnkr.co/edit/NggADnGRbw4TsYiIUsh3 p = preview

  • 只是添加到这个示例(仍然需要@ViewChild(ZingChart)),您可以利用ZingChart的API方法更新图表,而无需重新绘制所有内容,使其更高效.我在这个例子中使用'setdata'API方法:https://plnkr.co/edit/6dt3uyttnDrqHMjpfVmK?p = preview (3认同)
  • @ mike-schultz非常感谢我正在寻找的东西.非常感谢. (2认同)