Angular 2 and AmCharts

*爱你&永不变心* 提交于 2019-11-29 10:59:51

1. Installation

npm install amcharts/amcharts3-angular2 --save

2. In your HTML file, load the amCharts library using 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>

3. In your app module, import the AmChartsModule module and add it to the imports:

import { AmChartsModule } from "amcharts3-angular2";

@NgModule({
  imports: [
    AmChartsModule
  ]
})
export class AppModule {}

4. Inject the AmChartsService into your app component, create a element with an id, then use the makeChart method 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 '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.

5. If you want to change the chart after the chart has been created, you must make the changes using the updateChart method:

// 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
  });
});

Sample Project: https://github.com/amcharts/amcharts3-angular2

Chad

Thanks to the help of customer support at AmCharts.

A working plunker can be found here.

The key issue was this line of code: Instead of doing this:

AmCharts.ready(function () {
    createStockChart();
});

Instead do this:

if (AmCharts.isReady) {
    createStockChart();

} else {
    AmCharts.ready(function () {
        createStockChart();
    });
}

UPDATE:

Here is the extended answer using Angular Service to get the data for the chart. Angular2 HTTP Providers, get a string from JSON for Amcharts

I'd say your selector is wrong, you use the element tag <chart></chart>, but in the directive declaration you use [chart], which selects an attribute. This will leave you with 2 possible options:

Option 1

@Directive({
   selector: 'chart', //remove the straight brackets to select a tag
   ...
})

Option 2

<div chart>...</div> <!-- now you can use the attribute selector -->

Two things I did. (problem not solved yet.)

  1. This was the final code that I used in template.html

    <div id="chartdiv" chart2 style="width:100%; height:600px;"></div>

  2. The selector stayed the same. selector: '[chart2]'

  3. I also ran npm start to compile the TypeScript. Which I think might have helped.

Hopefully this will help someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!