Is there any way to use highcharts.js with angular2 ?Is there any option available instead of highcharts?
As of October 2018:
angular-highcharts is the new kid on the block and has overtaken angular2-highcharts in popularity.
angular-highcharts is an Angular directive based implementation of highcharts and ATTOW requires Angular 7 (which was released very recently)
I have no affiliation to either library.
PrimeNG charts can be an alternative. http://www.primefaces.org/primeng/
Try this approach without need of 3rd party library.
import {Component} from 'angular2/core';
declare var jQuery:any;
@Component({
selector: 'my-chart',
template: `<div style="width:60%" id="container"></div>`
})
export class AppComponent {
private data = [
{
name: 'USA',
data: [null, null, null, null, null, 6, 11, 32, 110, 235, 369, 640,
1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468, 20434, 24126,
27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662,
26956, 27912, 28999, 28965, 27826, 25579, 25722, 24826, 24605,
24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344, 23586,
22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950,
10871, 10824, 10577, 10527, 10475, 10421, 10358, 10295, 10104]
},
{
name: 'USSR/Russia',
data: [null, null, null, null, null, null, null, null, null, null,
5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471, 3322,
4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478,
15915, 17385, 19055, 21205, 23044, 25393, 27935, 30062, 32049,
33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,
35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000,
21000, 20000, 19000, 18000, 18000, 17000, 16000]
}];
ngAfterViewInit() {
this.renderChart();
}
renderChart(){
jQuery('#container').highcharts({
chart: {
type: 'area'
},
title: {
text: 'US and USSR nuclear stockpiles'
},
subtitle: {
text: 'Source: thebulletin.metapress.com'
},
xAxis: {
allowDecimals: false,
labels: {
formatter: function () {
return this.value;
}
}
},
yAxis: {
title: {
text: 'Nuclear weapon states'
},
labels: {
formatter: function () {
return this.value / 1000 + 'k';
}
}
},
tooltip: {
pointFormat: '{series.name} produced <b>{point.y:,.0f}</b>' +
'<br/>warheads in {point.x}'
},
plotOptions: {
area: {
pointStart: 1940,
marker: {
enabled: false,
symbol: 'circle',
radius: 2,
states: {
hover: {
enabled: true
}
}
}
}
},
series: this.data
});
}
}
And put down this to index.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
Or try this module http://ngmodules.org/modules/angular2-highcharts, if you need to handle on point select event.
I think that you could try ng2-highchart (https://github.com/Bigous/ng2-highcharts).
See this project for a sample of use: https://github.com/Bigous/angular2-seed-ng2-highcharts.
How to set it up in SystemJS configuration: https://github.com/Bigous/angular2-seed-ng2-highcharts/blob/master/src/index.html#L43 and https://github.com/Bigous/angular2-seed-ng2-highcharts/blob/master/tools/config.ts#L108
<script>
System.config({
map: {
'ng2-highchart': 'node_modules/ng2-highchart'
},
(...)
});
</script>
How to configure it within a component: https://github.com/Bigous/angular2-seed-ng2-highcharts/blob/master/src/home/components/home.ts#L10
import {Component, OnInit} from 'angular2/core';
import {Http} from 'angular2/http';
import {Ng2Highcharts, Ng2Highmaps, Ng2Highstocks} from 'ng2-highcharts/ng2-highcharts';
@Component({
selector: 'home',
moduleId: module.id,
templateUrl: './home.html',
styleUrls: ['./home.css'],
directives: [Ng2Highcharts, Ng2Highmaps, Ng2Highstocks]
})
export class HomeCmp implements OnInit {
(...)
}
How to use it within a component template: https://github.com/Bigous/angular2-seed-ng2-highcharts/blob/master/src/home/components/home.html#L9
<div [ng2-highcharts]="chartOptions" class="graph"></div>
I know this question is a bit stale, but is one of the first hits for angular2+highcharts queries... It's pretty simple and straight forward to get highcharts working. Here is a plunker example, https://plnkr.co/edit/8ccBrP?p=preview.
Here is the main logic:
import {
Component,
ElementRef,
AfterViewInit,
OnDestroy,
ViewChild
} from '@angular/core';
const Highcharts = require('highcharts/highcharts.src');
import 'highcharts/adapters/standalone-framework.src';
@Component({
selector: 'my-app',
template: `
<div>
<div #chart></div>
</div>
`
})
export class AppComponent implements AfterViewInit, OnDestroy {
@ViewChild('chart') public chartEl: ElementRef;
private _chart: any;
public ngAfterViewInit() {
let opts: any = {
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
name: 'Tokyo',
data: [
7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2,
26.5, 23.3, 18.3, 13.9, 9.6
]
}]
};
if (this.chartEl && this.chartEl.nativeElement) {
opts.chart = {
type: 'line',
renderTo: this.chartEl.nativeElement
};
this._chart = new Highcharts.Chart(opts);
}
}
public ngOnDestroy() {
this._chart.destroy();
}
}
Added this to package.json:
"angular2-highcharts": "^0.3.3",
"highcharts": "^5.0.0",
Added this on main.module.ts file:
import { ChartModule } from 'angular2-highcharts';
Added this on main.module.ts in @NgModule imports section
imports: [ // import Angular's modules
ChartModule
],
Added this in vendor.ts file:
//Angular2-highcharts
import { Highcharts } from 'angular2-highcharts';
require('highcharts/highcharts-more')(Highcharts);
require('highcharts/modules/solid-gauge')(Highcharts);
Added this on the chart.component.ts file
import { Highcharts } from 'angular2-highcharts';
declare this on inside the chart.component class code:
options: HighchartsOptions;
chartData: any = [];
Add this code in the method that binds the data to the chart:
this.options = {
chart: { type: 'spline' },
title : { text : 'chart' },
xAxis: {
type: 'datetime'
},
series: [{
name: "name",
data: this.chartData
}]
};
Added this on the chart.component.html page:
<div>
<chart [options]="options"></chart>
</div>