beautify labels

This commit is contained in:
Alfred Melch 2019-08-06 11:25:09 +02:00
parent 85d345dd10
commit 2bcd62ebbe
2 changed files with 14 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import React from 'react'
import { Line, Bar } from 'react-chartjs-2' import { Line, Bar } from 'react-chartjs-2'
import debounceRender from 'react-debounce-render' 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 { export class Chart extends React.Component {
render() { render() {
@ -10,7 +10,7 @@ export class Chart extends React.Component {
let { tolRange, chart } = formState let { tolRange, chart } = formState
tolRange = tolRange || [] tolRange = tolRange || []
let labels = tolRange let labels = beautifyLabels(tolRange)
// let labels = tolRange.map(x => Math.round(x * 1000) / 1000) // let labels = tolRange.map(x => Math.round(x * 1000) / 1000)
let datasets = Object.keys(stats).map(name => ({ let datasets = Object.keys(stats).map(name => ({
...datasetTemplates[name], ...datasetTemplates[name],

View File

@ -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
})
}