Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 5, 2021
1 parent 610ee33 commit cfe27e9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
56 changes: 56 additions & 0 deletions src/report/reporters/histogram/columns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { graphGradientColor } from '../../utils/colors.js'

import { HISTOGRAM_CHARS } from './characters.js'

// Computes the terminal height of each column for reporting.
export const getHistogramColumns = function ({
frequencies,
medianIndex,
contentWidth,
height,
}) {
const medianMaxWidth = Math.max(medianIndex, contentWidth - 1 - medianIndex)
const maxHeight = getMaxHeight(frequencies, height)
return frequencies.map((frequency, columnIndex) =>
getHistogramColumn({
frequency,
columnIndex,
maxHeight,
medianIndex,
medianMaxWidth,
}),
)
}

// Retrieve max terminal height of columns
const getMaxHeight = function (frequencies, height) {
return height / Math.max(...frequencies)
}

// Retrieve the height of each column.
// `heightLevel` is the integer part (full character) and `charIndex` is the
// fractional part (final character on top of column).
const getHistogramColumn = function ({
frequency,
columnIndex,
maxHeight,
medianIndex,
medianMaxWidth,
}) {
const columnHeight = maxHeight * frequency
const heightLevel = Math.floor(columnHeight)
const charIndex = Math.ceil(
(columnHeight - heightLevel) * (HISTOGRAM_CHARS.length - 1),
)
const color = getColumnColor(columnIndex, medianIndex, medianMaxWidth)
return { heightLevel, charIndex, color }
}

// Computes the column gradient color.
const getColumnColor = function (columnIndex, medianIndex, medianMaxWidth) {
const colorPercentage =
medianMaxWidth === 0
? 0
: Math.abs(medianIndex - columnIndex) / medianMaxWidth
return graphGradientColor(colorPercentage)
}
56 changes: 1 addition & 55 deletions src/report/reporters/histogram/rows.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { graphGradientColor } from '../../utils/colors.js'

import {
HISTOGRAM_CHARS,
EMPTY_HISTOGRAM_CHAR,
FULL_HISTOGRAM_CHAR,
} from './characters.js'
import { getHistogramColumns } from './columns.js'
import { getFrequencies } from './frequencies.js'

// Serialize the main part of the histogram, i.e. rows and columns
Expand Down Expand Up @@ -38,59 +37,6 @@ export const getHistogramRows = function ({
).join('')
}

// Computes the terminal height of each column for reporting.
const getHistogramColumns = function ({
frequencies,
medianIndex,
contentWidth,
height,
}) {
const medianMaxWidth = Math.max(medianIndex, contentWidth - 1 - medianIndex)
const maxHeight = getMaxHeight(frequencies, height)
return frequencies.map((frequency, columnIndex) =>
getHistogramColumn({
frequency,
columnIndex,
maxHeight,
medianIndex,
medianMaxWidth,
}),
)
}

// Retrieve max terminal height of columns
const getMaxHeight = function (frequencies, height) {
return height / Math.max(...frequencies)
}

// Retrieve the height of each column.
// `heightLevel` is the integer part (full character) and `charIndex` is the
// fractional part (final character on top of column).
const getHistogramColumn = function ({
frequency,
columnIndex,
maxHeight,
medianIndex,
medianMaxWidth,
}) {
const columnHeight = maxHeight * frequency
const heightLevel = Math.floor(columnHeight)
const charIndex = Math.ceil(
(columnHeight - heightLevel) * (HISTOGRAM_CHARS.length - 1),
)
const color = getColumnColor(columnIndex, medianIndex, medianMaxWidth)
return { heightLevel, charIndex, color }
}

// Computes the column gradient color.
const getColumnColor = function (columnIndex, medianIndex, medianMaxWidth) {
const colorPercentage =
medianMaxWidth === 0
? 0
: Math.abs(medianIndex - columnIndex) / medianMaxWidth
return graphGradientColor(colorPercentage)
}

// Serialize a single row, i.e. terminal line
const getHistogramRow = function ({
index,
Expand Down

0 comments on commit cfe27e9

Please sign in to comment.