1 import { AfterViewInit, Component, Input, OnChanges, OnInit, ViewChild } from '@angular/core';
3 import { CssHelper } from '~/app/shared/classes/css-helper';
4 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
5 import { DimlessBinaryPerSecondPipe } from '~/app/shared/pipes/dimless-binary-per-second.pipe';
6 import { FormatterService } from '~/app/shared/services/formatter.service';
7 import { BaseChartDirective, PluginServiceGlobalRegistrationAndOptions } from 'ng2-charts';
8 import { DimlessPipe } from '~/app/shared/pipes/dimless.pipe';
11 selector: 'cd-dashboard-area-chart',
12 templateUrl: './dashboard-area-chart.component.html',
13 styleUrls: ['./dashboard-area-chart.component.scss']
15 export class DashboardAreaChartComponent implements OnInit, OnChanges, AfterViewInit {
16 @ViewChild(BaseChartDirective) chart: BaseChartDirective;
33 currentDataUnits: string;
35 currentDataUnits2?: string;
36 currentData2?: number;
42 data: [{ x: 0, y: 0 }],
44 pointBackgroundColor: this.cssHelper.propertyValue('chart-color-strong-blue'),
45 backgroundColor: this.cssHelper.propertyValue('chart-color-translucent-blue'),
46 borderColor: this.cssHelper.propertyValue('chart-color-strong-blue')
52 pointBackgroundColor: this.cssHelper.propertyValue('chart-color-orange'),
53 backgroundColor: this.cssHelper.propertyValue('chart-color-yellow'),
54 borderColor: this.cssHelper.propertyValue('chart-color-orange')
61 maintainAspectRatio: false,
73 backgroundColor: this.cssHelper.propertyValue('chart-color-tooltip-background'),
75 title: function (tooltipItem: any): any {
76 return tooltipItem[0].xLabel;
92 tooltipFormat: 'YYYY/MM/DD hh:mm:ss'
104 callback: (value: any) => {
108 return this.fillString(this.convertUnits(value));
117 borderColor: this.cssHelper.propertyValue('chart-color-slight-dark-gray'),
123 public chartAreaBorderPlugin: PluginServiceGlobalRegistrationAndOptions[] = [
125 beforeDraw(chart: Chart) {
126 if (!chart.options.plugins.borderArea) {
131 chartArea: { left, top, right, bottom }
134 ctx.strokeStyle = chart.options.plugins.chartAreaBorder.borderColor;
135 ctx.lineWidth = chart.options.plugins.chartAreaBorder.borderWidth;
136 ctx.setLineDash(chart.options.plugins.chartAreaBorder.borderDash || []);
137 ctx.lineDashOffset = chart.options.plugins.chartAreaBorder.borderDashOffset;
138 ctx.strokeRect(left, top, right - left - 1, bottom);
145 private cssHelper: CssHelper,
146 private dimlessBinary: DimlessBinaryPipe,
147 private dimlessBinaryPerSecond: DimlessBinaryPerSecondPipe,
148 private dimlessPipe: DimlessPipe,
149 private formatter: FormatterService
153 this.currentData = Number(
154 this.chartData.dataset[0].data[this.chartData.dataset[0].data.length - 1].y
157 this.currentData2 = Number(
158 this.chartData.dataset[1].data[this.chartData.dataset[1].data.length - 1].y
163 ngOnChanges(): void {
165 this.setChartTicks();
166 this.chartData.dataset[0].data = this.formatData(this.data);
167 this.chartData.dataset[0].label = this.label;
168 [this.currentData, this.currentDataUnits] = this.convertUnits(
169 this.data[this.data.length - 1][1]
173 this.chartData.dataset[1].data = this.formatData(this.data2);
174 this.chartData.dataset[1].label = this.label2;
175 [this.currentData2, this.currentDataUnits2] = this.convertUnits(
176 this.data2[this.data2.length - 1][1]
181 ngAfterViewInit(): void {
183 this.setChartTicks();
187 private formatData(array: Array<any>): any {
188 let formattedData = {};
189 formattedData = array.map((data: any) => ({
191 y: Number(this.convertUnits(data[1]).replace(/[^\d,.]+/g, ''))
193 return formattedData;
196 private convertUnits(data: any): any {
197 let dataWithUnits: string;
198 if (this.dataUnits === 'bytes') {
199 dataWithUnits = this.dimlessBinary.transform(data);
200 } else if (this.dataUnits === 'bytesPerSecond') {
201 dataWithUnits = this.dimlessBinaryPerSecond.transform(data);
202 } else if (this.dataUnits === 'ms') {
203 dataWithUnits = this.formatter.format_number(data, 1000, ['ms', 's']);
205 dataWithUnits = this.dimlessPipe.transform(data);
207 return dataWithUnits;
210 private fillString(str: string): string {
211 let maxNumberOfChar: number = 8;
212 let numberOfChars: number = str.length;
213 if (str.length < 4) {
214 maxNumberOfChar = 11;
216 for (; numberOfChars < maxNumberOfChar; numberOfChars++) {
217 str = '\u00A0' + str;
219 return str + '\u00A0\u00A0';
222 private setChartTicks() {
223 if (this.chart && this.maxValue) {
224 let [maxValue, maxValueDataUnits] = this.convertUnits(this.maxValue).split(' ');
225 this.chart.chart.options.scales.yAxes[0].ticks.suggestedMax = maxValue;
226 this.chart.chart.options.scales.yAxes[0].ticks.suggestedMin = 0;
227 this.chart.chart.options.scales.yAxes[0].ticks.stepSize = Number((maxValue / 2).toFixed(0));
228 this.chart.chart.options.scales.yAxes[0].ticks.callback = (value: any) => {
232 return this.fillString(`${value} ${maxValueDataUnits}`);
234 this.chart.chart.update();
235 } else if (this.chart && this.data) {
237 maxValueDataUnits = '';
238 let maxValueData = Math.max(...this.data.map((values: any) => values[1]));
240 var maxValueData2 = Math.max(...this.data2.map((values: any) => values[1]));
241 [maxValue, maxValueDataUnits] = this.convertUnits(
242 Math.max(maxValueData, maxValueData2)
245 [maxValue, maxValueDataUnits] = this.convertUnits(Math.max(maxValueData)).split(' ');
248 this.chart.chart.options.scales.yAxes[0].ticks.suggestedMax = maxValue * 1.2;
249 this.chart.chart.options.scales.yAxes[0].ticks.suggestedMin = 0;
250 this.chart.chart.options.scales.yAxes[0].ticks.stepSize = Number(
251 ((maxValue * 1.2) / 2).toFixed(0)
253 this.chart.chart.options.scales.yAxes[0].ticks.callback = (value: any) => {
257 if (!maxValueDataUnits) {
258 return this.fillString(`${value}`);
260 return this.fillString(`${value} ${maxValueDataUnits}`);
262 this.chart.chart.update();