UNPKG

simple-ascii-chart

Version:
278 lines (277 loc) 11.8 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.plot = void 0; var coords_1 = require("./coords"); var settings_1 = require("./settings"); var overrides_1 = require("./overrides"); var defaults_1 = require("./defaults"); var draw_1 = require("./draw"); var plot = function (rawInput, _a) { var _b = _a === void 0 ? {} : _a, color = _b.color, width = _b.width, height = _b.height, yRange = _b.yRange, showTickLabel = _b.showTickLabel, axisCenter = _b.axisCenter, formatter = _b.formatter, lineFormatter = _b.lineFormatter, symbols = _b.symbols, title = _b.title, fillArea = _b.fillArea, horizontalBarChart = _b.horizontalBarChart, barChart = _b.barChart, hideXAxis = _b.hideXAxis, hideYAxis = _b.hideYAxis, xLabel = _b.xLabel, yLabel = _b.yLabel, legend = _b.legend, thresholds = _b.thresholds, debugMode = _b.debugMode; // Multiline var input = (0, defaults_1.getInput)({ rawInput: rawInput }); // Filter out points that are outside of the yRange (if defined) if (yRange) { var _c = __read(yRange, 2), yMin_1 = _c[0], yMax_1 = _c[1]; input = input.map(function (line) { return line.filter(function (_a) { var _b = __read(_a, 2), y = _b[1]; return y >= yMin_1 && y <= yMax_1; }); }); } // Empty input, return early if (input.length === 0) { return ''; } // Proceed with the rest of your function as usual var transformLabel = (0, overrides_1.getTransformLabel)({ formatter: formatter }); var scaledCoords = [[0, 0]]; var _d = (0, defaults_1.getChartSize)({ width: width, height: height, input: input, yRange: yRange, }), minX = _d.minX, minY = _d.minY, plotWidth = _d.plotWidth, plotHeight = _d.plotHeight, expansionX = _d.expansionX, expansionY = _d.expansionY; var _e = (0, defaults_1.getSymbols)({ symbols: symbols }), axisSymbols = _e.axisSymbols, emptySymbol = _e.emptySymbol, backgroundSymbol = _e.backgroundSymbol, borderSymbol = _e.borderSymbol; // create placeholder var graph = (0, draw_1.drawGraph)({ plotWidth: plotWidth, plotHeight: plotHeight, emptySymbol: emptySymbol }); var axis = (0, coords_1.getAxisCenter)(axisCenter, plotWidth, plotHeight, expansionX, expansionY, [ 0, graph.length - 1, ]); // get default chart symbols input.forEach(function (coords, series) { // override default chart symbols with colored ones var chartSymbols = (0, settings_1.getChartSymbols)(color, series, symbols === null || symbols === void 0 ? void 0 : symbols.chart, input, fillArea); // sort input by the first value var sortedCoords = (0, coords_1.toSorted)(coords); scaledCoords = (0, coords_1.getPlotCoords)(sortedCoords, plotWidth, plotHeight, expansionX, expansionY).map(function (_a, index, arr) { var _b = __read(_a, 2), x = _b[0], y = _b[1]; var toPlotCoordinates = (0, coords_1.toPlot)(plotWidth, plotHeight); var _c = __read(toPlotCoordinates(x, y), 2), scaledX = _c[0], scaledY = _c[1]; if (!lineFormatter) { (0, draw_1.drawLine)({ debugMode: debugMode, index: index, arr: arr, graph: graph, scaledX: scaledX, scaledY: scaledY, plotHeight: plotHeight, emptySymbol: emptySymbol, chartSymbols: chartSymbols, horizontalBarChart: horizontalBarChart, axis: axis, axisCenter: axisCenter, barChart: barChart, }); // fill empty area under the line if fill area is true if (fillArea) { (0, overrides_1.setFillArea)({ graph: graph, chartSymbols: chartSymbols, debugMode: debugMode }); } } else { (0, draw_1.drawCustomLine)({ debugMode: debugMode, sortedCoords: sortedCoords, scaledX: scaledX, scaledY: scaledY, input: input, index: index, lineFormatter: lineFormatter, graph: graph, toPlotCoordinates: toPlotCoordinates, expansionX: expansionX, expansionY: expansionY, minY: minY, minX: minX, }); } return [scaledX, scaledY]; }); }); if (thresholds) { (0, overrides_1.addThresholds)({ debugMode: debugMode, graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY, }); } // axis (0, draw_1.drawAxis)({ debugMode: debugMode, graph: graph, hideXAxis: hideXAxis, hideYAxis: hideYAxis, axisCenter: axisCenter, axisSymbols: axisSymbols, axis: axis, }); // labels // takes the longest label that needs to be rendered // on the Y axis and returns it's length var _f = (0, defaults_1.getLabelShift)({ input: input, transformLabel: transformLabel, expansionX: expansionX, expansionY: expansionY, minX: minX }), xShift = _f.xShift, yShift = _f.yShift; // shift graph var hasToBeMoved = (0, draw_1.drawShift)({ graph: graph, plotWidth: plotWidth, emptySymbol: emptySymbol, scaledCoords: scaledCoords, xShift: xShift, yShift: yShift, }).hasToBeMoved; // apply background symbol if override if (backgroundSymbol) { (0, overrides_1.addBackgroundSymbol)({ debugMode: debugMode, graph: graph, backgroundSymbol: backgroundSymbol, emptySymbol: emptySymbol }); } // shift coords input.forEach(function (current) { var coord = (0, coords_1.getPlotCoords)(current, plotWidth, plotHeight, expansionX, expansionY); current.forEach(function (_a, index) { var _b = __read(_a, 2), pointX = _b[0], pointY = _b[1]; var _c = __read(coord[index], 2), x = _c[0], y = _c[1]; var _d = __read((0, coords_1.toPlot)(plotWidth, plotHeight)(x, y), 2), scaledX = _d[0], scaledY = _d[1]; if (!hideYAxis) { (0, draw_1.drawYAxisEnd)({ debugMode: debugMode, showTickLabel: showTickLabel, plotHeight: plotHeight, graph: graph, scaledY: scaledY, axisCenter: axisCenter, yShift: yShift, axis: axis, pointY: pointY, transformLabel: transformLabel, axisSymbols: axisSymbols, expansionX: expansionX, expansionY: expansionY, }); } if (!hideXAxis) { var pointXShift = (0, coords_1.toArray)(transformLabel(pointX, { axis: 'x', xRange: expansionX, yRange: expansionY })); var yPos_1 = graph.length - 1; var shift_1 = axisCenter ? -1 : 0; // check if place is taken by previous point // take into consideration different axis center, // when axis center is set to true symbol might be '-' var hasPlaceToRender = pointXShift.every(function (_, i) { return [emptySymbol, axisSymbols.ns].includes(graph[yPos_1 - 1][scaledX + yShift - i + 2 + shift_1]); }); for (var i = 0; i < pointXShift.length; i += 1) { var signShift = -1; if (hasToBeMoved) { signShift = -2; } var rowIndex = yPos_1 + signShift; var colIndex = scaledX + yShift + 2 + shift_1; // Check if rowIndex is within bounds var rowExists = rowIndex >= 0 && rowIndex < graph.length; // Initialize isSymbolOnXAxisOccupied var isSymbolOnXAxisOccupied = false; if (rowExists) { var row = graph[rowIndex]; // Check if colIndex is within bounds var colExists = colIndex >= 0 && colIndex < row.length; if (colExists) { isSymbolOnXAxisOccupied = row[colIndex] === axisSymbols.x; } } // Make sure position is not taken already if (isSymbolOnXAxisOccupied) { break; } // Apply shift only when place is taken if (axisCenter) { yPos_1 = axis.y + 1; } (0, draw_1.drawXAxisEnd)({ debugMode: debugMode, hasPlaceToRender: hasPlaceToRender, axisCenter: axisCenter, yPos: yPos_1, graph: graph, yShift: yShift, i: i, scaledX: scaledX, shift: shift_1, signShift: signShift, axisSymbols: axisSymbols, pointXShift: pointXShift, }); } } }); }); // Remove empty lines (0, overrides_1.removeEmptyLines)({ graph: graph, backgroundSymbol: backgroundSymbol }); // Adds title above the graph if (title) { (0, overrides_1.setTitle)({ debugMode: debugMode, title: title, graph: graph, backgroundSymbol: backgroundSymbol, plotWidth: plotWidth, yShift: yShift, }); } // Adds x axis label below the graph if (xLabel) { (0, overrides_1.addXLable)({ debugMode: debugMode, xLabel: xLabel, graph: graph, backgroundSymbol: backgroundSymbol, plotWidth: plotWidth, yShift: yShift, }); } // Adds x axis label below the graph if (yLabel) { (0, overrides_1.addYLabel)({ debugMode: debugMode, yLabel: yLabel, graph: graph, backgroundSymbol: backgroundSymbol, }); } if (legend) { (0, overrides_1.addLegend)({ debugMode: debugMode, input: input, graph: graph, legend: legend, backgroundSymbol: backgroundSymbol, color: color, symbols: symbols, fillArea: fillArea, }); } if (borderSymbol) { (0, overrides_1.addBorder)({ graph: graph, borderSymbol: borderSymbol }); } return (0, draw_1.drawChart)({ graph: graph }); }; exports.plot = plot; exports.default = exports.plot;