JSCharting is a JavaScript chart library for visualizing your data, providing resolution independent results across all devices and platorms. Every JSCharting license includes the full suite of 150+ advanced chart types, interactive stock charts and JSMapping at no additional charge.
This set of samples demonstrate how to use JSCharting with the Angular framework. Samples are located in the src/app/samples
folder.
Install the necessary packages including JSCharting.
npm install
Run the webpack dev server: http://localhost:4200/
npm run start
Or build the dashboard manually.
npm run build
Or.
npm run build-prod
- In your component import Chart and create an instance.
import {Chart} from 'jscharting';
this.chart = new Chart({});
- In your app module, import the
JSChartingModule
module and add it to theimports
:
import {JSChartingModule} from './jscharting/jscharting.module';
@NgModule({
imports: [
JSChartingModule
]
})
export class AppModule {}
- Inject the
JSChartingService
into your app component, create a '' element with referencechartTargetElement: ElementRef
.
import {AfterViewInit, Component, ElementRef, OnDestroy, ViewChild} from '@angular/core';
import {Chart} from './jscharting/jscharting';
import {JSChartingService} from './shared/jscharting.service';
@Component({
template: '<div><div #chartTargetElement class="chart-container"></div></div>'
})
export class AppComponent implements AfterViewInit, OnDestroy {
@ViewChild('chartTargetElement') chartTargetElement: ElementRef;
private chart: Chart;
constructor(private chartService: JSChartingService) {
}
ngAfterViewInit(): void {
this.chart = this.chartService.chart({
targetElement: this.chartTargetElement.nativeElement
});
}
ngOnDestroy(): void {
if (this.chart) {
this.chart.destroy();
}
}
Make sure you destroy chart in ngOnDestroy
method.
- Rather than using
JSChartingService
you can instead use theappJSCharting
directive:
import {Component, OnInit} from '@angular/core';
import {JSCChartConfig} from './jscharting/jscharting';
@Component({
template: '<div><div appJSCharting [options]="chartOptions" class="chart-container"></div></div>'
})
export class AppComponent implements OnInit {
public chartOptions = {
...
};
ngOnInit(): void {
// You can update config.
this.chartOptions = {};
}
}
It is easer to use appJSCharting
directive than service or create chart directly.
Please see full samples in the src/app/samples
folder.