From 7d59dc6c181f176e4b3525e135613d616fc73cfd Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Tue, 24 Mar 2020 13:16:22 -0400 Subject: [PATCH] add updateData and removeAllData methods to donut chart --- .../donut-chart/donut-chart.component.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/app/charts/donut-chart/donut-chart.component.ts b/src/app/charts/donut-chart/donut-chart.component.ts index 5555ea7e8..e75adf556 100644 --- a/src/app/charts/donut-chart/donut-chart.component.ts +++ b/src/app/charts/donut-chart/donut-chart.component.ts @@ -1,5 +1,6 @@ import { Component, Input } from '@angular/core'; import { Chart } from 'chart.js' +import { LabelOptions } from '@angular/material/core'; @Component({ selector: 'app-donut-chart', @@ -23,6 +24,16 @@ export class DonutChartComponent { } } + updateData(data: { label: string, count: number }[]): void { + //Using object destructuring + for (let i = 0; i < data.length; i++) { + let s = data[i]; + this.chart.data.labels[i] = s.label; + this.chart.data.datasets[0].data[i] = s.count; + } + this.chart.update(); + } + addData(data: number): void { // this.chart.data.labels.push(label); this.chart.data.datasets.forEach((dataset) => { @@ -31,6 +42,14 @@ export class DonutChartComponent { this.chart.update(); } + removeAllData(withAnimation: boolean): void { + this.chart.data.labels.pop(); + this.chart.data.datasets.forEach((dataset) => { + dataset.data = []; + }); + this.chart.update(withAnimation); + } + ngAfterViewInit(): void { this.canvas = document.getElementById(this.elementID); this.ctx = this.canvas.getContext('2d');