Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 5, 2021
1 parent 03247c4 commit 056e40a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
25 changes: 5 additions & 20 deletions src/report/reporters/histogram/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ export const getContent = function ({
width,
mini,
}) {
const { medianIndex, medianMaxWidth } = getMedianPositions({
median,
min,
max,
width,
})
const rows = getHistogramRows({
histogram,
width,
height,
medianIndex,
medianMaxWidth,
})
const medianIndex = getMedianIndex({ median, min, max, width })
const rows = getHistogramRows({ histogram, width, height, medianIndex })

if (mini) {
return rows
Expand All @@ -31,14 +20,10 @@ export const getContent = function ({
${abscissa}`
}

// Compute positions of the median tick.
// Compute the position of the median tick on the screen.
// When `histogram` has a single item, it is in the first bucket.
// Also compute the maximum width between the median and either the start or end
// Also computes `medianIndex|medianMaxWidth` used for the color gradient.
const getMedianPositions = function ({ median, min, max, width }) {
const getMedianIndex = function ({ median, min, max, width }) {
const medianPercentage =
max.raw === min.raw ? 0 : (median.raw - min.raw) / (max.raw - min.raw)
const medianIndex = Math.round((width - 1) * medianPercentage)
const medianMaxWidth = Math.max(medianIndex, width - 1 - medianIndex)
return { medianIndex, medianMaxWidth }
return Math.round((width - 1) * medianPercentage)
}
6 changes: 3 additions & 3 deletions src/report/reporters/histogram/rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export const getHistogramRows = function ({
width,
height,
medianIndex,
medianMaxWidth,
}) {
const frequencies = getFrequencies(histogram, width)
const columns = getHistogramColumns({
frequencies,
medianIndex,
medianMaxWidth,
width,
height,
})
return Array.from({ length: height }, (_, index) =>
Expand All @@ -31,9 +30,10 @@ export const getHistogramRows = function ({
const getHistogramColumns = function ({
frequencies,
medianIndex,
medianMaxWidth,
width,
height,
}) {
const medianMaxWidth = Math.max(medianIndex, width - 1 - medianIndex)
const maxHeight = getMaxHeight(frequencies, height)
return frequencies.map(
getHistogramColumn.bind(undefined, {
Expand Down

0 comments on commit 056e40a

Please sign in to comment.