From 2bcd62ebbee329c59cd39d2594dfc96f36bd9307 Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Tue, 6 Aug 2019 11:25:09 +0200 Subject: [PATCH] beautify labels --- benchmarking/src/components/chart.js | 4 ++-- benchmarking/src/components/chartOptions.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/benchmarking/src/components/chart.js b/benchmarking/src/components/chart.js index d1aa514..770bad1 100644 --- a/benchmarking/src/components/chart.js +++ b/benchmarking/src/components/chart.js @@ -2,7 +2,7 @@ import React from 'react' import { Line, Bar } from 'react-chartjs-2' import debounceRender from 'react-debounce-render' -import { datasetTemplates, genOptions } from './chartOptions.js' +import { datasetTemplates, genOptions, beautifyLabels } from './chartOptions.js' export class Chart extends React.Component { render() { @@ -10,7 +10,7 @@ export class Chart extends React.Component { let { tolRange, chart } = formState tolRange = tolRange || [] - let labels = tolRange + let labels = beautifyLabels(tolRange) // let labels = tolRange.map(x => Math.round(x * 1000) / 1000) let datasets = Object.keys(stats).map(name => ({ ...datasetTemplates[name], diff --git a/benchmarking/src/components/chartOptions.js b/benchmarking/src/components/chartOptions.js index 6b0826c..193e052 100644 --- a/benchmarking/src/components/chartOptions.js +++ b/benchmarking/src/components/chartOptions.js @@ -74,3 +74,15 @@ export function genOptions(title, stacked = false, scaleY = 'linear') { } } } + +/** + * Show a maximum precision + * @param {array} labels + */ +export function beautifyLabels(labels, precision = 5) { + let prec = Math.pow(10, precision) + return labels.map(label => { + let rounded = Math.round(label * prec) / prec + return rounded === 0 ? label : rounded + }) +}