2022-03-15 15:18:33 +05:30
|
|
|
import { formattedValueToString, getValueFormat } from '@grafana/data';
|
|
|
|
|
|
|
|
|
|
export const getYAxisFormattedValue = (
|
2022-05-04 19:30:57 +05:30
|
|
|
value: string,
|
2022-03-15 15:18:33 +05:30
|
|
|
format: string,
|
|
|
|
|
): string => {
|
2022-05-04 19:30:57 +05:30
|
|
|
let numberValue: number = parseInt(value, 10);
|
|
|
|
|
let decimalPrecision: number | undefined;
|
2022-03-15 15:18:33 +05:30
|
|
|
try {
|
2022-05-04 19:30:57 +05:30
|
|
|
const decimalSplitted = value.split('.');
|
|
|
|
|
if (decimalSplitted.length === 1) {
|
|
|
|
|
decimalPrecision = 0;
|
|
|
|
|
} else {
|
|
|
|
|
const decimalDigits = decimalSplitted[1].split('');
|
|
|
|
|
decimalPrecision = decimalDigits.length;
|
|
|
|
|
let nonZeroCtr = 0;
|
|
|
|
|
for (let idx = 0; idx < decimalDigits.length; idx += 1) {
|
|
|
|
|
if (decimalDigits[idx] !== '0') {
|
|
|
|
|
nonZeroCtr += 1;
|
|
|
|
|
if (nonZeroCtr >= 2) {
|
|
|
|
|
decimalPrecision = idx + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
numberValue = parseFloat(value);
|
|
|
|
|
}
|
2022-03-15 15:18:33 +05:30
|
|
|
return formattedValueToString(
|
2022-05-04 19:30:57 +05:30
|
|
|
getValueFormat(format)(numberValue, decimalPrecision, undefined, undefined),
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
return `${numberValue}`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getToolTipValue = (value: string, format: string): string => {
|
|
|
|
|
try {
|
|
|
|
|
return formattedValueToString(
|
|
|
|
|
getValueFormat(format)(parseFloat(value), undefined, undefined, undefined),
|
2022-03-15 15:18:33 +05:30
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
return `${value}`;
|
|
|
|
|
};
|