simple-ascii-chart
Version:
Simple ascii chart generator
197 lines (193 loc) • 5.15 kB
TypeScript
/**
* Symbols for drawing the axes on the graph.
*/
declare const AXIS: {
n: string;
ns: string;
y: string;
nse: string;
x: string;
we: string;
e: string;
intersectionXY: string;
intersectionX: string;
intersectionY: string;
};
/**
* Symbols for rendering chart elements, including lines and areas.
*/
declare const CHART: {
we: string;
wns: string;
ns: string;
nse: string;
wsn: string;
sne: string;
area: string;
};
/**
* Symbol representing an empty space on the graph.
*/
declare const EMPTY = " ";
/**
* Symbols for drawing thresholds on the graph.
*/
declare const THRESHOLDS: {
x: string;
y: string;
};
/**
* Symbol for the point of the graph.
*/
declare const POINT = "\u25CF";
/**
* Represents a point with x and y coordinates.
*/
type Point = [x: number, y: number];
/**
* A point that may contain undefined values or be completely undefined.
*/
type MaybePoint = Point | undefined | [number | undefined, number | undefined];
/**
* A series of connected points representing a single line on a graph.
*/
type SingleLine = Point[];
/**
* A collection of single lines, used for plotting multiple lines on a graph.
*/
type MultiLine = SingleLine[];
/**
* Coordinates, which can be either a single line or multiple lines.
*/
type Coordinates = SingleLine | MultiLine;
/**
* Represents ANSI colors for styling output.
*/
type Color = `ansi${'Red' | 'Green' | 'Black' | 'Yellow' | 'Blue' | 'Magenta' | 'Cyan' | 'White'}`;
/**
* Arguments required for custom line formatting.
*/
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.
*/
type CustomSymbol = {
x: number;
y: number;
symbol: string;
};
/**
* Helpers for formatting, providing axis and range information.
*/
type FormatterHelpers = {
axis: 'x' | 'y';
xRange: number[];
yRange: number[];
};
/**
* Symbols for customizing chart appearance, including axis, chart, and background symbols.
*/
type Symbols = {
axis?: Partial<typeof AXIS>;
chart?: Partial<typeof CHART>;
empty?: string;
background?: string;
border?: string;
thresholds?: Partial<typeof THRESHOLDS>;
point?: string;
};
/**
* Function type for formatting numbers on the chart.
*/
type Formatter = (value: number, helpers: FormatterHelpers) => number | string;
/**
* Configuration for the legend display on the chart.
*/
type Legend = {
position?: 'left' | 'right' | 'top' | 'bottom';
series?: string | string[];
points?: string | string[];
thresholds?: string | string[];
};
/**
* Threshold definition with optional x, y coordinates and a color.
*/
type Threshold = {
x?: number;
y?: number;
color?: Color;
};
/**
* Points definition with x, y coordinates and a color.
*/
type GraphPoint = {
x: number;
y: number;
color?: Color;
};
/**
* Function type for dynamically determining color based on series and coordinates.
*/
type ColorGetter = (series: number, coordinates: MultiLine) => Color;
/**
* Color options, which can be a single color, an array of colors, or a color getter function.
*/
type Colors = Color | Color[] | ColorGetter;
/**
* A 2D array representing the grid of symbols for the graph display.
*/
type Graph = string[][];
/**
* Graph mode options for rendering the graph.
* 'line' mode connects points with lines.
* 'point' mode displays points without connecting lines.
* line is the default mode.
*/
type GraphMode = 'line' | 'point' | 'bar' | 'horizontalBar';
/**
* Configuration settings for rendering a plot.
*/
type Settings = {
color?: Colors;
width?: number;
height?: number;
yRange?: [number, number];
showTickLabel?: boolean;
hideXAxis?: boolean;
hideXAxisTicks?: boolean;
hideYAxis?: boolean;
hideYAxisTicks?: boolean;
customXAxisTicks?: number[];
customYAxisTicks?: number[];
title?: string;
xLabel?: string;
yLabel?: string;
thresholds?: Threshold[];
points?: GraphPoint[];
fillArea?: boolean;
legend?: Legend;
axisCenter?: MaybePoint;
formatter?: Formatter;
lineFormatter?: (args: LineFormatterArgs) => CustomSymbol | CustomSymbol[];
symbols?: Symbols;
mode?: GraphMode;
debugMode?: boolean;
};
/**
* Plot function type that takes coordinates and settings to generate a graph output.
*/
type Plot = (coordinates: Coordinates, settings?: Settings) => string;
declare const plot: Plot;
export { AXIS, CHART, type Color, type ColorGetter, type Colors, type Coordinates, type CustomSymbol, EMPTY, type Formatter, type FormatterHelpers, type Graph, type GraphMode, type GraphPoint, type Legend, type LineFormatterArgs, type MaybePoint, type MultiLine, POINT, type Plot, type Point, type Settings, type SingleLine, type Symbols, THRESHOLDS, type Threshold, plot as default, plot };