ansi-chart
Version:
A lightweight and simple tool for rendering beautiful charts directly in your terminal using only ANSI-compatible ASCII characters. Create bar charts, line graphs, and more with customizable styles, all without external dependencies. Perfect for CLI appli
34 lines • 1.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScatterPlot = void 0;
const ervy_1 = __importDefault(require("ervy"));
class ScatterPlot {
constructor(options) {
this.options = options;
this.chart = ervy_1.default;
this.colors = ['green', 'red', 'blue', 'yellow', 'magenta', 'cyan', 'white'];
}
plot(dataset, colors) {
console.log(this.chart.scatter(this.normalize(dataset).map((v, i) => ({
key: colors && colors[i] !== undefined ? `C${Math.floor(colors[i])}` : 'point',
value: v,
style: this.chart.fg(this.colors[colors && colors[i] !== undefined ? colors[i] % (this.colors.length - 1) : this.colors.length - 1], this.options?.pointChar || '●'),
})), { width: 25, height: 25, legendGap: 12 }));
}
normalize(dataset) {
const X = dataset.column(0);
const Y = dataset.column(1);
const mx = Math.max(...X);
const my = Math.max(...Y);
const nx = Math.min(...X);
const ny = Math.min(...Y);
return dataset
.toArray()
.map((v) => [Math.round(((v[0] - nx) / (mx - nx)) * 25), Math.round(((v[1] - ny) / (my - ny)) * 25)]);
}
}
exports.ScatterPlot = ScatterPlot;
//# sourceMappingURL=scatter-plot.js.map