@tradeglotnpm/charts 0.1.0 → 0.2.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.
@@ -0,0 +1,46 @@
1
+ import type { Timeline } from "./timeline";
2
+ import { type Layout } from "./paint";
3
+ import type { AnySeries } from "./series";
4
+ export interface AxisOptions {
5
+ grid: "solid" | "dotted" | "none";
6
+ timeVisible: boolean;
7
+ secondsVisible: boolean;
8
+ timeLabel: string;
9
+ priceDecimals: number | undefined;
10
+ }
11
+ /** Price ticks for the current scale and the decimals of their labels. */
12
+ export declare function priceTicks(lo: number, hi: number, plotH: number, decimals?: number): {
13
+ ticks: number[];
14
+ dec: number;
15
+ };
16
+ /**
17
+ * Bar indexes that get a time label: the first bar of each round period (5 min, 1 h, 1 day…),
18
+ * with the period chosen so labels are at least ~90 px apart.
19
+ */
20
+ export declare function timeTicks(L: Layout, tl: Timeline): number[];
21
+ /** Label of one time on the axis: the day when it changes, else the clock time. */
22
+ export declare function timeText(t: number, prev: number | null, step: number, o: AxisOptions): string;
23
+ /** Full time for the crosshair label. */
24
+ export declare function fullTime(t: number, o: AxisOptions): string;
25
+ export declare function paintGrid(ctx: CanvasRenderingContext2D, L: Layout, ticks: number[], times: number[], o: AxisOptions): void;
26
+ /** A label on the price axis, with an optional title box just left of it in the plot. */
27
+ export interface AxisLabel {
28
+ y: number;
29
+ text: string;
30
+ bg: string;
31
+ title?: string;
32
+ }
33
+ /**
34
+ * Moves labels apart so none overlap: down first, then back up where the stack would run past
35
+ * `bottom` (sorted by price position).
36
+ */
37
+ export declare function spreadLabels(ys: number[], bottom?: number, h?: number): number[];
38
+ export declare function axisLabel(ctx: CanvasRenderingContext2D, L: Layout, y: number, text: string, bg: string, fg?: string): void;
39
+ /** Price line and last value labels of every visible series. */
40
+ export declare function collectLabels(series: readonly AnySeries[], L: Layout, dec: number): AxisLabel[];
41
+ export declare function paintPriceAxis(ctx: CanvasRenderingContext2D, L: Layout, series: readonly AnySeries[], ticks: number[], dec: number): void;
42
+ export declare function paintTimeAxis(ctx: CanvasRenderingContext2D, L: Layout, tl: Timeline, ticks: number[], o: AxisOptions): void;
43
+ export declare function paintCrosshair(ctx: CanvasRenderingContext2D, L: Layout, tl: Timeline, hover: {
44
+ index: number;
45
+ y: number;
46
+ }, price: number, dec: number, o: AxisOptions): void;
@@ -0,0 +1,40 @@
1
+ import type { ChartTheme } from "../theme";
2
+ import { type AxisOptions } from "./axes";
3
+ import { type Layout } from "./paint";
4
+ import type { AnySeries } from "./series";
5
+ import type { Timeline } from "./timeline";
6
+ import type { Overlay } from "./types";
7
+ export interface View {
8
+ /** Logical index at the right edge of the plot (the center of the last slot). */
9
+ end: number;
10
+ /** Pixels per bar. */
11
+ bw: number;
12
+ }
13
+ export interface Scale {
14
+ lo: number;
15
+ hi: number;
16
+ top: number;
17
+ inner: number;
18
+ }
19
+ export interface FrameState {
20
+ width: number;
21
+ height: number;
22
+ axisW: number;
23
+ axisH: number;
24
+ view: View;
25
+ timeline: Timeline;
26
+ series: readonly AnySeries[];
27
+ overlays: readonly Overlay[];
28
+ theme: ChartTheme;
29
+ axis: AxisOptions;
30
+ hover: {
31
+ index: number;
32
+ y: number;
33
+ } | null;
34
+ }
35
+ /** Price range the visible series ask for, after each series' autoscale hook. */
36
+ export declare function priceRange(series: readonly AnySeries[], tl: Timeline, first: number, last: number): [number, number] | null;
37
+ export declare function makeLayout(st: FrameState, scale: Scale): Layout;
38
+ /** Works out the scale for the current view. */
39
+ export declare function makeScale(st: FrameState, prev: Scale | null): Scale;
40
+ export declare function paintFrame(ctx: CanvasRenderingContext2D, st: FrameState, scale: Scale): Layout;
@@ -0,0 +1,28 @@
1
+ export interface GestureTarget {
2
+ /** Pan and zoom are on (checked on every event, so a host can turn them off mid-drag). */
3
+ enabled(): boolean;
4
+ viewEnd(): number;
5
+ /** Pan so that the view end becomes `start` shifted by `dx` pixels. */
6
+ pan(start: number, dx: number): void;
7
+ zoom(factor: number, x: number): void;
8
+ hover(x: number | null, y: number): void;
9
+ click(x: number, y: number): void;
10
+ }
11
+ export declare class Gestures {
12
+ private el;
13
+ private t;
14
+ private pointers;
15
+ private drag;
16
+ private pinch;
17
+ private off;
18
+ constructor(el: HTMLElement, t: GestureTarget);
19
+ private on;
20
+ private pos;
21
+ private wheel;
22
+ private down;
23
+ private move;
24
+ private up;
25
+ private end;
26
+ private spread;
27
+ dispose(): void;
28
+ }
@@ -0,0 +1,2 @@
1
+ import type { TimeChartOptions } from "./types";
2
+ export declare function attributionLink(opts: TimeChartOptions): HTMLAnchorElement;
@@ -0,0 +1,33 @@
1
+ import type { ChartTheme } from "../theme";
2
+ import type { AnySeries } from "./series";
3
+ import type { LineStyleName } from "./types";
4
+ /** Geometry of one frame. */
5
+ export interface Layout {
6
+ width: number;
7
+ height: number;
8
+ plotW: number;
9
+ plotH: number;
10
+ /** Pixels per bar. */
11
+ bw: number;
12
+ /** First and last whole bar index to draw. */
13
+ first: number;
14
+ last: number;
15
+ x(i: number): number;
16
+ y(price: number): number;
17
+ /** Timeline index of an exact time. */
18
+ at(time: number): number | undefined;
19
+ theme: ChartTheme;
20
+ }
21
+ export declare function dash(style: LineStyleName | undefined, w: number): number[];
22
+ export declare function paintSeries(ctx: CanvasRenderingContext2D, s: AnySeries, L: Layout): void;
23
+ /** Price lines of a series, and its last price line when asked for. */
24
+ export declare function paintPriceLines(ctx: CanvasRenderingContext2D, s: AnySeries, L: Layout): void;
25
+ /** Black or white, whichever reads better on `color` (#rgb, #rrggbb or rgb()/rgba()). */
26
+ export declare function contrast(color: string): string;
27
+ /** Last value of a series and the color its label uses. */
28
+ export declare function lastPoint(s: AnySeries, th: ChartTheme): {
29
+ value: number;
30
+ color: string;
31
+ } | null;
32
+ /** Markers of a series: shapes stacked above or below the bar, with their text. */
33
+ export declare function paintMarkers(ctx: CanvasRenderingContext2D, s: AnySeries, L: Layout): void;
@@ -0,0 +1,67 @@
1
+ import type { Bar, CandleOptions, HistogramOptions, LineOptions, Marker, PriceLineOptions, SeriesOptions, TimeValue } from "./types";
2
+ /** What a series needs from its chart. */
3
+ export interface SeriesHost {
4
+ /** Data changed: rebuild the time axis and redraw. */
5
+ dataChanged(): void;
6
+ /** Only looks changed: redraw. */
7
+ redraw(): void;
8
+ priceToY(price: number): number | null;
9
+ yToPrice(y: number): number | null;
10
+ }
11
+ export type SeriesKind = "candles" | "line" | "histogram";
12
+ /** A horizontal line at one price. */
13
+ export declare class PriceLine {
14
+ private host;
15
+ opts: PriceLineOptions;
16
+ private owner;
17
+ constructor(host: SeriesHost, opts: PriceLineOptions, owner: {
18
+ removePriceLine(l: PriceLine): void;
19
+ });
20
+ update(opts: Partial<PriceLineOptions>): void;
21
+ remove(): void;
22
+ }
23
+ export declare abstract class Series<P extends {
24
+ time: number;
25
+ }, O extends SeriesOptions> {
26
+ protected host: SeriesHost;
27
+ opts: O;
28
+ abstract readonly kind: SeriesKind;
29
+ protected points: P[];
30
+ lines: PriceLine[];
31
+ markers: Marker[];
32
+ constructor(host: SeriesHost, opts: O);
33
+ /** Replaces the data (any order; sorted by time here). */
34
+ setData(points: readonly P[]): void;
35
+ /** Adds a point or replaces the one with the same time. */
36
+ update(point: P): void;
37
+ data(): readonly P[];
38
+ times(): number[];
39
+ setOptions(opts: Partial<O>): void;
40
+ get visible(): boolean;
41
+ addPriceLine(opts: PriceLineOptions): PriceLine;
42
+ removePriceLine(l: PriceLine): void;
43
+ /** Replaces the markers. */
44
+ setMarkers(markers: readonly Marker[]): void;
45
+ priceToY(price: number): number | null;
46
+ yToPrice(y: number): number | null;
47
+ /** Low and high of one point, for the scale. */
48
+ abstract span(p: P): [number, number];
49
+ /** The value labels and the last price line use. */
50
+ abstract value(p: P): number;
51
+ }
52
+ export declare class CandleSeries extends Series<Bar, CandleOptions> {
53
+ readonly kind = "candles";
54
+ span(p: Bar): [number, number];
55
+ value(p: Bar): number;
56
+ }
57
+ export declare class LineSeriesApi extends Series<TimeValue, LineOptions> {
58
+ readonly kind = "line";
59
+ span(p: TimeValue): [number, number];
60
+ value(p: TimeValue): number;
61
+ }
62
+ export declare class HistogramSeries extends Series<TimeValue, HistogramOptions> {
63
+ readonly kind = "histogram";
64
+ span(p: TimeValue): [number, number];
65
+ value(p: TimeValue): number;
66
+ }
67
+ export type AnySeries = CandleSeries | LineSeriesApi | HistogramSeries;
@@ -0,0 +1,76 @@
1
+ import { CandleSeries, HistogramSeries, LineSeriesApi, type AnySeries } from "./series";
2
+ import type { CandleOptions, ClickInfo, HistogramOptions, LineOptions, Overlay, Range, TimeChartOptions } from "./types";
3
+ export declare class TimeChart {
4
+ private opts;
5
+ readonly el: HTMLDivElement;
6
+ private canvas;
7
+ private legendEl;
8
+ private series;
9
+ private overlays;
10
+ private timeline;
11
+ private view;
12
+ private scale;
13
+ private hover;
14
+ private clicks;
15
+ private rangeSubs;
16
+ private lastRange;
17
+ private raf;
18
+ private resize;
19
+ private host;
20
+ private gestures;
21
+ constructor(hostEl: HTMLElement, opts?: TimeChartOptions);
22
+ addCandles(opts?: CandleOptions): CandleSeries;
23
+ addLine(opts?: LineOptions): LineSeriesApi;
24
+ addHistogram(opts?: HistogramOptions): HistogramSeries;
25
+ private add;
26
+ removeSeries(s: AnySeries): void;
27
+ /** Adds a host drawing; returns a function that removes it. */
28
+ addOverlay(o: Overlay): () => void;
29
+ setOptions(opts: Partial<TimeChartOptions>): void;
30
+ /** Current options, with `barSpacing` and `rightOffset` as the view has them now. */
31
+ options(): TimeChartOptions & {
32
+ barSpacing: number;
33
+ rightOffset: number;
34
+ };
35
+ /** Width and height of the plot area (without the axes), in CSS pixels. */
36
+ plotSize(): {
37
+ width: number;
38
+ height: number;
39
+ };
40
+ indexToX(i: number): number;
41
+ xToIndex(x: number): number;
42
+ timeToX(t: number): number | null;
43
+ xToTime(x: number): number | null;
44
+ /** Index of an exact time on the shared time axis, or null. */
45
+ timeToIndex(t: number): number | null;
46
+ priceToY(p: number): number | null;
47
+ yToPrice(y: number): number | null;
48
+ getVisibleIndexRange(): Range | null;
49
+ setVisibleIndexRange(r: Range): void;
50
+ /** First and last visible bar times inside the data, or null without data. */
51
+ getVisibleRange(): Range | null;
52
+ setVisibleRange(r: Range): void;
53
+ /** Shows the newest bar with the right offset. */
54
+ scrollToEnd(): void;
55
+ /** Fits every bar into the plot. */
56
+ fitContent(): void;
57
+ /** Calls `cb` when the visible time range changes; returns an unsubscribe function. */
58
+ onVisibleRangeChange(cb: (r: Range | null) => void): () => void;
59
+ /** Calls `cb` on a click (not a drag); returns an unsubscribe function. */
60
+ onClick(cb: (c: ClickInfo) => void): () => void;
61
+ /** Redraws on the next frame (for example after an overlay's data changed). */
62
+ invalidate(): void;
63
+ dispose(): void;
64
+ private dataChanged;
65
+ private panTo;
66
+ private zoomAt;
67
+ private setHover;
68
+ private emitClick;
69
+ private state;
70
+ private currentScale;
71
+ private draw;
72
+ private renderLegend;
73
+ private emitRange;
74
+ }
75
+ /** Creates a time chart inside `host` (it fills the host, so give the host a size). */
76
+ export declare function createTimeChart(host: HTMLElement, options?: TimeChartOptions): TimeChart;
@@ -0,0 +1,15 @@
1
+ export declare class Timeline {
2
+ times: number[];
3
+ private index;
4
+ /** Rebuilds from the times of every series. */
5
+ rebuild(lists: readonly (readonly number[])[]): void;
6
+ get length(): number;
7
+ /** Index of an exact time, or undefined. */
8
+ exact(t: number): number | undefined;
9
+ /** Typical bar interval: the last gap, or 60 s with fewer than two bars. */
10
+ step(): number;
11
+ /** Logical index of a time: exact, interpolated between bars, or extrapolated outside. */
12
+ toIndex(t: number): number | null;
13
+ /** Time of a logical index (rounded), extrapolated past the ends. */
14
+ toTime(i: number): number | null;
15
+ }
@@ -0,0 +1,128 @@
1
+ import type { ThemeOption } from "../theme";
2
+ import type { Bar } from "../types";
3
+ /** How a line is stroked. */
4
+ export type LineStyleName = "solid" | "dotted" | "dashed" | "large-dashed" | "sparse-dotted";
5
+ /** One value at one time, for line and histogram series. */
6
+ export interface TimeValue {
7
+ time: number;
8
+ value: number;
9
+ /** Optional color of this point (histogram bars). */
10
+ color?: string;
11
+ }
12
+ /**
13
+ * Changes the price range a series asks for. Receives the range the chart worked out from the
14
+ * visible data (or null when nothing is visible) and returns the range to use, or null to take
15
+ * no part in the scale.
16
+ */
17
+ export type AutoscaleFn = (base: [number, number] | null) => [number, number] | null;
18
+ export interface SeriesOptions {
19
+ /** Shown in the legend and next to the last value label. */
20
+ title?: string;
21
+ /** Hidden series draw nothing and take no part in the scale (default true). */
22
+ visible?: boolean;
23
+ /** Decimals of the labels this series puts on the price axis (default: from the axis step). */
24
+ priceDecimals?: number;
25
+ /** See `AutoscaleFn`. */
26
+ autoscale?: AutoscaleFn;
27
+ /** Label with the last value on the price axis (default true). */
28
+ lastValueLabel?: boolean;
29
+ /** Dashed line across the chart at the last value (default true for candles, false otherwise). */
30
+ lastPriceLine?: boolean;
31
+ }
32
+ export interface CandleOptions extends SeriesOptions {
33
+ /** Body and wick colors (default the theme's up and down). */
34
+ upColor?: string;
35
+ downColor?: string;
36
+ wickUpColor?: string;
37
+ wickDownColor?: string;
38
+ }
39
+ export interface LineOptions extends SeriesOptions {
40
+ color?: string;
41
+ /** Line width in CSS pixels (default 1.5). */
42
+ width?: number;
43
+ style?: LineStyleName;
44
+ }
45
+ export interface HistogramOptions extends SeriesOptions {
46
+ /** Color of bars that carry no color of their own. */
47
+ color?: string;
48
+ /** Value the bars start from (default 0). */
49
+ base?: number;
50
+ }
51
+ export interface PriceLineOptions {
52
+ price: number;
53
+ color?: string;
54
+ /** Width in CSS pixels (default 1). */
55
+ width?: number;
56
+ style?: LineStyleName;
57
+ /** Label with the price on the axis (default true). */
58
+ axisLabel?: boolean;
59
+ /** Text drawn at the right end of the line. */
60
+ title?: string;
61
+ }
62
+ /** A mark tied to one time of a series. */
63
+ export interface Marker {
64
+ time: number;
65
+ /** Above the bar's high, below its low, or at the series value. */
66
+ position: "above" | "below" | "inside";
67
+ shape: "arrow_up" | "arrow_down" | "circle" | "square";
68
+ color?: string;
69
+ text?: string;
70
+ /** Size multiplier (default 1). Small sizes leave mostly the text. */
71
+ size?: number;
72
+ }
73
+ /** Something the host draws on the price pane, in CSS pixels of the plot area. */
74
+ export interface Overlay {
75
+ /** Below the series ("bottom") or above them ("top", default). */
76
+ zOrder?: "bottom" | "top";
77
+ draw(ctx: CanvasRenderingContext2D, size: {
78
+ width: number;
79
+ height: number;
80
+ }): void;
81
+ }
82
+ /** A visible range: by time (Unix ms) or by logical bar index. */
83
+ export interface Range {
84
+ from: number;
85
+ to: number;
86
+ }
87
+ /** What a click hit. `time` and `index` are null outside the data. */
88
+ export interface ClickInfo {
89
+ x: number;
90
+ y: number;
91
+ time: number | null;
92
+ index: number | null;
93
+ price: number | null;
94
+ }
95
+ export interface TimeChartOptions {
96
+ /** "dark" (default), "light" or your own colors. */
97
+ theme?: ThemeOption;
98
+ /** Glot Charts attribution logo, on by default (see NOTICE and docs/attribution.md). */
99
+ attributionLogo?: boolean;
100
+ attributionHref?: string;
101
+ attributionSrc?: string;
102
+ /** Pan with drag, zoom with wheel or pinch (default true). */
103
+ interactive?: boolean;
104
+ /** Crosshair with price and time labels (default true). */
105
+ crosshair?: boolean;
106
+ /** Grid lines (default "solid"). */
107
+ grid?: "solid" | "dotted" | "none";
108
+ /** Empty bars kept right of the newest bar (default 6). */
109
+ rightOffset?: number;
110
+ /** Pixels per bar (default 8). */
111
+ barSpacing?: number;
112
+ /** Show hours and minutes on the time axis (default true). */
113
+ timeVisible?: boolean;
114
+ /** Show seconds on the time axis (default false). */
115
+ secondsVisible?: boolean;
116
+ /** Time zone name shown at the end of the time axis (default "UTC"; "" hides it). Times are
117
+ * always drawn as given, read as UTC. */
118
+ timeLabel?: string;
119
+ /** Width of the price axis in CSS pixels (default 72). */
120
+ priceAxisWidth?: number;
121
+ /** Height of the time axis in CSS pixels (default 24). */
122
+ timeAxisHeight?: number;
123
+ /** OHLC and titled series values in the top-left corner (default true). */
124
+ legend?: boolean;
125
+ /** Decimals of the price axis (default: from the axis step). */
126
+ priceDecimals?: number;
127
+ }
128
+ export type { Bar };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tradeglotnpm/charts",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Open-source financial chart library for the web: candles, indicator lines, marks and trades on Canvas 2D, with no run-time dependencies",
5
5
  "license": "Apache-2.0",
6
6
  "author": "TradeGlot",