From f342a2a54b8404c57dd613cd9eda57b3195ae3fa Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Tue, 24 Mar 2020 13:18:01 -0400 Subject: [PATCH] refactor to use array of objects to map state data to chart update --- .../folder-chart/folder-chart.component.html | 2 +- .../folder-chart/folder-chart.component.ts | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/app/charts/folder-chart/folder-chart.component.html b/src/app/charts/folder-chart/folder-chart.component.html index 286280c98..dd0c0dcd7 100644 --- a/src/app/charts/folder-chart/folder-chart.component.html +++ b/src/app/charts/folder-chart/folder-chart.component.html @@ -4,7 +4,7 @@
- +
diff --git a/src/app/charts/folder-chart/folder-chart.component.ts b/src/app/charts/folder-chart/folder-chart.component.ts index 8261b1c67..98127336a 100644 --- a/src/app/charts/folder-chart/folder-chart.component.ts +++ b/src/app/charts/folder-chart/folder-chart.component.ts @@ -12,11 +12,11 @@ import { DonutChartComponent } from '../donut-chart/donut-chart.component'; export class FolderChartComponent implements OnInit { @ViewChild(DonutChartComponent) donutChart: DonutChartComponent; chartID: string = 'foldersChart'; - states: Map; + states: { label: string, count: number }[]; elevation: string = cardElevation; constructor(private folderService: FolderService) { - this.states = new Map(); + this.states = []; } ngOnInit(): void { @@ -28,20 +28,29 @@ export class FolderChartComponent implements OnInit { ngAfterViewInit() { this.folderService.getAll().subscribe( folder => { - // TODO: Clear existing data - this.donutChart.data([40]); - // Get StateType and convert to string const stateType: Folder.StateType = Folder.getStateType(folder); const state: string = Folder.stateTypeToString(stateType); - // Instantiate empty count states - if (!this.states.has(state)) { - this.states.set(state, 0); + // Check if state exists + let found: boolean = false; + this.states.forEach(s => { + if (s.label === state) { + s.count = s.count + 1; + found = true; + console.log("increase count", s.count); + } + }); + + if (!found) { + this.states.push({ label: state, count: 1 }); } - const count: number = this.states.get(state) + 1; - this.states.set(state, count); + + this.donutChart.updateData(this.states); + }, + err => console.error('Observer got an error: ' + err), + () => { } ); } -} +} \ No newline at end of file