Expected outcome: The chart should update with only the selected projects.
My component:
Code: Select all
import { formatDate } from '@angular/common';
import { AfterViewInit, ChangeDetectorRef, Component } from '@angular/core';
import * as Highcharts from 'highcharts';
import HC_exporting from 'highcharts/modules/exporting';
import { ChartService } from 'src/app/services/chart.service';
import { ChartFiltersInput } from 'src/app/types/chart';
HC_exporting(Highcharts);
Highcharts.setOptions({
lang: {
thousandsSep: ','
}
});
@Component({
selector: 'app-project-purchase-column',
templateUrl: './project-purchase-column.component.html',
styleUrls: ['./project-purchase-column.component.scss']
})
export class ProjectPurchaseColumnComponent implements AfterViewInit {
Highcharts: typeof Highcharts = Highcharts;
chart: Highcharts.Chart | undefined;
updateFlag = false;
chartCallback: Highcharts.ChartCallbackFunction;
chartOptions: Highcharts.Options = {
accessibility: {
enabled: false
},
chart: {
type: 'column',
style: {
fontFamily: 'inherit'
}
},
title: {
text: 'Purchases by Project in a Period',
y: 18
},
xAxis: {
categories: [],
crosshair: true
},
yAxis: {
title: {
useHTML: true,
text: 'Amounts in hundreds'
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: Array.from({ length: 6 }, (_, k) => ({
name: '',
type: 'column',
data: []
})) as Highcharts.SeriesOptionsType[]
};
constructor(private _chartService: ChartService, private _cdr: ChangeDetectorRef) {
const self = this;
this.chartCallback = chart => {
// saving chart reference
self.chart = chart;
};
}
ngAfterViewInit(): void {
this.updateChart();
}
updateChart(filters?: ChartFiltersInput) {
const chart = this.chart;
let end = filters?.end ?? new Date();
let start = filters?.start ?? new Date().setMonth(new Date().getMonth() - 12);
const params = {
"project_id[]": filters?.project_id ?? [],
start: formatDate(start, 'YYYY-MM-dd', 'en-US'),
end: formatDate(end, 'YYYY-MM-dd', 'en-US')
};
if (chart) {
chart.showLoading();
this._chartService.getColumnChartData('PURCHASE_BY_PROJECT', params).subscribe(result => {
if (result && result.data.length) {
this.updateFlag = true;
const data = result.data.map(d => ({
...d,
type: 'column'
})) as Highcharts.SeriesOptionsType[];
this.chartOptions.xAxis = {
categories: result.x,
crosshair: true
};
this.chartOptions.series = data;
console.log(this.chartOptions.series);
chart.update(this.chartOptions, true);
chart.hideLoading();
this.updateFlag = false;
this._cdr.detectChanges();
}
});
}
}
}
Code: Select all
[
{
"name": "A",
"data": [
0,
0,
0,
0,
0,
294998.28,
511051.22,
206935.2,
443584.6,
244004.96,
838440.32,
430719.82,
303089.88
],
"type": "column"
},
{
"name": "B",
"data": [
0,
0,
0,
0,
0,
221240,
175234.9,
373981.18,
293857.75,
5202.94,
152317.53,
44295.88,
62250
],
"type": "column"
},
{
"name": "C",
"data": [
0,
0,
0,
0,
0,
32190,
183497,
93295,
850263.37,
174252.77,
292558,
283369.36,
157581.8
],
"type": "column"
},
{
"name": "D",
"data": [
0,
0,
0,
0,
0,
0,
13500,
0,
0,
0,
9398,
0,
0
],
"type": "column"
},
{
"name": "E",
"data": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"type": "column"
},
{
"name": "F",
"data": [
0,
0,
0,
0,
0,
0,
27020,
0,
427180,
0,
6250,
0,
0
],
"type": "column"
}
]
Code: Select all
[
{
"name": "A",
"data": [
0,
0,
0,
0,
0,
294998.28,
511051.22,
206935.2,
443584.6,
244004.96,
838440.32,
430719.82,
303089.88
],
"type": "column"
},]