UNPKG

9.46 kBMarkdownView Raw
1# angular2-highcharts
2
3> Highcharts chart components for Angular apps. 👉 [Live Demo](http://plnkr.co/edit/AJwozFWVR7TkQZnt05dN?p=preview)
4
5
6[![build](https://travis-ci.org/gevgeny/angular2-highcharts.svg?branch=master)](https://travis-ci.org/gevgeny/angular2-highcharts)
7[![npm version](https://badge.fury.io/js/angular2-highcharts.svg)](https://badge.fury.io/js/angular2-highcharts)
8[![npm dependencies](https://david-dm.org/gevgeny/angular2-highcharts.svg)](https://david-dm.org/gevgeny/angular2-highcharts)
9[![npm downloads](https://img.shields.io/npm/dm/angular2-highcharts.svg)](https://www.npmjs.com/package/angular2-highcharts)
10
11## Table of Contents
12 - [Setting Up](#setting-up)
13 - [Install angular2-highcharts](#install-angular2-highcharts)
14 - [Setup App @NgModule](#setup-app-ngmodule)
15 - [Usage](#usage)
16 - [Basic Usage](#basic-usage)
17 - [Setup App Module](#setup-app-module)
18 - [Create First Chart Component](#create-first-chart-component)
19 - [Handling Events](#handling-events)
20 - [Chart Events](#chart-events)
21 - [Series Events](#series-events)
22 - [Point Events](#point-events)
23 - [Axis Events](#axis-events)
24 - [Dynamic Interaction with Chart Object](#dynamic-interaction-with-chart-object)
25 - [Highstock](#highstock)
26 - [Highmaps](#highmaps)
27 - [Add Highcharts Modules](#add-highcharts-modules)
28 - [Access to the Highcharts Static API](#access-to-the-highcharts-static-api)
29 - [More Examples](#more-examples)
30 - [FAQ](#faq)
31 - [License](#license)
32
33## Setting Up
34
35### Install angular2-highcharts
36```
37npm install angular2-highcharts --save
38```
39
40### Setup App @NgModule
41```TypeScript
42import { NgModule } from '@angular/core';
43import { BrowserModule } from '@angular/platform-browser';
44import { ChartModule } from 'angular2-highcharts';
45import { App } from './App';
46
47@NgModule({
48 imports: [
49 BrowserModule,
50 ChartModule.forRoot(require('highcharts')
51 ],
52 declarations: [App],
53 bootstrap: [App]
54})
55export class AppModule {}
56```
57
58### For angular-cli and other Webpack environments
59No any additional setup needed
60
61### For SystemJS environment
62You should add appropriate mapping to your `systemjs.config.js`
63
64```js
65...
66map: {
67 ...
68 'angular2-highcharts': 'node_modules/angular2-highcharts',
69 'highcharts': 'node_modules/highcharts',
70}
71...
72packages: {
73 ...
74 highcharts: {
75 main: './highcharts.js',
76 defaultExtension: 'js'
77 },
78 'angular2-highcharts': {
79 main: './index.js',
80 defaultExtension: 'js'
81 }
82}
83```
84
85## Usage
86
87### Basic Usage
88
89#### Create First Chart Component
90Main charts functionality provided by the `chart` component and its `options` property.
91
92```TypeScript
93import { Component } from '@angular/core';
94
95@Component({
96 selector: 'simple-chart-example',
97 template: `
98 <chart [options]="options"></chart>
99 `
100})
101export class App {
102 constructor() {
103 this.options = {
104 title : { text : 'simple chart' },
105 series: [{
106 data: [29.9, 71.5, 106.4, 129.2],
107 }]
108 };
109 }
110 options: Object;
111}
112```
113👉 [Live Demo](http://plnkr.co/edit/IuwjpPB1YQW1T7i4B8SZ?p=preview)
114
115### Handling Events
116Highcharts itself provides bunch of events, and you still can use them with angular2-higcharts via the `options` property of the `chart` component. But it is not an angular way to handle events like this. So that angular2-higcharts provides `EventEmitter<ChartEvent>` wrappers for highcharts events. `ChartEvent` is an angular2-higcharts class which simply wraps original Highcharts events (`chartEvent.originalEvent`) and adds event handler context (`chartEvent.context`) since it differs depending on events.
117
118#### Chart Events
119
120All the events from the [options.chart.events](http://api.highcharts.com/highcharts#chart.events) are available as output properties of the `chart` component.
121
122```HTML
123<chart [options]="options" (selection)="onChartSelection($event)"> </chart>
124```
125```TypeScript
126onChartSelection (e) {
127 this.from = e.originalEvent.xAxis[0].min.toFixed(2);
128 this.to = e.originalEvent.xAxis[0].max.toFixed(2);
129}
130```
131👉 [Live Demo](http://plnkr.co/edit/vdgKVJOymMYhiyqZrPma?p=preview)
132
133#### Series Events
134
135To use series events the same way you need to add the `series` component as a child of your chart. The only purpose of this auxiliary component is to provide access to [options.plotOptions.series.events](http://api.highcharts.com/highcharts#plotOptions.series.events) API
136
137```HTML
138<chart [options]="options">
139 <series (mouseOver)="onSeriesMouseOver($event)">
140 </series>
141</chart>
142<p><b>{{serieName}}</b> is hovered<p>
143```
144```TypeScript
145onSeriesMouseOver (e) {
146 this.serieName = e.context.name;
147}
148```
149👉 [Live Demo](http://plnkr.co/edit/GkaJlk2UJjbTwsPyGXGC?p=preview)
150#### Point Events
151
152Similary you can use the `point` to access to [options.plotOptions.series.point.events](http://api.highcharts.com/highcharts#plotOptions.series.point.events) API.
153```HTML
154<chart [options]="options">
155 <series>
156 <point (select)="onPointSelect($event)"></point>
157 </series>
158</chart>
159<p><b>{{point}}</b> is selected<p>
160```
161👉 [Live Demo](http://plnkr.co/edit/TpKoJ60n4vyIDWxHNUkg?p=preview)
162#### Axis Events
163
164Similary you can use the `xAxis` or `yAxes` to access to [options.xAxis.events](http://api.highcharts.com/highcharts#xAxis.events) or [options.yAxis.events](http://api.highcharts.com/highcharts#yAxis.events) API.
165```HTML
166<chart [options]="options">
167 <xAxis (afterSetExtremes)="onAfterSetExtremesX($event)"></xAxis>
168 <yAxis (afterSetExtremes)="onAfterSetExtremesY($event)"></yAxis>
169</chart>
170<p>{{minX}} - {{maxX}}<p>
171<p>{{minY}} - {{maxY}}<p>
172```
173```TypeScript
174onAfterSetExtremesX (e) {
175 this.minX = e.context.min;
176 this.maxX = e.context.max;
177}
178onAfterSetExtremesY (e) {
179 this.minY = e.context.min;
180 this.maxY = e.context.max;
181}
182```
183👉 [Live Demo](http://plnkr.co/edit/c4ojcIRVOOwq7xmk9kfx?p=preview)
184### Dynamic Interaction with Chart Object
185
186angular2-higcharts provides possibility to interact with native `HighchartsChartObject` chart object.
187
188```TypeScript
189@Component({
190 selector: 'my-app',
191 directives: [CHART_DIRECTIVES],
192 template: `
193 <chart [options]="options"
194 (load)="saveInstance($event.context)">
195 </chart>
196 `
197})
198class AppComponent {
199 constructor() {
200 this.options = {
201 chart: { type: 'spline' },
202 title: { text : 'dynamic data example'}
203 series: [{ data: [2,3,5,8,13] }]
204 };
205 setInterval(() => this.chart.series[0].addPoint(Math.random() * 10), 1000);
206 }
207 chart : Object;
208 options: Object;
209 saveInstance(chartInstance) {
210 this.chart = chartInstance;
211 }
212}
213```
214👉 [Live Demo](http://plnkr.co/edit/OQSFSKisIIWAH0megy4d?p=preview)
215
216### Highstock
217```
218<chart type="StockChart" [options]="options"></chart>
219```
220Also you need to change your `@NgModule` setup.
221
222```diff
223...
224@NgModule({
225 ...
226 imports: [
227 BrowserModule,
228 ChartModule.forRoot(
229- require('highcharts')
230+ require('highcharts/highstock')
231 )
232 ]
233})
234```
235
236👉 [Live Demo](http://plnkr.co/edit/2xSewTZ9b213vA0ALmFq?p=preview)
237
238### Highmaps
239```
240<chart type="Map" [options]="options"></chart>
241```
242Also you need to change your `@NgModule` setup.
243
244```diff
245...
246@NgModule({
247 ...
248 imports: [
249 BrowserModule,
250 ChartModule.forRoot(
251- require('highcharts')
252+ require('highcharts/highmaps')
253 ],
254})
255```
256
257👉 [Live Demo](http://plnkr.co/edit/AmDfKwhRhshFn3CPprkk?p=preview)
258
259
260### Add Highcharts Modules
261Any other modules like highcharts-3d, highcharts-exporintg and etc. can be also added in `@NgModule` after main chart module
262
263```diff
264...
265@NgModule({
266 ...
267 imports: [
268 BrowserModule,
269 ChartModule.forRoot(
270 require('highcharts'),
271+ require('highcharts/highchart-3d'),
272+ require('highcharts/modules/exporting')
273 ],
274})
275```
276
277Check out structure of the `node-modules/highcharts` folder to find necessary module.
278
279👉 [Live Demo](http://plnkr.co/edit/sz6OfccvAetQcBX8KFXy?p=preview)
280
281
282
283### Access to the Highcharts Static API
284
285```diff
286...
287const Highcharts = require('highcharts');
288
289Highcharts.setOptions({
290 colors: ['#50B432']
291});
292
293@NgModule({
294 ...
295 imports: [
296 BrowserModule,
297 ChartModule.forRoot(
298- require('highcharts'),
299+ Highcharts,
300 ],
301})
302```
303
304👉 [Live Demo](http://plnkr.co/edit/uCtPFUExmZFG0diOvbXS?p=preview)
305
306##More Examples
307
308Here are some common charts examples with Webpack integration https://github.com/gevgeny/angular2-highcharts/tree/master/examples/webpack
309
310##FAQ
311
312#### Why don't my series, title, axes and etc redraw after I update initial options ?
313
314Because `angular-highcharts` is just a thin wrapper of the [Highcharts](http:/ /www.highcharts.com/) library and doesn't bind to initial options. I understand that you expect more angular-way behaviour like data binding with appropriate redrawing. But it is barely possible to implement it without redundant complications and performance decrease because almost all options can be dynamic. So my idea was to avoid any additional logic more than just a sugar (like events for series and options). In the other hand Highcharts has great [API](http://api.highcharts.com/highcharts#Chart) for dynamic manipulations with chart and `angular-highcharts` [provides you access](#dynamic-interaction-with-chart-object) to the original chart object.
315
316## License
317MIT @ Eugene Gluhotorenko
318
319