Skip to content

Commit

Permalink
Fix label centering
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 5, 2021
1 parent 58cb670 commit 32d328b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/report/reporters/boxplot/labels.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { centerString } from '../../utils/center.js'

// Retrieve the median label line shown below the box plot
export const getLabels = function ({
positions: { median },
titlesWidth,
minBlockWidth,
contentWidth,
}) {
const leftShift = Math.max(Math.floor((median.pretty.length - 1) / 2), 0)
const shiftedIndex = median.index - leftShift
const maxContentIndex = contentWidth - median.pretty.length
const contentIndex = Math.min(Math.max(shiftedIndex, 0), maxContentIndex)
const labelIndex = contentIndex + titlesWidth + minBlockWidth
const labelLeft = ' '.repeat(labelIndex)
return `${labelLeft}${median.prettyColor}\n`
const centeredMedian = centerString(
median.prettyColor,
median.index,
contentWidth,
)
const initialSpace = ' '.repeat(titlesWidth + minBlockWidth)
return `${initialSpace}${centeredMedian}\n`
}
15 changes: 8 additions & 7 deletions src/report/reporters/histogram/abscissa.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { centerString } from '../../utils/center.js'
import { separatorColor } from '../../utils/colors.js'

import {
Expand Down Expand Up @@ -80,11 +81,11 @@ const getLabelLine = function ({
median,
medianIndex,
}) {
const leftShift = Math.max(Math.floor((median.pretty.length - 1) / 2), 0)
const shiftedIndex = medianIndex - leftShift
const maxContentIndex = contentWidth - median.pretty.length
const contentIndex = Math.min(Math.max(shiftedIndex, 0), maxContentIndex)
const labelIndex = contentIndex + titlesWidth + minBlockWidth
const labelLeft = ' '.repeat(labelIndex)
return `${labelLeft}${median.prettyColor}\n`
const centeredMedian = centerString(
median.prettyColor,
medianIndex,
contentWidth,
)
const initialSpace = ' '.repeat(titlesWidth + minBlockWidth)
return `${initialSpace}${centeredMedian}\n`
}
15 changes: 15 additions & 0 deletions src/report/utils/center.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import stringWidth from 'string-width'

// Center a string at a specific `index` inside a container of a given `width`
// Ensures the string is usually centered around `index` except when at the
// start or end of the container.
export const centerString = function (string, index, width) {
const stringLength = stringWidth(string)
const leftShift = Math.max(Math.floor((stringLength - 1) / 2), 0)
const shiftedIndex = index - leftShift
const maxIndex = width - stringLength
const finalIndex = Math.min(Math.max(shiftedIndex, 0), maxIndex)
const leftSpace = ' '.repeat(finalIndex)
const rightSpace = ' '.repeat(width - stringLength - finalIndex)
return `${leftSpace}${string}${rightSpace}`
}

0 comments on commit 32d328b

Please sign in to comment.