vmui: limit the number of decimal places to 10 characters (#3258)

* fix: limit the number of decimal places to 10 characters

* fix: add float numbers stabilizer
This commit is contained in:
Yury Molodov 2022-10-26 01:43:13 +02:00 committed by Aliaksandr Valialkin
parent ab880afa50
commit 346d49ae61
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 12 additions and 3 deletions

View file

@ -4,10 +4,11 @@ import LineChart from "../../LineChart/LineChart";
import {AlignedData as uPlotData, Series as uPlotSeries} from "uplot";
import Legend from "../../Legend/Legend";
import {getHideSeries, getLegendItem, getSeriesItem} from "../../../utils/uplot/series";
import {getLimitsYAxis, getTimeSeries} from "../../../utils/uplot/axes";
import {getLimitsYAxis, getMinMaxBuffer, getTimeSeries} from "../../../utils/uplot/axes";
import {LegendItem} from "../../../utils/uplot/types";
import {TimeParams} from "../../../types";
import {AxisRange, YaxisState} from "../../../state/graph/reducer";
import {getAvgFromArray, getMaxFromArray, getMinFromArray} from "../../../utils/math";
export interface GraphViewProps {
data?: MetricResult[];
@ -104,10 +105,16 @@ const GraphView: FC<GraphViewProps> = ({
}
results.push(v);
}
return results;
// stabilize float numbers
const resultAsNumber = results.filter(s => s !== null) as number[];
const avg = Math.abs(getAvgFromArray(resultAsNumber));
const range = getMinMaxBuffer(getMinFromArray(resultAsNumber), getMaxFromArray(resultAsNumber));
const rangeStep = Math.abs(range[1] - range[0]);
return (avg > rangeStep * 1e10) ? results.map(() => avg) : results;
});
timeDataSeries.unshift(timeSeries);
setLimitsYaxis(tempValues);
setDataChart(timeDataSeries as uPlotData);
setSeries(tempSeries);

View file

@ -21,3 +21,5 @@ export const getMinFromArray = (a: number[]) => {
}
return Number.isFinite(min) ? min : null;
};
export const getAvgFromArray = (a: number[]) => a.reduce((a,b) => a+b) / a.length;