simple-ascii-chart
Version:
Simple ascii chart generator
100 lines (99 loc) • 3.89 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultFormatter = exports.getChartSymbols = exports.getAnsiColor = void 0;
var index_1 = require("../constants/index");
var colorMap = {
ansiBlack: '\u001b[30m',
ansiRed: '\u001b[31m',
ansiGreen: '\u001b[32m',
ansiYellow: '\u001b[33m',
ansiBlue: '\u001b[34m',
ansiMagenta: '\u001b[35m',
ansiCyan: '\u001b[36m',
ansiWhite: '\u001b[37m',
};
/**
* Maps a color name to its corresponding ANSI color code.
* @param {Color} color - The color to map.
* @returns {string} - The ANSI escape code for the specified color.
*/
var getAnsiColor = function (color) { return colorMap[color] || colorMap.ansiWhite; };
exports.getAnsiColor = getAnsiColor;
/**
* Configures and applies colors to chart symbols based on the specified color or series.
* @param {Color | Color[] | ColorGetter | undefined} color - The color setting for the series.
* @param {number} series - The index of the series.
* @param {Partial<typeof CHART> | undefined} chartSymbols - Custom symbols for the chart, if any.
* @param {MultiLine} input - The dataset used in the chart.
* @param {boolean} [fillArea] - If true, fills the area under the plot with the chart's fill symbol.
* @returns {object} - An object with chart symbols applied in the specified color(s).
*/
var getChartSymbols = function (color, series, chartSymbols, input, fillArea) {
var chart = __assign(__assign({}, index_1.CHART), chartSymbols);
// Apply the fill area symbol to all chart symbols if fillArea is true
if (fillArea) {
Object.entries(chart).forEach(function (_a) {
var _b = __read(_a, 1), key = _b[0];
chart[key] = chart.area;
});
}
// Determine the color for the current series and apply it to all chart symbols
if (color) {
var currentColor_1 = 'ansiWhite';
if (Array.isArray(color)) {
currentColor_1 = color[series];
}
else if (typeof color === 'function') {
currentColor_1 = color(series, input);
}
else {
currentColor_1 = color;
}
Object.entries(chart).forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], symbol = _b[1];
chart[key] = "".concat((0, exports.getAnsiColor)(currentColor_1)).concat(symbol, "\u001B[0m");
});
}
return chart;
};
exports.getChartSymbols = getChartSymbols;
/**
* Formats a number for display, converting values >= 1000 to a "k" notation.
* @param {number} value - The value to format.
* @returns {string | number} - The formatted value as a string with "k" for thousands or as a rounded number.
*/
var defaultFormatter = function (value) {
if (Math.abs(value) >= 1000) {
var rounded = value / 1000;
return rounded % 1 === 0 ? "".concat(rounded, "k") : "".concat(rounded.toFixed(3), "k");
}
return Number(value.toFixed(3));
};
exports.defaultFormatter = defaultFormatter;