10 } from '@angular/core';
12 import * as Chart from 'chart.js';
13 import * as _ from 'lodash';
15 import { ChartTooltip } from '../../../shared/models/chart-tooltip';
16 import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
17 import { DimlessPipe } from '../../../shared/pipes/dimless.pipe';
18 import { HealthPieColor } from './health-pie-color.enum';
21 selector: 'cd-health-pie',
22 templateUrl: './health-pie.component.html',
23 styleUrls: ['./health-pie.component.scss']
25 export class HealthPieComponent implements OnChanges, OnInit {
26 @ViewChild('chartCanvas')
27 chartCanvasRef: ElementRef;
28 @ViewChild('chartTooltip')
29 chartTooltipRef: ElementRef;
40 showLabelAsTooltip = false;
42 prepareFn = new EventEmitter();
56 labels: { usePointStyle: true },
57 onClick: (event, legendItem) => {
58 this.onLegendClick(event, legendItem);
61 animation: { duration: 0 },
70 private hiddenSlices = [];
72 constructor(private dimlessBinary: DimlessBinaryPipe, private dimless: DimlessPipe) {}
75 // An extension to Chart.js to enable rendering some
76 // text in the middle of a doughnut
77 Chart.pluginService.register({
78 beforeDraw: function(chart) {
79 if (!chart.options.center_text) {
83 const width = chart.chart.width,
84 height = chart.chart.height,
85 ctx = chart.chart.ctx;
88 const fontSize = (height / 114).toFixed(2);
89 ctx.font = fontSize + 'em sans-serif';
90 ctx.textBaseline = 'middle';
92 const text = chart.options.center_text,
93 textX = Math.round((width - ctx.measureText(text).width) / 2),
96 ctx.fillText(text, textX, textY);
101 const getStyleTop = (tooltip, positionY) => {
102 return positionY + tooltip.caretY - tooltip.height - 10 + 'px';
105 const getStyleLeft = (tooltip, positionX) => {
106 return positionX + tooltip.caretX + 'px';
109 const chartTooltip = new ChartTooltip(
111 this.chartTooltipRef,
116 const getBody = (body) => {
117 return this.getChartTooltipBody(body);
120 chartTooltip.getBody = getBody;
122 this.chartConfig.options.tooltips.custom = (tooltip) => {
123 chartTooltip.customTooltips(tooltip);
126 this.chartConfig.colors = [
129 HealthPieColor.DEFAULT_RED,
130 HealthPieColor.DEFAULT_BLUE,
131 HealthPieColor.DEFAULT_ORANGE,
132 HealthPieColor.DEFAULT_GREEN,
133 HealthPieColor.DEFAULT_MAGENTA
138 _.merge(this.chartConfig, this.config);
140 this.prepareFn.emit([this.chartConfig, this.data]);
144 this.prepareFn.emit([this.chartConfig, this.data]);
146 this.setChartSliceBorderWidth();
149 private getChartTooltipBody(body) {
150 const bodySplit = body[0].split(': ');
152 if (this.showLabelAsTooltip) {
156 bodySplit[1] = this.isBytesData
157 ? this.dimlessBinary.transform(bodySplit[1])
158 : this.dimless.transform(bodySplit[1]);
160 return bodySplit.join(': ');
163 private setChartSliceBorderWidth() {
164 let nonZeroValueSlices = 0;
165 _.forEach(this.chartConfig.dataset[0].data, function(slice) {
167 nonZeroValueSlices += 1;
171 this.chartConfig.dataset[0].borderWidth = nonZeroValueSlices > 1 ? 1 : 0;
174 private onLegendClick(event, legendItem) {
175 event.stopPropagation();
176 this.hiddenSlices[legendItem.index] = !legendItem.hidden;
180 private hideSlices() {
181 _.forEach(this.chartConfig.dataset[0].data, (_slice, sliceIndex) => {
182 if (this.hiddenSlices[sliceIndex]) {
183 this.chartConfig.dataset[0].data[sliceIndex] = undefined;