simple-ascii-chart
Version:
Simple ascii chart generator
128 lines (127 loc) • 3.49 kB
TypeScript
import { AXIS, CHART } from '../constants';
/**
* Represents a point with x and y coordinates.
*/
export type Point = [x: number, y: number];
/**
* A point that may contain undefined values or be completely undefined.
*/
export type MaybePoint = Point | undefined | [number | undefined, number | undefined];
/**
* A series of connected points representing a single line on a graph.
*/
export type SingleLine = Point[];
/**
* A collection of single lines, used for plotting multiple lines on a graph.
*/
export type MultiLine = SingleLine[];
/**
* Coordinates, which can be either a single line or multiple lines.
*/
export type Coordinates = SingleLine | MultiLine;
/**
* Represents ANSI colors for styling output.
*/
export type Color = `ansi${'Red' | 'Green' | 'Black' | 'Yellow' | 'Blue' | 'Magenta' | 'Cyan' | 'White'}`;
/**
* Arguments required for custom line formatting.
*/
export type LineFormatterArgs = {
x: number;
y: number;
plotX: number;
plotY: number;
input: SingleLine;
index: number;
minY: number;
minX: number;
expansionX: number[];
expansionY: number[];
toPlotCoordinates: (x: number, y: number) => Point;
};
/**
* Custom symbol with specified coordinates and symbol character.
*/
export type CustomSymbol = {
x: number;
y: number;
symbol: string;
};
/**
* Helpers for formatting, providing axis and range information.
*/
export type FormatterHelpers = {
axis: 'x' | 'y';
xRange: number[];
yRange: number[];
};
/**
* Symbols for customizing chart appearance, including axis, chart, and background symbols.
*/
export type Symbols = {
axis?: Partial<typeof AXIS>;
chart?: Partial<typeof CHART>;
empty?: string;
background?: string;
border?: string;
};
/**
* Function type for formatting numbers on the chart.
*/
export type Formatter = (value: number, helpers: FormatterHelpers) => number | string;
/**
* Configuration for the legend display on the chart.
*/
export type Legend = {
position?: 'left' | 'right' | 'top' | 'bottom';
series: string | string[];
};
/**
* Threshold definition with optional x, y coordinates and a color.
*/
export type Threshold = {
x?: number;
y?: number;
color?: Color;
};
/**
* Function type for dynamically determining color based on series and coordinates.
*/
export type ColorGetter = (series: number, coordinates: MultiLine) => Color;
/**
* Color options, which can be a single color, an array of colors, or a color getter function.
*/
export type Colors = Color | Color[] | ColorGetter;
/**
* A 2D array representing the grid of symbols for the graph display.
*/
export type Graph = string[][];
/**
* Configuration settings for rendering a plot.
*/
export type Settings = {
color?: Colors;
width?: number;
height?: number;
yRange?: [number, number];
showTickLabel?: boolean;
hideXAxis?: boolean;
hideYAxis?: boolean;
title?: string;
xLabel?: string;
yLabel?: string;
thresholds?: Threshold[];
fillArea?: boolean;
horizontalBarChart?: boolean;
barChart?: boolean;
legend?: Legend;
axisCenter?: MaybePoint;
formatter?: Formatter;
lineFormatter?: (args: LineFormatterArgs) => CustomSymbol | CustomSymbol[];
symbols?: Symbols;
debugMode?: boolean;
};
/**
* Plot function type that takes coordinates and settings to generate a graph output.
*/
export type Plot = (coordinates: Coordinates, settings?: Settings) => string;