10 } from '@angular/core';
12 import * as Chart from 'chart.js';
13 import _ from 'lodash';
14 import { PluginServiceGlobalRegistrationAndOptions } from 'ng2-charts';
16 import { ChartTooltip } from '~/app/shared/models/chart-tooltip';
17 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
18 import { DimlessPipe } from '~/app/shared/pipes/dimless.pipe';
19 import styles from '~/styles.scss';
22 selector: 'cd-health-pie',
23 templateUrl: './health-pie.component.html',
24 styleUrls: ['./health-pie.component.scss']
26 export class HealthPieComponent implements OnChanges, OnInit {
27 @ViewChild('chartCanvas', { static: true })
28 chartCanvasRef: ElementRef;
29 @ViewChild('chartTooltip', { static: true })
30 chartTooltipRef: ElementRef;
41 showLabelAsTooltip = false;
43 prepareFn = new EventEmitter();
46 chartType: 'doughnut',
56 styles.chartHealthColorGreen,
57 styles.chartHealthColorYellow,
58 styles.chartHealthColorOrange,
59 styles.chartHealthColorRed,
60 styles.chartHealthColorBlue
66 events: ['click', 'mouseout', 'touchstart'],
81 backgroundColor: styles.chartHealthTootlipBgColor,
89 label: (item: Record<string, any>, data: Record<string, any>) => {
90 let text = data.labels[item.index];
91 if (!text.includes('%')) {
92 text = `${text} (${data.datasets[item.datasetIndex].data[item.index]}%)`;
104 public doughnutChartPlugins: PluginServiceGlobalRegistrationAndOptions[] = [
107 beforeDraw(chart: Chart) {
108 const defaultFontFamily = 'Helvetica Neue, Helvetica, Arial, sans-serif';
109 Chart.defaults.global.defaultFontFamily = defaultFontFamily;
110 const ctx = chart.ctx;
111 if (!chart.options.plugins.center_text || !chart.data.datasets[0].label) {
116 const label = chart.data.datasets[0].label.split('\n');
118 const centerX = (chart.chartArea.left + chart.chartArea.right) / 2;
119 const centerY = (chart.chartArea.top + chart.chartArea.bottom) / 2;
120 ctx.textAlign = 'center';
121 ctx.textBaseline = 'middle';
123 ctx.font = `24px ${defaultFontFamily}`;
124 ctx.fillStyle = styles.chartHealthCenterTextColor;
125 ctx.fillText(label[0], centerX, centerY - 10);
127 if (label.length > 1) {
128 ctx.font = `14px ${defaultFontFamily}`;
129 ctx.fillStyle = styles.chartHealthCenterTextDescriptionColor;
130 ctx.fillText(label[1], centerX, centerY + 10);
137 constructor(private dimlessBinary: DimlessBinaryPipe, private dimless: DimlessPipe) {}
140 const getStyleTop = (tooltip: any, positionY: number) => {
141 return positionY + tooltip.caretY - tooltip.height - 10 + 'px';
144 const getStyleLeft = (tooltip: any, positionX: number) => {
145 return positionX + tooltip.caretX + 'px';
148 const chartTooltip = new ChartTooltip(
150 this.chartTooltipRef,
155 chartTooltip.getBody = (body: any) => {
156 return this.getChartTooltipBody(body);
159 _.merge(this.chartConfig, this.config);
161 this.prepareFn.emit([this.chartConfig, this.data]);
165 this.prepareFn.emit([this.chartConfig, this.data]);
166 this.setChartSliceBorderWidth();
169 private getChartTooltipBody(body: string[]) {
170 const bodySplit = body[0].split(': ');
172 if (this.showLabelAsTooltip) {
176 bodySplit[1] = this.isBytesData
177 ? this.dimlessBinary.transform(bodySplit[1])
178 : this.dimless.transform(bodySplit[1]);
180 return bodySplit.join(': ');
183 private setChartSliceBorderWidth() {
184 let nonZeroValueSlices = 0;
185 _.forEach(this.chartConfig.dataset[0].data, function (slice) {
187 nonZeroValueSlices += 1;
191 this.chartConfig.dataset[0].borderWidth = nonZeroValueSlices > 1 ? 1 : 0;