Browse Source

fix: rounding precision errors in aggregation chart legend

tags/1.5.3
Shivam Mishra 4 years ago
parent
commit
d1123786cb
2 changed files with 13 additions and 2 deletions
  1. +2
    -1
      src/js/charts/AggregationChart.js
  2. +11
    -1
      src/js/utils/helpers.js

+ 2
- 1
src/js/charts/AggregationChart.js View File

@@ -1,6 +1,7 @@
import BaseChart from './BaseChart';
import { truncateString } from '../utils/draw-utils';
import { legendDot } from '../utils/draw';
import { round } from '../utils/helpers';
import { getExtraWidth } from '../utils/constants';

export default class AggregationChart extends BaseChart {
@@ -45,7 +46,7 @@ export default class AggregationChart extends BaseChart {

s.labels = [];
totals.map(d => {
s.sliceTotals.push(d[0]);
s.sliceTotals.push(round(d[0]));
s.labels.push(d[1]);
});



+ 11
- 1
src/js/utils/helpers.js View File

@@ -104,4 +104,14 @@ export function isValidNumber(candidate, nonNegative=false) {
else if (!Number.isFinite(candidate)) return false;
else if (nonNegative && candidate < 0) return false;
else return true;
}
}

/**
* Round a number to the closes precision, max max precision 4
* @param {Number} d Any Number
*/
export function round(d) {
// https://floating-point-gui.de/
// https://www.jacklmoore.com/notes/rounding-in-javascript/
return Number(Math.round(d + 'e4') + 'e-4');
}

Loading…
Cancel
Save