amcharts3-angular2-fix-error
v1.2.0
Published
Official Angular 2 plugin for amCharts V3, fix ng server error Module build failed: TypeError: Cannot read property 'text' of undefined
Readme
Official Angular plugin for amCharts V3
Installation
npm install amcharts/amcharts3-angular2 --saveHow to use
- In your HTML file, load the amCharts library using
<script>tags:
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>- In your app module, import the
AmChartsModulemodule and add it to theimports:
import { AmChartsModule } from "amcharts3-angular2";
@NgModule({
imports: [
AmChartsModule
]
})
export class AppModule {}- Inject the
AmChartsServiceinto your app component, create a<div>element with anid, then use themakeChartmethod to create the chart:
import { AmChartsService } from "amcharts3-angular2";
@Component({
template: `<div id="chartdiv" [style.width.%]="100" [style.height.px]="500"></div>`
})
export class AppComponent {
private chart: any;
constructor(private AmCharts: AmChartsService) {}
ngOnInit() {
this.chart = this.AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"dataProvider": []
...
});
}
ngOnDestroy() {
this.AmCharts.destroyChart(this.chart);
}
}The first argument to makeChart must be the same as the <div>'s id. The id can be whatever you want, but if you display multiple charts each chart must have a different id
When you are finished with the chart, you must call the destroyChart method. It's good to put this inside the ngOnDestroy method.
- If you want to change the chart after the chart has been created, you must make the changes using the
updateChartmethod:
// This must be called when making any changes to the chart
this.AmCharts.updateChart(this.chart, () => {
// Change whatever properties you want, add event listeners, etc.
this.chart.dataProvider = [];
this.chart.addListener("init", () => {
// Do stuff after the chart is initialized
});
});You can see some examples in the examples directory.
Changelog
1.2.0
- Adding in support for Angular 4
- Deprecating the
<amCharts>element in favor of the newAmChartsService
1.1.0
- Various fixes
- Adding examples
1.0.0
- Initial release
