|
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the ChartModule for the Chart component
import { ChartModule, StackingColumnSeriesService, CategoryService, LegendService } from '@syncfusion/ej2-ng-charts';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, ChartModule ],
providers: [StackingColumnSeriesService, CategoryService, LegendService],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
|
|
selector: 'app-container',
template:
`<button id="add-series" (click)="addSeries($event)">Click to Add Series</button>
<ej-chart #chart style='display:block;' width='700px' id='chart-container' [title]='title' [primaryXAxis]='primaryXAxis'>
<e-series-collection>
<e-series [dataSource]='product1.data' [type]='product1.type' xName='date' yName='qty' [name]='product1.name' [animation]='animation'></e-series>
<e-series [dataSource]='product2.data' [type]='product2.type' xName='date' yName='qty' [name]='product2.name' [animation]='animation'></e-series>
<e-series [dataSource]='product3.data' [type]='product3.type' xName='date' yName='qty' [name]='product3.name' [animation]='animation'></e-series>
</e-series-collection>
</ej-chart>`, |
|
export class Products {
public name: string;
public data: {date: string, qty: number}[] = [];
public type: ChartSeriesType = 'StackingColumn';
constructor(product: string, data: {date: string, qty: number}[]) {
this.name = product;
this.data = data;
}
// Binding the values to products
export class AppComponent {
public product1: Products;
public product2: Products;
public product3: Products;
constructor() {
this.product1 = new Products('Product A', [
{ date: '2016-01-01', qty: 200}, {date: '2016-02-01', qty: 20 }, {date: '2016-03-01', qty: 40 }]);
this.product2 = new Products('Product B', [
{ date: '2016-01-01', qty: 100}, {date: '2016-02-01', qty: 30 }, {date: '2016-03-01', qty: 30 }]);
this.product3 = new Products('Product C', [
{ date: '2016-01-01', qty: 50}, {date: '2016-02-01', qty: 60 }, {date: '2016-03-01', qty: 90 }]);
}
}
|
|
selector: 'app-container',
template:
`<button id="add-series" (click)="addSeries($event)">Click to Add Series</button>`
// Creating addSeries handler for button click.
export class AppComponent {
// To get chart instance by tag id chart
@ViewChild('chart')
public chart: Chart;
// button click event handler to add dynamic series in chart
public addSeries(e: Event) {
let series: Products = new Products('Product ' + (this.chart.series.length + 1), this.getRandomData(3));
this.chart.addSeries([{dataSource : series.data, name: series.name, type : series.type, xName: 'date',
yName: 'qty', animation: this.animation}]);
}
// To generate random data
public getRandomData(count: number): {date: string, qty: number}[] {
let data: {date: string, qty: number}[] = [];
for (let i = 0; i < count; i++) {
data.push({ date: '2016-0' + (i + 1) + '-01', qty: Math.round(Math.random() * 100)});
}
return data;
}
} |
|
<body>
<app-container>Chart Loading</app-container>
</body> |
|
|