UNPKG

simple-ascii-chart

Version:
184 lines (183 loc) 9.38 kB
"use strict"; 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.drawShift = exports.drawLine = exports.drawCustomLine = exports.drawChart = exports.drawGraph = exports.drawAxis = exports.drawYAxisEnd = exports.drawXAxisEnd = void 0; var constants_1 = require("../constants"); var coords_1 = require("./coords"); var drawXAxisEnd = function (_a) { var hasPlaceToRender = _a.hasPlaceToRender, axisCenter = _a.axisCenter, yPos = _a.yPos, graph = _a.graph, yShift = _a.yShift, i = _a.i, scaledX = _a.scaledX, shift = _a.shift, signShift = _a.signShift, axisSymbols = _a.axisSymbols, pointXShift = _a.pointXShift; var yShiftWhenOccupied = hasPlaceToRender ? -1 : 0; var yShiftWhenHasAxisCenter = axisCenter ? 1 : 0; var graphY = yPos + yShiftWhenOccupied + yShiftWhenHasAxisCenter; var graphX = scaledX + yShift - i + 2 + shift; graph[graphY][graphX] = pointXShift[pointXShift.length - 1 - i]; // Add X tick only for the last value if (pointXShift.length - 1 === i) { graph[yPos + signShift][scaledX + yShift + 2 + shift] = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.x) || constants_1.AXIS.x; } }; exports.drawXAxisEnd = drawXAxisEnd; var drawYAxisEnd = function (_a) { var graph = _a.graph, scaledY = _a.scaledY, yShift = _a.yShift, axis = _a.axis, pointY = _a.pointY, transformLabel = _a.transformLabel, axisSymbols = _a.axisSymbols, expansionX = _a.expansionX, expansionY = _a.expansionY; // make sure position is not taken already if (graph[scaledY + 1][axis.x + yShift + 1] !== (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.y)) { var pointYShift = (0, coords_1.toArray)(transformLabel(pointY, { axis: 'y', xRange: expansionX, yRange: expansionY })); for (var i = 0; i < pointYShift.length; i += 1) { graph[scaledY + 1][axis.x + yShift - i] = pointYShift[pointYShift.length - 1 - i]; } graph[scaledY + 1][axis.x + yShift + 1] = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.y) || constants_1.AXIS.y; } }; exports.drawYAxisEnd = drawYAxisEnd; var drawAxis = function (_a) { var graph = _a.graph, hideXAxis = _a.hideXAxis, hideYAxis = _a.hideYAxis, axisCenter = _a.axisCenter, axisSymbols = _a.axisSymbols, axis = _a.axis; graph.forEach(function (line, index) { line.forEach(function (_, curr) { var lineChar = ''; if (curr === axis.x && !hideYAxis) { if (index === 0) { lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.n) || constants_1.AXIS.n; } else if (index === graph.length - 1 && !axisCenter && !(hideYAxis || hideXAxis)) { lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.nse) || constants_1.AXIS.nse; } else { lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.ns) || constants_1.AXIS.ns; } } else if (index === axis.y && !hideXAxis) { if (curr === line.length - 1) { lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.e) || constants_1.AXIS.e; } else { lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.we) || constants_1.AXIS.we; } } if (lineChar) { // eslint-disable-next-line line[curr] = lineChar; } }); }); }; exports.drawAxis = drawAxis; var drawGraph = function (_a) { var plotWidth = _a.plotWidth, plotHeight = _a.plotHeight, emptySymbol = _a.emptySymbol; var callback = function () { return (0, coords_1.toEmpty)(plotWidth + 2, emptySymbol); }; return Array.from({ length: plotHeight + 2 }, callback); }; exports.drawGraph = drawGraph; var drawChart = function (_a) { var graph = _a.graph; return "\n".concat(graph.map(function (line) { return line.join(''); }).join('\n'), "\n"); }; exports.drawChart = drawChart; var drawCustomLine = function (_a) { var sortedCoords = _a.sortedCoords, scaledX = _a.scaledX, scaledY = _a.scaledY, input = _a.input, index = _a.index, lineFormatter = _a.lineFormatter, graph = _a.graph; // custom line formatter var lineFormatterArgs = { x: sortedCoords[index][0], y: sortedCoords[index][1], plotX: scaledX + 1, plotY: scaledY + 1, index: index, input: input[0], }; var customSymbols = lineFormatter(lineFormatterArgs); if (Array.isArray(customSymbols)) { customSymbols.forEach(function (_a) { var symbolX = _a.x, symbolY = _a.y, symbol = _a.symbol; graph[symbolY][symbolX] = symbol; }); } else { graph[customSymbols.y][customSymbols.x] = customSymbols.symbol; } }; exports.drawCustomLine = drawCustomLine; var drawLine = function (_a) { var index = _a.index, arr = _a.arr, graph = _a.graph, scaledX = _a.scaledX, scaledY = _a.scaledY, plotHeight = _a.plotHeight, emptySymbol = _a.emptySymbol, chartSymbols = _a.chartSymbols; if (index - 1 >= 0) { var _b = __read(arr[index - 1], 2), prevX_1 = _b[0], prevY_1 = _b[1]; var _c = __read(arr[index], 2), currX = _c[0], currY_1 = _c[1]; Array((0, coords_1.distance)(currY_1, prevY_1)) .fill('') .forEach(function (_, steps, array) { if (Math.round(prevY_1) > Math.round(currY_1)) { graph[scaledY + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.nse) || constants_1.CHART.nse; if (steps === array.length - 1) { graph[scaledY - steps][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.wns) || constants_1.CHART.wns; } else { graph[scaledY - steps][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.ns) || constants_1.CHART.ns; } } else { graph[scaledY + steps + 2][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.wsn) || constants_1.CHART.wsn; graph[scaledY + steps + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.ns) || constants_1.CHART.ns; } }); if (Math.round(prevY_1) < Math.round(currY_1)) { graph[scaledY + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.sne) || constants_1.CHART.sne; // The same Y values } else if (Math.round(prevY_1) === Math.round(currY_1)) { // Add line only if space is not occupied already - valid case for small similar Y if (graph[scaledY + 1][scaledX] === emptySymbol) { graph[scaledY + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) || constants_1.CHART.we; } } var distanceX = (0, coords_1.distance)(currX, prevX_1); Array(distanceX ? distanceX - 1 : 0) .fill('') .forEach(function (_, steps) { var thisY = plotHeight - Math.round(prevY_1); graph[thisY][Math.round(prevX_1) + steps + 1] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) || constants_1.CHART.we; }); } // plot the last coordinate if (arr.length - 1 === index) { graph[scaledY + 1][scaledX + 1] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) || constants_1.CHART.we; } }; exports.drawLine = drawLine; var drawShift = function (_a) { var graph = _a.graph, plotWidth = _a.plotWidth, emptySymbol = _a.emptySymbol, scaledCoords = _a.scaledCoords, xShift = _a.xShift, yShift = _a.yShift; // shift graph graph.push((0, coords_1.toEmpty)(plotWidth + 2, emptySymbol)); // bottom // check step var step = plotWidth; scaledCoords.forEach(function (_a, index) { var _b = __read(_a, 1), x = _b[0]; if (scaledCoords[index - 1]) { var current = x - scaledCoords[index - 1][0]; step = current <= step ? current : step; } }); // x coords overlap var hasToBeMoved = step < xShift; if (hasToBeMoved) graph.push((0, coords_1.toEmpty)(plotWidth + 1, emptySymbol)); graph.forEach(function (line) { for (var i = 0; i <= yShift; i += 1) { line.unshift(emptySymbol); // left } }); return { hasToBeMoved: hasToBeMoved }; }; exports.drawShift = drawShift;