simple-ascii-chart-cli 1.0.1 → 1.2.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.
@@ -1,213 +0,0 @@
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.plot = void 0;
20
- var coords_1 = require("./coords");
21
- var settings_1 = require("./settings");
22
- var overrides_1 = require("./overrides");
23
- var defaults_1 = require("./defaults");
24
- var draw_1 = require("./draw");
25
- var plot = function (rawInput, _a) {
26
- var _b = _a === void 0 ? {} : _a, color = _b.color, width = _b.width, height = _b.height, axisCenter = _b.axisCenter, formatter = _b.formatter, lineFormatter = _b.lineFormatter, symbols = _b.symbols, title = _b.title, fillArea = _b.fillArea, hideXAxis = _b.hideXAxis, hideYAxis = _b.hideYAxis, xLabel = _b.xLabel, yLabel = _b.yLabel, legend = _b.legend, thresholds = _b.thresholds;
27
- // Multiline
28
- var input = (0, defaults_1.getInput)({ rawInput: rawInput });
29
- // Empty input, return early
30
- if (input.length === 0) {
31
- return '';
32
- }
33
- var transformLabel = (0, overrides_1.getTransformLabel)({ formatter: formatter });
34
- var scaledCoords = [[0, 0]];
35
- var _c = (0, defaults_1.getChartSize)({
36
- width: width,
37
- height: height,
38
- input: input,
39
- }), minX = _c.minX, plotWidth = _c.plotWidth, plotHeight = _c.plotHeight, expansionX = _c.expansionX, expansionY = _c.expansionY;
40
- var _d = (0, defaults_1.getSymbols)({ symbols: symbols }), axisSymbols = _d.axisSymbols, emptySymbol = _d.emptySymbol, backgroundSymbol = _d.backgroundSymbol, borderSymbol = _d.borderSymbol;
41
- // create placeholder
42
- var graph = (0, draw_1.drawGraph)({ plotWidth: plotWidth, plotHeight: plotHeight, emptySymbol: emptySymbol });
43
- var axis = (0, coords_1.getAxisCenter)(axisCenter, plotWidth, plotHeight, expansionX, expansionY, [
44
- 0,
45
- graph.length - 1,
46
- ]);
47
- // get default chart symbols
48
- input.forEach(function (coords, series) {
49
- // override default chart symbols with colored ones
50
- var chartSymbols = (0, settings_1.getChartSymbols)(color, series, symbols === null || symbols === void 0 ? void 0 : symbols.chart, fillArea);
51
- // sort input by the first value
52
- var sortedCoords = (0, coords_1.toSorted)(coords);
53
- scaledCoords = (0, coords_1.getPlotCoords)(sortedCoords, plotWidth, plotHeight, expansionX, expansionY).map(function (_a, index, arr) {
54
- var _b = __read(_a, 2), x = _b[0], y = _b[1];
55
- var _c = __read((0, coords_1.toPlot)(plotWidth, plotHeight)(x, y), 2), scaledX = _c[0], scaledY = _c[1];
56
- if (!lineFormatter) {
57
- (0, draw_1.drawLine)({ index: index, arr: arr, graph: graph, scaledX: scaledX, scaledY: scaledY, plotHeight: plotHeight, emptySymbol: emptySymbol, chartSymbols: chartSymbols });
58
- // fill empty area under the line if fill area is true
59
- if (fillArea) {
60
- (0, overrides_1.setFillArea)({ graph: graph, chartSymbols: chartSymbols });
61
- }
62
- }
63
- else {
64
- (0, draw_1.drawCustomLine)({ sortedCoords: sortedCoords, scaledX: scaledX, scaledY: scaledY, input: input, index: index, lineFormatter: lineFormatter, graph: graph });
65
- }
66
- return [scaledX, scaledY];
67
- });
68
- });
69
- if (thresholds) {
70
- (0, overrides_1.addThresholds)({
71
- graph: graph,
72
- thresholds: thresholds,
73
- axis: axis,
74
- plotWidth: plotWidth,
75
- plotHeight: plotHeight,
76
- expansionX: expansionX,
77
- expansionY: expansionY,
78
- });
79
- }
80
- // axis
81
- (0, draw_1.drawAxis)({
82
- graph: graph,
83
- hideXAxis: hideXAxis,
84
- hideYAxis: hideYAxis,
85
- axisCenter: axisCenter,
86
- axisSymbols: axisSymbols,
87
- axis: axis,
88
- });
89
- // labels
90
- // takes the longest label that needs to be rendered
91
- // on the Y axis and returns it's length
92
- var _e = (0, defaults_1.getLabelShift)({ input: input, transformLabel: transformLabel, expansionX: expansionX, expansionY: expansionY, minX: minX }), xShift = _e.xShift, yShift = _e.yShift;
93
- // shift graph
94
- var hasToBeMoved = (0, draw_1.drawShift)({
95
- graph: graph,
96
- plotWidth: plotWidth,
97
- emptySymbol: emptySymbol,
98
- scaledCoords: scaledCoords,
99
- xShift: xShift,
100
- yShift: yShift,
101
- }).hasToBeMoved;
102
- // apply background symbol if override
103
- if (backgroundSymbol) {
104
- (0, overrides_1.addBackgroundSymbol)({ graph: graph, backgroundSymbol: backgroundSymbol, emptySymbol: emptySymbol });
105
- }
106
- // shift coords
107
- input.forEach(function (current) {
108
- var coord = (0, coords_1.getPlotCoords)(current, plotWidth, plotHeight, expansionX, expansionY);
109
- current.forEach(function (_a, index) {
110
- var _b = __read(_a, 2), pointX = _b[0], pointY = _b[1];
111
- var _c = __read(coord[index], 2), x = _c[0], y = _c[1];
112
- var _d = __read((0, coords_1.toPlot)(plotWidth, plotHeight)(x, y), 2), scaledX = _d[0], scaledY = _d[1];
113
- if (!hideYAxis) {
114
- (0, draw_1.drawYAxisEnd)({
115
- graph: graph,
116
- scaledY: scaledY,
117
- yShift: yShift,
118
- axis: axis,
119
- pointY: pointY,
120
- transformLabel: transformLabel,
121
- axisSymbols: axisSymbols,
122
- expansionX: expansionX,
123
- expansionY: expansionY,
124
- });
125
- }
126
- if (!hideXAxis) {
127
- var pointXShift = (0, coords_1.toArray)(transformLabel(pointX, { axis: 'x', xRange: expansionX, yRange: expansionY }));
128
- var yPos_1 = graph.length - 1;
129
- var shift_1 = axisCenter ? -1 : 0;
130
- // check if place is taken by previous point
131
- // take into consideration different axis center,
132
- // when axis center is set to true symbol might be '-'
133
- var hasPlaceToRender = pointXShift.every(function (_, i) {
134
- return [emptySymbol, axisSymbols.ns].includes(graph[yPos_1 - 1][scaledX + yShift - i + 2 + shift_1]);
135
- });
136
- for (var i = 0; i < pointXShift.length; i += 1) {
137
- var signShift = -1;
138
- if (hasToBeMoved) {
139
- signShift = -2;
140
- }
141
- var isSymbolOnXAxisOccupied = graph[yPos_1 + signShift][scaledX + yShift + 2 + shift_1] === axisSymbols.x;
142
- // Make sure position is not taken already
143
- if (isSymbolOnXAxisOccupied) {
144
- break;
145
- }
146
- // make sure that shift is applied only when place is taken
147
- if (axisCenter) {
148
- yPos_1 = axis.y + 1;
149
- }
150
- (0, draw_1.drawXAxisEnd)({
151
- hasPlaceToRender: hasPlaceToRender,
152
- axisCenter: axisCenter,
153
- yPos: yPos_1,
154
- graph: graph,
155
- yShift: yShift,
156
- i: i,
157
- scaledX: scaledX,
158
- shift: shift_1,
159
- signShift: signShift,
160
- axisSymbols: axisSymbols,
161
- pointXShift: pointXShift,
162
- });
163
- }
164
- }
165
- });
166
- });
167
- // Remove empty lines
168
- (0, overrides_1.removeEmptyLines)({ graph: graph, backgroundSymbol: backgroundSymbol });
169
- // Adds title above the graph
170
- if (title) {
171
- (0, overrides_1.setTitle)({
172
- title: title,
173
- graph: graph,
174
- backgroundSymbol: backgroundSymbol,
175
- plotWidth: plotWidth,
176
- yShift: yShift,
177
- });
178
- }
179
- // Adds x axis label below the graph
180
- if (xLabel) {
181
- (0, overrides_1.addXLable)({
182
- xLabel: xLabel,
183
- graph: graph,
184
- backgroundSymbol: backgroundSymbol,
185
- plotWidth: plotWidth,
186
- yShift: yShift,
187
- });
188
- }
189
- // Adds x axis label below the graph
190
- if (yLabel) {
191
- (0, overrides_1.addYLabel)({
192
- yLabel: yLabel,
193
- graph: graph,
194
- backgroundSymbol: backgroundSymbol,
195
- });
196
- }
197
- if (legend) {
198
- (0, overrides_1.addLegend)({
199
- graph: graph,
200
- legend: legend,
201
- backgroundSymbol: backgroundSymbol,
202
- color: color,
203
- symbols: symbols,
204
- fillArea: fillArea,
205
- });
206
- }
207
- if (borderSymbol) {
208
- (0, overrides_1.addBorder)({ graph: graph, borderSymbol: borderSymbol });
209
- }
210
- return (0, draw_1.drawChart)({ graph: graph });
211
- };
212
- exports.plot = plot;
213
- exports.default = exports.plot;
@@ -1,21 +0,0 @@
1
- import { CHART } from '../constants/index';
2
- import { Color, Formatter } from '../types/index';
3
- export declare const getAnsiColor: (color: Color) => string;
4
- export declare const getChartSymbols: (color: Color | Color[] | undefined, series: number, chartSymbols: Partial<typeof CHART> | void, fillArea?: boolean) => {
5
- we: string;
6
- wns: string;
7
- ns: string;
8
- nse: string;
9
- wsn: string;
10
- sne: string;
11
- area: string;
12
- } | {
13
- we: string;
14
- wns: string;
15
- ns: string;
16
- nse: string;
17
- wsn: string;
18
- sne: string;
19
- area: string;
20
- };
21
- export declare const defaultFormatter: Formatter;
@@ -1,67 +0,0 @@
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.defaultFormatter = exports.getChartSymbols = exports.getAnsiColor = void 0;
31
- var index_1 = require("../constants/index");
32
- var colorMap = {
33
- ansiBlack: '\u001b[30m',
34
- ansiRed: '\u001b[31m',
35
- ansiGreen: '\u001b[32m',
36
- ansiYellow: '\u001b[33m',
37
- ansiBlue: '\u001b[34m',
38
- ansiMagenta: '\u001b[35m',
39
- ansiCyan: '\u001b[36m',
40
- ansiWhite: '\u001b[37m',
41
- };
42
- var getAnsiColor = function (color) { return colorMap[color] || colorMap.ansiWhite; };
43
- exports.getAnsiColor = getAnsiColor;
44
- var getChartSymbols = function (color, series, chartSymbols, fillArea) {
45
- var chart = __assign(__assign({}, index_1.CHART), chartSymbols);
46
- if (fillArea) {
47
- Object.entries(chart).forEach(function (_a) {
48
- var _b = __read(_a, 1), key = _b[0];
49
- chart[key] = chart.area;
50
- });
51
- }
52
- if (color) {
53
- var currentColor_1 = Array.isArray(color) ? color[series] : color;
54
- Object.entries(chart).forEach(function (_a) {
55
- var _b = __read(_a, 2), key = _b[0], sign = _b[1];
56
- chart[key] = "".concat((0, exports.getAnsiColor)(currentColor_1)).concat(sign, "\u001B[0m");
57
- });
58
- }
59
- return chart;
60
- };
61
- exports.getChartSymbols = getChartSymbols;
62
- var defaultFormatter = function (value) {
63
- if (Math.abs(value) >= 1000)
64
- return "".concat(Number(value.toFixed(3)) / 1000, "k");
65
- return Number(value.toFixed(3));
66
- };
67
- exports.defaultFormatter = defaultFormatter;
@@ -1,61 +0,0 @@
1
- import { AXIS, CHART } from '../constants';
2
- export type Point = [x: number, y: number];
3
- export type SingleLine = Point[];
4
- export type MultiLine = SingleLine[];
5
- export type Coordinates = SingleLine | MultiLine;
6
- export type Color = `ansi${'Red' | 'Green' | 'Black' | 'Yellow' | 'Blue' | 'Magenta' | 'Cyan' | 'White'}`;
7
- export type LineFormatterArgs = {
8
- x: number;
9
- y: number;
10
- plotX: number;
11
- plotY: number;
12
- input: SingleLine;
13
- index: number;
14
- };
15
- export type CustomSymbol = {
16
- x: number;
17
- y: number;
18
- symbol: string;
19
- };
20
- export type FormatterHelpers = {
21
- axis: 'x' | 'y';
22
- xRange: number[];
23
- yRange: number[];
24
- };
25
- export type Symbols = {
26
- axis?: Partial<typeof AXIS>;
27
- chart?: Partial<typeof CHART>;
28
- empty?: string;
29
- background?: string;
30
- border?: string;
31
- };
32
- export type Formatter = (number: number, helpers: FormatterHelpers) => number | string;
33
- export type Legend = {
34
- position?: 'left' | 'right' | 'top' | 'bottom';
35
- series: string | string[];
36
- };
37
- export type Threshold = {
38
- x?: number;
39
- y?: number;
40
- color?: Color;
41
- };
42
- export type Colors = Color | Color[];
43
- export type Graph = string[][];
44
- export type Settings = {
45
- color?: Colors;
46
- width?: number;
47
- height?: number;
48
- hideXAxis?: boolean;
49
- hideYAxis?: boolean;
50
- title?: string;
51
- xLabel?: string;
52
- yLabel?: string;
53
- thresholds?: Threshold[];
54
- fillArea?: boolean;
55
- legend?: Legend;
56
- axisCenter?: Point;
57
- formatter?: Formatter;
58
- lineFormatter?: (args: LineFormatterArgs) => CustomSymbol | CustomSymbol[];
59
- symbols?: Symbols;
60
- };
61
- export type Plot = (coordinates: Coordinates, settings?: Settings) => string;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });