-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.component.ts
More file actions
60 lines (51 loc) · 1.26 KB
/
chart.component.ts
File metadata and controls
60 lines (51 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// chart.component.ts
import { Component, Input, OnInit } from '@angular/core';
import Chart from 'chart.js/auto';
@Component({
selector: 'app-chart',
templateUrl: './chart.component.html',
styleUrls: ['./chart.component.scss']
})
export class ChartComponent implements OnInit {
@Input() data: { label: string, value: number }[] = [];
@Input() type: 'bar' | 'line' | 'pie' = 'bar';
chart: Chart;
constructor() { }
ngOnInit(): void {
const canvas = document.getElementById('language-chart') as HTMLCanvasElement;
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
this.chart = new Chart(ctx, {
type: this.type,
data: {
labels: this.data.map(item => item.label),
datasets: [{
label: '',
data: this.data.map(item => item.value),
backgroundColor: ['#ffcc00', '#ff5733', '#c70039', '#900c3f', '#581845']
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
ngOnChanges(): void {
if (this.chart) {
this.chart.destroy();
}
this.ngOnInit();
}
ngOnDestroy(): void {
if (this.chart) {
this.chart.destroy();
}
}
}