simple-ascii-chart-cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.getInput = exports.getLabelShift = exports.getChartSize = exports.getSymbols = void 0;
31
+ var constants_1 = require("../constants");
32
+ var coords_1 = require("./coords");
33
+ var getSymbols = function (_a) {
34
+ var symbols = _a.symbols;
35
+ var axisSymbols = __assign(__assign({}, constants_1.AXIS), symbols === null || symbols === void 0 ? void 0 : symbols.axis);
36
+ var emptySymbol = (symbols === null || symbols === void 0 ? void 0 : symbols.empty) || constants_1.EMPTY;
37
+ var backgroundSymbol = (symbols === null || symbols === void 0 ? void 0 : symbols.background) || emptySymbol;
38
+ var borderSymbol = symbols === null || symbols === void 0 ? void 0 : symbols.border;
39
+ return {
40
+ axisSymbols: axisSymbols,
41
+ emptySymbol: emptySymbol,
42
+ backgroundSymbol: backgroundSymbol,
43
+ borderSymbol: borderSymbol,
44
+ };
45
+ };
46
+ exports.getSymbols = getSymbols;
47
+ var getChartSize = function (_a) {
48
+ var input = _a.input, width = _a.width, height = _a.height;
49
+ var _b = __read((0, coords_1.toArrays)(input), 2), rangeX = _b[0], rangeY = _b[1];
50
+ var minX = (0, coords_1.getMin)(rangeX);
51
+ var maxX = (0, coords_1.getMax)(rangeX);
52
+ var minY = (0, coords_1.getMin)(rangeY);
53
+ var maxY = (0, coords_1.getMax)(rangeY);
54
+ var expansionX = [minX, maxX];
55
+ var expansionY = [minY, maxY];
56
+ // set default size
57
+ var plotWidth = width || rangeX.length;
58
+ var plotHeight = Math.round(height || maxY - minY + 1);
59
+ // for small values without height
60
+ if (!height && plotHeight < 3) {
61
+ plotHeight = rangeY.length;
62
+ }
63
+ return {
64
+ minX: minX,
65
+ plotWidth: plotWidth,
66
+ plotHeight: plotHeight,
67
+ expansionX: expansionX,
68
+ expansionY: expansionY,
69
+ };
70
+ };
71
+ exports.getChartSize = getChartSize;
72
+ var getLabelShift = function (_a) {
73
+ var input = _a.input, transformLabel = _a.transformLabel, expansionX = _a.expansionX, expansionY = _a.expansionY, minX = _a.minX;
74
+ var xShift = 0;
75
+ var longestY = 0;
76
+ input.forEach(function (current) {
77
+ current.forEach(function (_a) {
78
+ var _b = __read(_a, 2), pointX = _b[0], pointY = _b[1];
79
+ xShift = Math.max((0, coords_1.toArray)(transformLabel(pointX, {
80
+ axis: 'x',
81
+ xRange: expansionX,
82
+ yRange: expansionY,
83
+ })).length, xShift);
84
+ longestY = Math.max((0, coords_1.toArray)(transformLabel(pointY, {
85
+ axis: 'y',
86
+ xRange: expansionX,
87
+ yRange: expansionY,
88
+ })).length, longestY);
89
+ });
90
+ });
91
+ // calculate shift for x and y labels, take the longest number
92
+ // but first format it properly because it can be trimmed
93
+ var formattedMinX = transformLabel(minX, {
94
+ axis: 'x',
95
+ xRange: expansionX,
96
+ yRange: expansionY,
97
+ });
98
+ // first x0 label might be longer than the yShift
99
+ // -2 is for the symbol.nse and symbol.x - at least two places for the label
100
+ var x0Shift = (0, coords_1.toArray)(formattedMinX).length - 2;
101
+ var yShift = Math.max(x0Shift, longestY);
102
+ return {
103
+ xShift: xShift,
104
+ yShift: yShift,
105
+ };
106
+ };
107
+ exports.getLabelShift = getLabelShift;
108
+ var getInput = function (_a) {
109
+ var _b;
110
+ var rawInput = _a.rawInput;
111
+ // Multiline
112
+ var input = rawInput;
113
+ // Singleline
114
+ if (typeof ((_b = input[0]) === null || _b === void 0 ? void 0 : _b[0]) === 'number') {
115
+ input = [rawInput];
116
+ }
117
+ return input;
118
+ };
119
+ exports.getInput = getInput;
@@ -0,0 +1,83 @@
1
+ import { CustomSymbol, Formatter, Graph, MultiLine, Point, Symbols } from '../types';
2
+ export declare const drawXAxisEnd: ({ hasPlaceToRender, axisCenter, yPos, graph, yShift, i, scaledX, shift, signShift, axisSymbols, pointXShift, }: {
3
+ hasPlaceToRender: boolean;
4
+ axisCenter?: Point | undefined;
5
+ yPos: number;
6
+ graph: Graph;
7
+ yShift: number;
8
+ i: number;
9
+ scaledX: number;
10
+ shift: number;
11
+ signShift: number;
12
+ axisSymbols: Symbols['axis'];
13
+ pointXShift: string[];
14
+ }) => void;
15
+ export declare const drawYAxisEnd: ({ graph, scaledY, yShift, axis, pointY, transformLabel, axisSymbols, expansionX, expansionY, }: {
16
+ graph: Graph;
17
+ scaledY: number;
18
+ yShift: number;
19
+ axis: {
20
+ x: number;
21
+ y: number;
22
+ };
23
+ pointY: number;
24
+ transformLabel: Formatter;
25
+ axisSymbols: Symbols['axis'];
26
+ expansionX: number[];
27
+ expansionY: number[];
28
+ }) => void;
29
+ export declare const drawAxis: ({ graph, hideXAxis, hideYAxis, axisCenter, axisSymbols, axis, }: {
30
+ graph: Graph;
31
+ axis: {
32
+ x: number;
33
+ y: number;
34
+ };
35
+ hideXAxis?: boolean | undefined;
36
+ axisCenter?: Point | undefined;
37
+ hideYAxis?: boolean | undefined;
38
+ axisSymbols: Symbols['axis'];
39
+ }) => void;
40
+ export declare const drawGraph: ({ plotWidth, plotHeight, emptySymbol, }: {
41
+ plotWidth: number;
42
+ plotHeight: number;
43
+ emptySymbol: string;
44
+ }) => string[][];
45
+ export declare const drawChart: ({ graph }: {
46
+ graph: Graph;
47
+ }) => string;
48
+ export declare const drawCustomLine: ({ sortedCoords, scaledX, scaledY, input, index, lineFormatter, graph, }: {
49
+ sortedCoords: Point[];
50
+ scaledX: number;
51
+ scaledY: number;
52
+ input: MultiLine;
53
+ index: number;
54
+ lineFormatter: (args: {
55
+ x: number;
56
+ y: number;
57
+ plotX: number;
58
+ plotY: number;
59
+ index: number;
60
+ input: Point[];
61
+ }) => CustomSymbol | CustomSymbol[];
62
+ graph: Graph;
63
+ }) => void;
64
+ export declare const drawLine: ({ index, arr, graph, scaledX, scaledY, plotHeight, emptySymbol, chartSymbols, }: {
65
+ index: number;
66
+ arr: Point[];
67
+ graph: Graph;
68
+ scaledX: number;
69
+ scaledY: number;
70
+ plotHeight: number;
71
+ emptySymbol: string;
72
+ chartSymbols: Symbols['chart'];
73
+ }) => void;
74
+ export declare const drawShift: ({ graph, plotWidth, emptySymbol, scaledCoords, xShift, yShift, }: {
75
+ graph: Graph;
76
+ plotWidth: number;
77
+ emptySymbol: string;
78
+ scaledCoords: number[][];
79
+ xShift: number;
80
+ yShift: number;
81
+ }) => {
82
+ hasToBeMoved: boolean;
83
+ };
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.drawShift = exports.drawLine = exports.drawCustomLine = exports.drawChart = exports.drawGraph = exports.drawAxis = exports.drawYAxisEnd = exports.drawXAxisEnd = void 0;
20
+ var constants_1 = require("../constants");
21
+ var coords_1 = require("./coords");
22
+ var drawXAxisEnd = function (_a) {
23
+ 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;
24
+ var yShiftWhenOccupied = hasPlaceToRender ? -1 : 0;
25
+ var yShiftWhenHasAxisCenter = axisCenter ? 1 : 0;
26
+ var graphY = yPos + yShiftWhenOccupied + yShiftWhenHasAxisCenter;
27
+ var graphX = scaledX + yShift - i + 2 + shift;
28
+ graph[graphY][graphX] = pointXShift[pointXShift.length - 1 - i];
29
+ // Add X tick only for the last value
30
+ if (pointXShift.length - 1 === i) {
31
+ graph[yPos + signShift][scaledX + yShift + 2 + shift] = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.x) || constants_1.AXIS.x;
32
+ }
33
+ };
34
+ exports.drawXAxisEnd = drawXAxisEnd;
35
+ var drawYAxisEnd = function (_a) {
36
+ 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;
37
+ // make sure position is not taken already
38
+ if (graph[scaledY + 1][axis.x + yShift + 1] !== (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.y)) {
39
+ var pointYShift = (0, coords_1.toArray)(transformLabel(pointY, { axis: 'y', xRange: expansionX, yRange: expansionY }));
40
+ for (var i = 0; i < pointYShift.length; i += 1) {
41
+ graph[scaledY + 1][axis.x + yShift - i] = pointYShift[pointYShift.length - 1 - i];
42
+ }
43
+ graph[scaledY + 1][axis.x + yShift + 1] = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.y) || constants_1.AXIS.y;
44
+ }
45
+ };
46
+ exports.drawYAxisEnd = drawYAxisEnd;
47
+ var drawAxis = function (_a) {
48
+ var graph = _a.graph, hideXAxis = _a.hideXAxis, hideYAxis = _a.hideYAxis, axisCenter = _a.axisCenter, axisSymbols = _a.axisSymbols, axis = _a.axis;
49
+ graph.forEach(function (line, index) {
50
+ line.forEach(function (_, curr) {
51
+ var lineChar = '';
52
+ if (curr === axis.x && !hideYAxis) {
53
+ if (index === 0) {
54
+ lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.n) || constants_1.AXIS.n;
55
+ }
56
+ else if (index === graph.length - 1 && !axisCenter && !(hideYAxis || hideXAxis)) {
57
+ lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.nse) || constants_1.AXIS.nse;
58
+ }
59
+ else {
60
+ lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.ns) || constants_1.AXIS.ns;
61
+ }
62
+ }
63
+ else if (index === axis.y && !hideXAxis) {
64
+ if (curr === line.length - 1) {
65
+ lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.e) || constants_1.AXIS.e;
66
+ }
67
+ else {
68
+ lineChar = (axisSymbols === null || axisSymbols === void 0 ? void 0 : axisSymbols.we) || constants_1.AXIS.we;
69
+ }
70
+ }
71
+ if (lineChar) {
72
+ // eslint-disable-next-line
73
+ line[curr] = lineChar;
74
+ }
75
+ });
76
+ });
77
+ };
78
+ exports.drawAxis = drawAxis;
79
+ var drawGraph = function (_a) {
80
+ var plotWidth = _a.plotWidth, plotHeight = _a.plotHeight, emptySymbol = _a.emptySymbol;
81
+ var callback = function () { return (0, coords_1.toEmpty)(plotWidth + 2, emptySymbol); };
82
+ return Array.from({ length: plotHeight + 2 }, callback);
83
+ };
84
+ exports.drawGraph = drawGraph;
85
+ var drawChart = function (_a) {
86
+ var graph = _a.graph;
87
+ return "\n".concat(graph.map(function (line) { return line.join(''); }).join('\n'), "\n");
88
+ };
89
+ exports.drawChart = drawChart;
90
+ var drawCustomLine = function (_a) {
91
+ var sortedCoords = _a.sortedCoords, scaledX = _a.scaledX, scaledY = _a.scaledY, input = _a.input, index = _a.index, lineFormatter = _a.lineFormatter, graph = _a.graph;
92
+ // custom line formatter
93
+ var lineFormatterArgs = {
94
+ x: sortedCoords[index][0],
95
+ y: sortedCoords[index][1],
96
+ plotX: scaledX + 1,
97
+ plotY: scaledY + 1,
98
+ index: index,
99
+ input: input[0],
100
+ };
101
+ var customSymbols = lineFormatter(lineFormatterArgs);
102
+ if (Array.isArray(customSymbols)) {
103
+ customSymbols.forEach(function (_a) {
104
+ var symbolX = _a.x, symbolY = _a.y, symbol = _a.symbol;
105
+ graph[symbolY][symbolX] = symbol;
106
+ });
107
+ }
108
+ else {
109
+ graph[customSymbols.y][customSymbols.x] = customSymbols.symbol;
110
+ }
111
+ };
112
+ exports.drawCustomLine = drawCustomLine;
113
+ var drawLine = function (_a) {
114
+ 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;
115
+ if (index - 1 >= 0) {
116
+ var _b = __read(arr[index - 1], 2), prevX_1 = _b[0], prevY_1 = _b[1];
117
+ var _c = __read(arr[index], 2), currX = _c[0], currY_1 = _c[1];
118
+ Array((0, coords_1.distance)(currY_1, prevY_1))
119
+ .fill('')
120
+ .forEach(function (_, steps, array) {
121
+ if (Math.round(prevY_1) > Math.round(currY_1)) {
122
+ graph[scaledY + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.nse) || constants_1.CHART.nse;
123
+ if (steps === array.length - 1) {
124
+ graph[scaledY - steps][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.wns) || constants_1.CHART.wns;
125
+ }
126
+ else {
127
+ graph[scaledY - steps][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.ns) || constants_1.CHART.ns;
128
+ }
129
+ }
130
+ else {
131
+ graph[scaledY + steps + 2][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.wsn) || constants_1.CHART.wsn;
132
+ graph[scaledY + steps + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.ns) || constants_1.CHART.ns;
133
+ }
134
+ });
135
+ if (Math.round(prevY_1) < Math.round(currY_1)) {
136
+ graph[scaledY + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.sne) || constants_1.CHART.sne;
137
+ // The same Y values
138
+ }
139
+ else if (Math.round(prevY_1) === Math.round(currY_1)) {
140
+ // Add line only if space is not occupied already - valid case for small similar Y
141
+ if (graph[scaledY + 1][scaledX] === emptySymbol) {
142
+ graph[scaledY + 1][scaledX] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) || constants_1.CHART.we;
143
+ }
144
+ }
145
+ var distanceX = (0, coords_1.distance)(currX, prevX_1);
146
+ Array(distanceX ? distanceX - 1 : 0)
147
+ .fill('')
148
+ .forEach(function (_, steps) {
149
+ var thisY = plotHeight - Math.round(prevY_1);
150
+ graph[thisY][Math.round(prevX_1) + steps + 1] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) || constants_1.CHART.we;
151
+ });
152
+ }
153
+ // plot the last coordinate
154
+ if (arr.length - 1 === index) {
155
+ graph[scaledY + 1][scaledX + 1] = (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) || constants_1.CHART.we;
156
+ }
157
+ };
158
+ exports.drawLine = drawLine;
159
+ var drawShift = function (_a) {
160
+ var graph = _a.graph, plotWidth = _a.plotWidth, emptySymbol = _a.emptySymbol, scaledCoords = _a.scaledCoords, xShift = _a.xShift, yShift = _a.yShift;
161
+ // shift graph
162
+ graph.push((0, coords_1.toEmpty)(plotWidth + 2, emptySymbol)); // bottom
163
+ // check step
164
+ var step = plotWidth;
165
+ scaledCoords.forEach(function (_a, index) {
166
+ var _b = __read(_a, 1), x = _b[0];
167
+ if (scaledCoords[index - 1]) {
168
+ var current = x - scaledCoords[index - 1][0];
169
+ step = current <= step ? current : step;
170
+ }
171
+ });
172
+ // x coords overlap
173
+ var hasToBeMoved = step < xShift;
174
+ if (hasToBeMoved)
175
+ graph.push((0, coords_1.toEmpty)(plotWidth + 1, emptySymbol));
176
+ graph.forEach(function (line) {
177
+ for (var i = 0; i <= yShift; i += 1) {
178
+ line.unshift(emptySymbol); // left
179
+ }
180
+ });
181
+ return { hasToBeMoved: hasToBeMoved };
182
+ };
183
+ exports.drawShift = drawShift;
@@ -0,0 +1,60 @@
1
+ import { Colors, Formatter, Graph, Legend, Symbols, Threshold } from '../types';
2
+ export declare const setTitle: ({ title, graph, backgroundSymbol, plotWidth, yShift, }: {
3
+ title: string;
4
+ graph: Graph;
5
+ backgroundSymbol: string;
6
+ plotWidth: number;
7
+ yShift: number;
8
+ }) => void;
9
+ export declare const addXLable: ({ graph, plotWidth, yShift, backgroundSymbol, xLabel, }: {
10
+ xLabel: string;
11
+ graph: Graph;
12
+ backgroundSymbol: string;
13
+ plotWidth: number;
14
+ yShift: number;
15
+ }) => void;
16
+ export declare const addYLabel: ({ graph, backgroundSymbol, yLabel, }: {
17
+ graph: Graph;
18
+ backgroundSymbol: string;
19
+ yLabel: string;
20
+ }) => void;
21
+ export declare const addLegend: ({ graph, legend, backgroundSymbol, color, symbols, fillArea, }: {
22
+ graph: Graph;
23
+ legend: Legend;
24
+ backgroundSymbol: string;
25
+ color?: Colors | undefined;
26
+ symbols?: Symbols | undefined;
27
+ fillArea?: boolean | undefined;
28
+ }) => void;
29
+ export declare const addBorder: ({ graph, borderSymbol }: {
30
+ graph: Graph;
31
+ borderSymbol: string;
32
+ }) => void;
33
+ export declare const addBackgroundSymbol: ({ graph, backgroundSymbol, emptySymbol, }: {
34
+ graph: Graph;
35
+ backgroundSymbol: string;
36
+ emptySymbol: string;
37
+ }) => void;
38
+ export declare const addThresholds: ({ graph, thresholds, axis, plotWidth, plotHeight, expansionX, expansionY, }: {
39
+ graph: Graph;
40
+ thresholds: Threshold[];
41
+ axis: {
42
+ x: number;
43
+ y: number;
44
+ };
45
+ plotWidth: number;
46
+ plotHeight: number;
47
+ expansionX: number[];
48
+ expansionY: number[];
49
+ }) => void;
50
+ export declare const setFillArea: ({ graph, chartSymbols, }: {
51
+ graph: Graph;
52
+ chartSymbols: Symbols['chart'];
53
+ }) => void;
54
+ export declare const removeEmptyLines: ({ graph, backgroundSymbol, }: {
55
+ graph: Graph;
56
+ backgroundSymbol: string;
57
+ }) => void;
58
+ export declare const getTransformLabel: ({ formatter }: {
59
+ formatter?: Formatter | undefined;
60
+ }) => Formatter;