图形工具 - Angular2

时间:2016-07-18 21:32:57

标签: javascript charts angular zingchart

我对Web应用程序比较陌生,因此我刚刚开始使用Angular2(没有使用angularJS)和Typescript。我正在尝试使用Zingchart绘制图表。我经历了5分钟的快速启动以及angular2页面中的教程,并且对它的工作方式有了一个很好的了解。我按照Zingchart页面上的说明使用两个工具(https://blog.zingchart.com/2016/03/16/angular-2-example-zingchart/)在网页上创建了一个图表。但是,我似乎遇到了问题。 1)我无法从@ angular / core导入AfterView。虽然页面说我必须使用angular2 / core我使用@ angular / core作为源文件夹来导入模块。 angular2 / core未得到认可。 2)当有绑定到zingchart对象的函数(例如 - zingchart.render(),zingchart.exec())时,会出现一个错误,指出无法找到zingchart。我也不确定这里出了什么问题。

import { Component, NgZone, AfterViewInit, OnDestroy }        from '@angular/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 AfterViewInit, 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>
`,
})
export class App {
charts : Chart[];
constructor() {
this.charts = [{
  id : 'chart-1',
  data : {
    type : 'line',
    series : [{
      values :[2,3,4,5,3,3,2]
    }],
  },
  height : 400,
  width : 600
}]
}
}

1 个答案:

答案 0 :(得分:5)

完全披露,我是ZingChart团队的成员。

1)"I am not able to import AfterView from @angular/core. Although the page says that I must be using angular2/core I am using @angular/core as the source folder"

如果不遵守blog post中的说明,那么在写这篇文章时你已经破坏了Angular 2的语法。用于导入函数及其名称的Angular 2语法从2.0.0 beta 9(编写此版本的版本)和2.0.0 RC0(我假设您正在使用的最小版本)更改。如果你想使用现有的代码,你必须使用我们编写的import语句和我们使用的Angular 2的版本(2.0.0 beta 9)。

我们正在为Angular 2.0.0 RC4编写更新的博客文章,其中将包括如何使用您说要导入的新@angular符号

2) 对于事件绑定,有一个关于另一个stackoverflow帖子here的很好的解释。