-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
32 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} |