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.
- package/LICENSE +21 -0
- package/README.md +1074 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +134 -0
- package/dist/constants/index.d.ts +19 -0
- package/dist/constants/index.js +22 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/dist/services/coords.d.ts +113 -0
- package/dist/services/coords.js +228 -0
- package/dist/services/defaults.d.ts +41 -0
- package/dist/services/defaults.js +119 -0
- package/dist/services/draw.d.ts +83 -0
- package/dist/services/draw.js +183 -0
- package/dist/services/overrides.d.ts +60 -0
- package/dist/services/overrides.js +262 -0
- package/dist/services/plot.d.ts +3 -0
- package/dist/services/plot.js +213 -0
- package/dist/services/settings.d.ts +21 -0
- package/dist/services/settings.js +67 -0
- package/dist/types/index.d.ts +61 -0
- package/dist/types/index.js +2 -0
- package/package.json +69 -0
|
@@ -0,0 +1,262 @@
|
|
|
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
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.getTransformLabel = exports.removeEmptyLines = exports.setFillArea = exports.addThresholds = exports.addBackgroundSymbol = exports.addBorder = exports.addLegend = exports.addYLabel = exports.addXLable = exports.setTitle = void 0;
|
|
29
|
+
var constants_1 = require("../constants");
|
|
30
|
+
var coords_1 = require("./coords");
|
|
31
|
+
var settings_1 = require("./settings");
|
|
32
|
+
var setTitle = function (_a) {
|
|
33
|
+
var title = _a.title, graph = _a.graph, backgroundSymbol = _a.backgroundSymbol, plotWidth = _a.plotWidth, yShift = _a.yShift;
|
|
34
|
+
// add one line for the title
|
|
35
|
+
graph.unshift((0, coords_1.toEmpty)(plotWidth + yShift + 2, backgroundSymbol)); // top
|
|
36
|
+
Array.from(title).forEach(function (letter, index) {
|
|
37
|
+
graph[0][index] = letter;
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
exports.setTitle = setTitle;
|
|
41
|
+
var addXLable = function (_a) {
|
|
42
|
+
var graph = _a.graph, plotWidth = _a.plotWidth, yShift = _a.yShift, backgroundSymbol = _a.backgroundSymbol, xLabel = _a.xLabel;
|
|
43
|
+
var totalWidth = graph[0].length;
|
|
44
|
+
var labelLength = (0, coords_1.toArray)(xLabel).length;
|
|
45
|
+
var startingPosition = Math.round((totalWidth - labelLength) / 2);
|
|
46
|
+
// add one line for the xLabel
|
|
47
|
+
graph.push((0, coords_1.toEmpty)(plotWidth + yShift + 2, backgroundSymbol)); // bottom
|
|
48
|
+
Array.from(xLabel).forEach(function (letter, index) {
|
|
49
|
+
graph[graph.length - 1][startingPosition + index] = letter;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
exports.addXLable = addXLable;
|
|
53
|
+
var addYLabel = function (_a) {
|
|
54
|
+
var graph = _a.graph, backgroundSymbol = _a.backgroundSymbol, yLabel = _a.yLabel;
|
|
55
|
+
var totalHeight = graph.length;
|
|
56
|
+
var labelLength = (0, coords_1.toArray)(yLabel).length;
|
|
57
|
+
var startingPosition = Math.round((totalHeight - labelLength) / 2) - 1;
|
|
58
|
+
var label = Array.from(yLabel);
|
|
59
|
+
// add one line for the xLabel
|
|
60
|
+
graph.forEach(function (line, position) {
|
|
61
|
+
line.unshift(backgroundSymbol); // left
|
|
62
|
+
if (position > startingPosition && label[position - startingPosition - 1]) {
|
|
63
|
+
graph[position][0] = label[position - startingPosition - 1];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
exports.addYLabel = addYLabel;
|
|
68
|
+
var addLegend = function (_a) {
|
|
69
|
+
// calculate legend width as the longest label
|
|
70
|
+
// adds 2 for one space and color indicator
|
|
71
|
+
var graph = _a.graph, legend = _a.legend, backgroundSymbol = _a.backgroundSymbol, color = _a.color, symbols = _a.symbols, fillArea = _a.fillArea;
|
|
72
|
+
var series = Array.isArray(legend.series) ? legend.series : [legend.series];
|
|
73
|
+
var legendWidth = 2 + series.reduce(function (acc, label) { return Math.max(acc, (0, coords_1.toArray)(label).length); }, 0);
|
|
74
|
+
var _loop_1 = function (i) {
|
|
75
|
+
graph.forEach(function (line, lineIndex) {
|
|
76
|
+
if (legend.position === 'left') {
|
|
77
|
+
line.unshift(backgroundSymbol); // left
|
|
78
|
+
series.forEach(function (label, index) {
|
|
79
|
+
if (lineIndex !== index)
|
|
80
|
+
return;
|
|
81
|
+
// get chart symbols for series
|
|
82
|
+
var chartSymbols = (0, settings_1.getChartSymbols)(color, index, symbols === null || symbols === void 0 ? void 0 : symbols.chart, fillArea);
|
|
83
|
+
var reversedLabel = __spreadArray(__spreadArray([
|
|
84
|
+
chartSymbols.area,
|
|
85
|
+
backgroundSymbol
|
|
86
|
+
], __read(Array.from(label)), false), __read(Array(legendWidth - label.length - 2).fill(backgroundSymbol)), false).reverse();
|
|
87
|
+
if (reversedLabel[i]) {
|
|
88
|
+
// eslint-disable-next-line no-param-reassign
|
|
89
|
+
line[0] = reversedLabel[i];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (legend.position === 'right') {
|
|
94
|
+
line.push(backgroundSymbol);
|
|
95
|
+
series.forEach(function (label, index) {
|
|
96
|
+
// get chart symbols for series
|
|
97
|
+
var chartSymbols = (0, settings_1.getChartSymbols)(color, index, symbols === null || symbols === void 0 ? void 0 : symbols.chart, fillArea);
|
|
98
|
+
var newSymbol = __spreadArray(__spreadArray([
|
|
99
|
+
chartSymbols.area,
|
|
100
|
+
backgroundSymbol
|
|
101
|
+
], __read(Array.from(label)), false), __read(Array(legendWidth - label.length - 2).fill(backgroundSymbol)), false);
|
|
102
|
+
if (lineIndex === index) {
|
|
103
|
+
// eslint-disable-next-line no-param-reassign
|
|
104
|
+
line[line.length - 1] = newSymbol[i];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
// prepare space for legend
|
|
111
|
+
// and then place the legend
|
|
112
|
+
for (var i = 0; i < legendWidth; i += 1) {
|
|
113
|
+
_loop_1(i);
|
|
114
|
+
}
|
|
115
|
+
if (legend.position === 'top') {
|
|
116
|
+
series.reverse().forEach(function (label, index) {
|
|
117
|
+
graph.unshift((0, coords_1.toEmpty)(graph[0].length, backgroundSymbol)); // top
|
|
118
|
+
// get chart symbols for series
|
|
119
|
+
var chartSymbols = (0, settings_1.getChartSymbols)(color, index, symbols === null || symbols === void 0 ? void 0 : symbols.chart, fillArea);
|
|
120
|
+
var newSymbol = __spreadArray([chartSymbols.area, backgroundSymbol], __read(Array.from(label)), false);
|
|
121
|
+
graph[index].forEach(function (_, symbolIndex) {
|
|
122
|
+
if (newSymbol[symbolIndex]) {
|
|
123
|
+
// eslint-disable-next-line no-param-reassign
|
|
124
|
+
graph[0][symbolIndex] = newSymbol[symbolIndex];
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (legend.position === 'bottom') {
|
|
130
|
+
series.forEach(function (label, index) {
|
|
131
|
+
graph.push((0, coords_1.toEmpty)(graph[0].length, backgroundSymbol)); // bottom
|
|
132
|
+
// get chart symbols for series
|
|
133
|
+
var chartSymbols = (0, settings_1.getChartSymbols)(color, index, symbols === null || symbols === void 0 ? void 0 : symbols.chart, fillArea);
|
|
134
|
+
var newSymbol = __spreadArray([chartSymbols.area, backgroundSymbol], __read(Array.from(label)), false);
|
|
135
|
+
graph[index].forEach(function (_, symbolIndex) {
|
|
136
|
+
if (newSymbol[symbolIndex]) {
|
|
137
|
+
// eslint-disable-next-line no-param-reassign
|
|
138
|
+
graph[graph.length - 1][symbolIndex] = newSymbol[symbolIndex];
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
exports.addLegend = addLegend;
|
|
145
|
+
var addBorder = function (_a) {
|
|
146
|
+
var graph = _a.graph, borderSymbol = _a.borderSymbol;
|
|
147
|
+
graph.forEach(function (line) {
|
|
148
|
+
line.unshift(borderSymbol); // left
|
|
149
|
+
line.push(borderSymbol); // right
|
|
150
|
+
});
|
|
151
|
+
graph.unshift((0, coords_1.toEmpty)(graph[0].length, borderSymbol)); // top
|
|
152
|
+
graph.push((0, coords_1.toEmpty)(graph[0].length, borderSymbol)); // bottom
|
|
153
|
+
};
|
|
154
|
+
exports.addBorder = addBorder;
|
|
155
|
+
var addBackgroundSymbol = function (_a) {
|
|
156
|
+
var graph = _a.graph, backgroundSymbol = _a.backgroundSymbol, emptySymbol = _a.emptySymbol;
|
|
157
|
+
graph.forEach(function (line) {
|
|
158
|
+
for (var index = 0; index < line.length; index += 1) {
|
|
159
|
+
if (line[index] === emptySymbol) {
|
|
160
|
+
// eslint-disable-next-line
|
|
161
|
+
line[index] = backgroundSymbol;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
exports.addBackgroundSymbol = addBackgroundSymbol;
|
|
170
|
+
var addThresholds = function (_a) {
|
|
171
|
+
var graph = _a.graph, thresholds = _a.thresholds, axis = _a.axis, plotWidth = _a.plotWidth, plotHeight = _a.plotHeight, expansionX = _a.expansionX, expansionY = _a.expansionY;
|
|
172
|
+
var mappedThreshold = thresholds.map(function (_a) {
|
|
173
|
+
var thresholdX = _a.x, thresholdY = _a.y;
|
|
174
|
+
var x = axis.x, y = axis.y;
|
|
175
|
+
if (thresholdX) {
|
|
176
|
+
x = thresholdX;
|
|
177
|
+
}
|
|
178
|
+
if (thresholdY) {
|
|
179
|
+
y = thresholdY;
|
|
180
|
+
}
|
|
181
|
+
return [x, y];
|
|
182
|
+
});
|
|
183
|
+
// add threshold line
|
|
184
|
+
(0, coords_1.getPlotCoords)(mappedThreshold, plotWidth, plotHeight, expansionX, expansionY).forEach(function (_a, thresholdNumber) {
|
|
185
|
+
var _b, _c;
|
|
186
|
+
var _d = __read(_a, 2), x = _d[0], y = _d[1];
|
|
187
|
+
var _e = __read((0, coords_1.toPlot)(plotWidth, plotHeight)(x, y), 2), scaledX = _e[0], scaledY = _e[1];
|
|
188
|
+
// display x threshold only if it's in the graph
|
|
189
|
+
if (((_b = thresholds[thresholdNumber]) === null || _b === void 0 ? void 0 : _b.x) && graph[0][scaledX]) {
|
|
190
|
+
graph.forEach(function (_, index) {
|
|
191
|
+
var _a, _b;
|
|
192
|
+
if (graph[index][scaledX]) {
|
|
193
|
+
graph[index][scaledX] = ((_a = thresholds[thresholdNumber]) === null || _a === void 0 ? void 0 : _a.color)
|
|
194
|
+
? "".concat((0, settings_1.getAnsiColor)(((_b = thresholds[thresholdNumber]) === null || _b === void 0 ? void 0 : _b.color) || 'ansiRed')).concat(constants_1.CHART.ns, "\u001B[0m")
|
|
195
|
+
: constants_1.CHART.ns;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
// display y threshold only if it's in the graph
|
|
200
|
+
if (((_c = thresholds[thresholdNumber]) === null || _c === void 0 ? void 0 : _c.y) && graph[scaledY]) {
|
|
201
|
+
graph[scaledY].forEach(function (_, index) {
|
|
202
|
+
var _a, _b;
|
|
203
|
+
if (graph[scaledY][index]) {
|
|
204
|
+
graph[scaledY][index] = ((_a = thresholds[thresholdNumber]) === null || _a === void 0 ? void 0 : _a.color)
|
|
205
|
+
? "".concat((0, settings_1.getAnsiColor)(((_b = thresholds[thresholdNumber]) === null || _b === void 0 ? void 0 : _b.color) || 'ansiRed')).concat(constants_1.CHART.we, "\u001B[0m")
|
|
206
|
+
: constants_1.CHART.we;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
exports.addThresholds = addThresholds;
|
|
213
|
+
var setFillArea = function (_a) {
|
|
214
|
+
var graph = _a.graph, chartSymbols = _a.chartSymbols;
|
|
215
|
+
graph.forEach(function (xValues, yIndex) {
|
|
216
|
+
xValues.forEach(function (xSymbol, xIndex) {
|
|
217
|
+
var _a;
|
|
218
|
+
if (xSymbol === (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.nse) ||
|
|
219
|
+
xSymbol === (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.wsn) ||
|
|
220
|
+
xSymbol === (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.we) ||
|
|
221
|
+
xSymbol === (chartSymbols === null || chartSymbols === void 0 ? void 0 : chartSymbols.area)) {
|
|
222
|
+
if ((_a = graph[yIndex + 1]) === null || _a === void 0 ? void 0 : _a[xIndex]) {
|
|
223
|
+
graph[yIndex + 1][xIndex] = chartSymbols.area || constants_1.CHART.area;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
exports.setFillArea = setFillArea;
|
|
230
|
+
var removeEmptyLines = function (_a) {
|
|
231
|
+
var graph = _a.graph, backgroundSymbol = _a.backgroundSymbol;
|
|
232
|
+
// clean up empty lines after shift
|
|
233
|
+
// when there are occupied positions and shift is not needed
|
|
234
|
+
// there might be empty lines at the bottom
|
|
235
|
+
var elementsToRemove = [];
|
|
236
|
+
graph.forEach(function (line, position) {
|
|
237
|
+
if (line.every(function (symbol) { return symbol === backgroundSymbol; })) {
|
|
238
|
+
// collect empty line positions and remove them later
|
|
239
|
+
elementsToRemove.push(position);
|
|
240
|
+
}
|
|
241
|
+
// remove empty lines from the beginning
|
|
242
|
+
if (graph.every(function (currentLine) { return currentLine[0] === backgroundSymbol; })) {
|
|
243
|
+
graph.forEach(function (currentLine) { return currentLine.shift(); });
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
// reverse to remove from the end, otherwise positions will be shifted
|
|
247
|
+
elementsToRemove.reverse().forEach(function (position) {
|
|
248
|
+
graph.splice(position, 1);
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
exports.removeEmptyLines = removeEmptyLines;
|
|
252
|
+
var getTransformLabel = function (_a) {
|
|
253
|
+
var formatter = _a.formatter;
|
|
254
|
+
var transformLabel = function (value, helpers) {
|
|
255
|
+
if (formatter) {
|
|
256
|
+
return formatter(value, helpers);
|
|
257
|
+
}
|
|
258
|
+
return (0, settings_1.defaultFormatter)(value, helpers);
|
|
259
|
+
};
|
|
260
|
+
return transformLabel;
|
|
261
|
+
};
|
|
262
|
+
exports.getTransformLabel = getTransformLabel;
|
|
@@ -0,0 +1,213 @@
|
|
|
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;
|
|
@@ -0,0 +1,21 @@
|
|
|
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;
|
|
@@ -0,0 +1,67 @@
|
|
|
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;
|
|
@@ -0,0 +1,61 @@
|
|
|
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;
|