-
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
2 changed files
with
57 additions
and
55 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 |
---|---|---|
@@ -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) | ||
} |
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