simple-ascii-chart
Version:
Simple ascii chart generator
393 lines (392 loc) • 8.91 kB
JavaScript
"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 });
var plot_1 = require("./services/plot");
var examples = [
[
[
[],
[],
],
{ title: 'simple example' },
],
[
[
[],
[],
[],
[],
],
{ title: 'bar chart', width: 10, barChart: true, height: 10 },
],
[
[
[-1, 2],
[],
[],
[],
],
{ title: 'horizontal bar chart', width: 20, horizontalBarChart: true, height: 10 },
],
[
[
[],
[],
[],
[],
],
{ title: 'area', width: 20, fillArea: true, height: 10 },
],
[
[
[],
[],
[],
[],
],
{ title: 'labels', width: 20, xLabel: 'x', yLabel: 'y', height: 10 },
],
[
[
[
[],
[],
[],
[],
],
[
[],
[],
[],
[],
],
],
{
title: 'legend',
width: 20,
legend: { position: 'bottom', series: ['first', 'second'] },
xLabel: 'x',
yLabel: 'y',
height: 10,
},
],
[
[
[
[],
[],
[],
[],
],
[
[],
[],
[],
[],
],
[
[],
[],
[],
[],
],
[
[],
[],
[],
[],
[],
],
],
{
title: 'multiline',
width: 20,
height: 10,
},
],
[
[
[],
[],
[],
[],
],
{ title: 'yRange', width: 20, height: 10, yRange: [1, 3] },
],
[
[
[],
[],
[],
[],
],
{ title: 'yRange', width: 20, height: 10, yRange: [0, 5] },
],
[
[
[],
[],
[],
[],
[],
[],
],
{ title: 'showTickLabel', width: 20, height: 10, showTickLabel: true },
],
[
[
[],
[],
[],
[],
],
{ title: 'hideXAxis', width: 20, height: 10, hideXAxis: true },
],
[
[
[],
[],
[],
[],
],
{ title: 'hideYAxis', width: 20, height: 10, hideYAxis: true },
],
[
[
[-1, 2],
[],
[],
[],
[],
],
{ title: 'axisCenter', width: 20, height: 10, axisCenter: [0, 0], showTickLabel: true },
],
[
[
[-1, 2],
[],
[],
[],
[],
],
{
title: 'lineFormatter',
width: 20,
height: 10,
lineFormatter: function (props) {
var output = [{ x: props.plotX, y: props.plotY, symbol: '█' }];
var _a = __read(props.toPlotCoordinates(props.minX, props.minY), 1), minX = _a[0];
var i = minX;
while (i <= props.plotX) {
output.push({ x: i, y: props.plotY, symbol: '█' });
i += 1;
}
return output;
},
},
],
[
[
[-1, 2],
[],
[],
[],
[],
],
{
title: 'lineFormatter',
width: 20,
height: 10,
lineFormatter: function (props) {
var output = [{ x: props.plotX, y: props.plotY, symbol: '█' }];
var _a = __read(props.toPlotCoordinates(props.x, props.minY), 2), _ = _a[0], maxY = _a[1];
var i = props.plotY;
while (i <= maxY + 1) {
output.push({ x: props.plotX, y: i, symbol: '█' });
i += 1;
}
return output;
},
},
],
[
[
[-1, 2],
[],
[],
[],
[],
],
{
title: 'symbols',
width: 20,
height: 10,
symbols: {
background: '█',
border: 'A',
empty: 'B',
},
},
],
[
[
[],
[],
[],
[],
],
{ title: 'colors', width: 20, color: 'ansiGreen', height: 10 },
],
[
[
[
[],
[],
[],
[],
],
[
[],
[],
[],
[],
],
],
{
title: 'colors with legend',
width: 20,
thresholds: [{ x: 2, y: 2, color: 'ansiBlue' }],
color: ['ansiGreen', 'ansiMagenta'],
height: 10,
legend: { position: 'bottom', series: ['first', 'second'] },
},
],
[
[
[],
[],
[],
[],
[],
[],
[],
[],
],
{
width: 40,
title: 'thresholds',
thresholds: [
{
y: 5,
x: 5,
color: 'ansiBlue',
},
{
y: 2,
color: 'ansiGreen',
},
],
},
],
[
[
[],
[],
[],
[],
[],
[],
[],
[],
],
{
title: 'with axis center',
color: 'ansiGreen',
showTickLabel: true,
width: 40,
axisCenter: [0, 2],
},
],
[
[
[],
[],
[],
[],
[],
[],
[],
[],
],
{
title: 'bar chart with colors',
color: 'ansiGreen',
barChart: true,
showTickLabel: true,
width: 40,
axisCenter: [0, 0],
},
],
[
[
[],
[],
[],
[],
[],
[],
[],
[],
],
{
title: 'horizontal bar chart with axis center',
horizontalBarChart: true,
showTickLabel: true,
width: 40,
height: 20,
axisCenter: [3, 1],
},
],
[
[
[],
[],
[],
],
{ height: 10, horizontalBarChart: true, width: 20, showTickLabel: true },
],
[
[
[],
[],
[],
],
{ height: 10, barChart: true, width: 20, showTickLabel: true },
],
];
var hasFilter = examples.some(function (_a) {
var _b = __read(_a, 2), only = _b[1].only;
return only !== undefined;
});
console.clear();
examples
.filter(function (example) { return (hasFilter ? example[1].only : true); })
.forEach(function (_a) {
var _b = __read(_a, 2), data = _b[0], options = _b[1];
console.log((0, plot_1.plot)(data, options));
});