fix: fixed graph axis and tooltip (#1075)

This commit is contained in:
Pranshu Chittora 2022-05-04 19:30:57 +05:30 committed by GitHub
parent ffae767fab
commit 62f8cddc27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -32,7 +32,7 @@ import { legend } from './Plugin';
import { emptyGraph } from './Plugin/EmptyGraph'; import { emptyGraph } from './Plugin/EmptyGraph';
import { LegendsContainer } from './styles'; import { LegendsContainer } from './styles';
import { useXAxisTimeUnit } from './xAxisConfig'; import { useXAxisTimeUnit } from './xAxisConfig';
import { getYAxisFormattedValue } from './yAxisConfig'; import { getToolTipValue, getYAxisFormattedValue } from './yAxisConfig';
Chart.register( Chart.register(
LineElement, LineElement,
@ -115,7 +115,7 @@ function Graph({
label += ': '; label += ': ';
} }
if (context.parsed.y !== null) { if (context.parsed.y !== null) {
label += getYAxisFormattedValue(context.parsed.y, yAxisUnit); label += getToolTipValue(context.parsed.y.toString(), yAxisUnit);
} }
return label; return label;
}, },
@ -160,7 +160,7 @@ function Graph({
ticks: { ticks: {
// Include a dollar sign in the ticks // Include a dollar sign in the ticks
callback(value) { callback(value) {
return getYAxisFormattedValue(parseFloat(value.toString()), yAxisUnit); return getYAxisFormattedValue(value.toString(), yAxisUnit);
}, },
}, },
}, },

View File

@ -1,12 +1,43 @@
import { formattedValueToString, getValueFormat } from '@grafana/data'; import { formattedValueToString, getValueFormat } from '@grafana/data';
export const getYAxisFormattedValue = ( export const getYAxisFormattedValue = (
value: number, value: string,
format: string, format: string,
): string => { ): string => {
let numberValue: number = parseInt(value, 10);
let decimalPrecision: number | undefined;
try {
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);
}
return formattedValueToString(
getValueFormat(format)(numberValue, decimalPrecision, undefined, undefined),
);
} catch (error) {
console.error(error);
}
return `${numberValue}`;
};
export const getToolTipValue = (value: string, format: string): string => {
try { try {
return formattedValueToString( return formattedValueToString(
getValueFormat(format)(value, 1, undefined, undefined), getValueFormat(format)(parseFloat(value), undefined, undefined, undefined),
); );
} catch (error) { } catch (error) {
console.error(error); console.error(error);

View File

@ -19,7 +19,7 @@ const getChartData = ({ queryData }: GetChartDataProps): ChartData => {
const [first = 0, second = ''] = e || []; const [first = 0, second = ''] = e || [];
return { return {
first: new Date(parseInt(convertIntoEpoc(first * 1000), 10)), // converting in ms first: new Date(parseInt(convertIntoEpoc(first * 1000), 10)), // converting in ms
second: Number(parseFloat(second).toFixed(2)), second: Number(parseFloat(second)),
}; };
}); });