@thl-ui/charts 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 thl-ui contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,36 @@
1
- # Temporary Holding Version
1
+ # @thl-ui/charts
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ thl-ui charts: dependency-free SVG geometry for line, bar, pie and sparkline charts.
4
+
5
+ Part of [thl-ui](https://thl.tandd.dev): one UI kit for React, Vue and Angular with a headless TypeScript core, global config, 12 skins, dark mode and i18n (en, ja, vi, zh).
6
+
7
+ The chart math behind the `LineChart`, `BarChart`, `PieChart` and `Sparkline` components in the framework adapters. Pure functions that return SVG paths, with no dependencies.
8
+
9
+ ```bash
10
+ npm i @thl-ui/charts
11
+ ```
12
+
13
+ ```ts
14
+ import { lineChart, niceTicks, smoothPath } from '@thl-ui/charts';
15
+ ```
16
+
17
+ Colors follow the semantic tokens, so charts follow the active skin and dark mode. For complex charts you can still use Chart.js or ECharts next to thl-ui.
18
+
19
+ ## Live demos
20
+
21
+ | Framework | Package | Live demo |
22
+ |---|---|---|
23
+ | React ≥ 18 | [`@thl-ui/react`](https://www.npmjs.com/package/@thl-ui/react) | [thl-react.tandd.dev](https://thl-react.tandd.dev) |
24
+ | Vue ≥ 3.5 | [`@thl-ui/vue`](https://www.npmjs.com/package/@thl-ui/vue) | [thl-vue.tandd.dev](https://thl-vue.tandd.dev) |
25
+ | Angular ≥ 20 | [`@thl-ui/angular`](https://www.npmjs.com/package/@thl-ui/angular) | [thl-angular.tandd.dev](https://thl-angular.tandd.dev) |
26
+
27
+ Each demo is the full 74-page admin template, built with the packages installed from npm. Open the ⚙ panel to switch skin, layout, dark mode and language.
28
+
29
+ ## Links
30
+
31
+ - Docs: [thl.tandd.dev/components/charts](https://thl.tandd.dev/components/charts) (English, 日本語, Tiếng Việt, 简体中文)
32
+ - Source and issues: [GitLab](https://gitlab.com/tandd.packages/thl-ui)
33
+
34
+ ## License
35
+
36
+ MIT
@@ -0,0 +1,147 @@
1
+ /**
2
+ * @thl-ui/charts — dependency-free SVG chart geometry.
3
+ * Chart geometry for thl-ui. Pure functions return
4
+ * paths/coords; each adapter renders <svg> with them, so output is identical across frameworks.
5
+ */
6
+ export interface Series {
7
+ name: string;
8
+ data: number[];
9
+ color?: string;
10
+ }
11
+ export interface Box {
12
+ width: number;
13
+ height: number;
14
+ padding?: {
15
+ top: number;
16
+ right: number;
17
+ bottom: number;
18
+ left: number;
19
+ };
20
+ }
21
+ export declare const chartColors: string[];
22
+ export declare const colorAt: (i: number, s?: {
23
+ color?: string;
24
+ }) => string;
25
+ /** "Nice" axis ticks. */
26
+ export declare function niceTicks(min: number, max: number, count?: number): number[];
27
+ /** Smooth path through points (monotone-ish cardinal spline). */
28
+ export declare function smoothPath(pts: Array<[number, number]>, tension?: number): string;
29
+ export interface XYChart {
30
+ width: number;
31
+ height: number;
32
+ yTicks: Array<{
33
+ value: number;
34
+ y: number;
35
+ }>;
36
+ xLabels: Array<{
37
+ label: string;
38
+ x: number;
39
+ }>;
40
+ series: Array<{
41
+ name: string;
42
+ color: string;
43
+ line: string;
44
+ area: string;
45
+ points: Array<{
46
+ x: number;
47
+ y: number;
48
+ value: number;
49
+ label: string;
50
+ }>;
51
+ }>;
52
+ plot: {
53
+ x: number;
54
+ y: number;
55
+ width: number;
56
+ height: number;
57
+ };
58
+ }
59
+ /** Line / area chart. */
60
+ export declare function lineChart(series: Series[], labels: string[], box: Box, o?: {
61
+ smooth?: boolean;
62
+ min?: number;
63
+ max?: number;
64
+ stacked?: boolean;
65
+ }): XYChart;
66
+ export interface BarChart {
67
+ width: number;
68
+ height: number;
69
+ yTicks: Array<{
70
+ value: number;
71
+ y: number;
72
+ }>;
73
+ xLabels: Array<{
74
+ label: string;
75
+ x: number;
76
+ }>;
77
+ bars: Array<{
78
+ x: number;
79
+ y: number;
80
+ width: number;
81
+ height: number;
82
+ color: string;
83
+ value: number;
84
+ series: string;
85
+ label: string;
86
+ }>;
87
+ plot: {
88
+ x: number;
89
+ y: number;
90
+ width: number;
91
+ height: number;
92
+ };
93
+ }
94
+ /** Grouped or stacked bars. */
95
+ export declare function barChart(series: Series[], labels: string[], box: Box, o?: {
96
+ stacked?: boolean;
97
+ gap?: number;
98
+ max?: number;
99
+ }): BarChart;
100
+ /** Arc path for pie/donut slices. Angles in radians, 0 = 12 o'clock, clockwise. */
101
+ export declare function arcPath(cx: number, cy: number, r: number, inner: number, a0: number, a1: number): string;
102
+ export interface PieSlice {
103
+ label: string;
104
+ value: number;
105
+ percent: number;
106
+ color: string;
107
+ path: string;
108
+ labelX: number;
109
+ labelY: number;
110
+ }
111
+ /** Pie / donut. */
112
+ export declare function pieChart(data: Array<{
113
+ label: string;
114
+ value: number;
115
+ color?: string;
116
+ }>, size: number, o?: {
117
+ donut?: number;
118
+ padAngle?: number;
119
+ }): PieSlice[];
120
+ /** Ring gauge: returns stroke-dasharray values for a circle. */
121
+ export declare function gauge(value: number, max?: number, size?: number, stroke?: number): {
122
+ r: number;
123
+ cx: number;
124
+ cy: number;
125
+ circumference: number;
126
+ offset: number;
127
+ percent: number;
128
+ };
129
+ /** Sparkline (line & bar). */
130
+ export declare function sparkline(data: number[], width: number, height: number, type?: 'line' | 'bar'): {
131
+ bars: {
132
+ x: number;
133
+ y: number;
134
+ width: number;
135
+ height: number;
136
+ value: number;
137
+ }[];
138
+ line: string;
139
+ area: string;
140
+ last?: undefined;
141
+ } | {
142
+ bars: never[];
143
+ line: string;
144
+ area: string;
145
+ last: [number, number];
146
+ };
147
+ export declare const formatTick: (v: number, locale?: string) => string;
package/dist/index.js ADDED
@@ -0,0 +1,172 @@
1
+ export const chartColors = [
2
+ 'var(--thl-color-primary)',
3
+ 'var(--thl-color-success)',
4
+ 'var(--thl-color-warning)',
5
+ 'var(--thl-color-danger)',
6
+ 'var(--thl-color-info)',
7
+ 'var(--thl-color-purple)',
8
+ 'var(--thl-color-inverse)',
9
+ ];
10
+ export const colorAt = (i, s) => s?.color ?? chartColors[i % chartColors.length];
11
+ /** "Nice" axis ticks. */
12
+ export function niceTicks(min, max, count = 5) {
13
+ if (min === max) {
14
+ max = min + 1;
15
+ }
16
+ const span = max - min;
17
+ const step0 = span / Math.max(1, count);
18
+ const mag = 10 ** Math.floor(Math.log10(step0));
19
+ const norm = step0 / mag;
20
+ const step = (norm >= 5 ? 10 : norm >= 2 ? 5 : norm >= 1 ? 2 : 1) * mag;
21
+ const lo = Math.floor(min / step) * step, hi = Math.ceil(max / step) * step;
22
+ const out = [];
23
+ for (let v = lo; v <= hi + step / 2; v += step)
24
+ out.push(Number(v.toFixed(10)));
25
+ return out;
26
+ }
27
+ function frame(box) {
28
+ const p = box.padding ?? { top: 10, right: 10, bottom: 28, left: 40 };
29
+ return { x0: p.left, y0: p.top, w: box.width - p.left - p.right, h: box.height - p.top - p.bottom, p };
30
+ }
31
+ /** Smooth path through points (monotone-ish cardinal spline). */
32
+ export function smoothPath(pts, tension = 0.2) {
33
+ if (pts.length < 2)
34
+ return pts.length ? `M${pts[0][0]},${pts[0][1]}` : '';
35
+ let d = `M${pts[0][0]},${pts[0][1]}`;
36
+ for (let i = 0; i < pts.length - 1; i++) {
37
+ const p0 = pts[i - 1] ?? pts[i], p1 = pts[i], p2 = pts[i + 1], p3 = pts[i + 2] ?? p2;
38
+ const c1 = [p1[0] + (p2[0] - p0[0]) * tension, p1[1] + (p2[1] - p0[1]) * tension];
39
+ const c2 = [p2[0] - (p3[0] - p1[0]) * tension, p2[1] - (p3[1] - p1[1]) * tension];
40
+ d += ` C${c1[0].toFixed(2)},${c1[1].toFixed(2)} ${c2[0].toFixed(2)},${c2[1].toFixed(2)} ${p2[0]},${p2[1]}`;
41
+ }
42
+ return d;
43
+ }
44
+ const linePath = (pts) => pts.map((p, i) => `${i ? 'L' : 'M'}${p[0]},${p[1]}`).join(' ');
45
+ /** Line / area chart. */
46
+ export function lineChart(series, labels, box, o = {}) {
47
+ const f = frame(box);
48
+ const all = series.flatMap((s) => s.data);
49
+ const ticks = niceTicks(o.min ?? Math.min(0, ...all), o.max ?? Math.max(...all, 1));
50
+ const lo = ticks[0], hi = ticks[ticks.length - 1];
51
+ const n = Math.max(1, labels.length - 1);
52
+ const sx = (i) => f.x0 + (labels.length === 1 ? f.w / 2 : (i / n) * f.w);
53
+ const sy = (v) => f.y0 + f.h - ((v - lo) / (hi - lo || 1)) * f.h;
54
+ return {
55
+ width: box.width,
56
+ height: box.height,
57
+ plot: { x: f.x0, y: f.y0, width: f.w, height: f.h },
58
+ yTicks: ticks.map((value) => ({ value, y: Number(sy(value).toFixed(2)) })),
59
+ xLabels: labels.map((label, i) => ({ label, x: Number(sx(i).toFixed(2)) })),
60
+ series: series.map((s, si) => {
61
+ const pts = s.data.map((v, i) => [Number(sx(i).toFixed(2)), Number(sy(v).toFixed(2))]);
62
+ const line = o.smooth ? smoothPath(pts) : linePath(pts);
63
+ const base = Number(sy(Math.max(lo, 0)).toFixed(2));
64
+ const area = pts.length ? `${line} L${pts[pts.length - 1][0]},${base} L${pts[0][0]},${base} Z` : '';
65
+ return { name: s.name, color: colorAt(si, s), line, area, points: pts.map(([x, y], i) => ({ x, y, value: s.data[i], label: labels[i] })) };
66
+ }),
67
+ };
68
+ }
69
+ /** Grouped or stacked bars. */
70
+ export function barChart(series, labels, box, o = {}) {
71
+ const f = frame(box);
72
+ const totals = labels.map((_, i) => series.reduce((a, s) => a + Math.max(0, s.data[i] ?? 0), 0));
73
+ const max = o.max ?? (o.stacked ? Math.max(...totals, 1) : Math.max(...series.flatMap((s) => s.data), 1));
74
+ const ticks = niceTicks(0, max);
75
+ const hi = ticks[ticks.length - 1];
76
+ const band = f.w / Math.max(1, labels.length);
77
+ const gap = o.gap ?? 0.3;
78
+ const inner = band * (1 - gap);
79
+ const sy = (v) => f.y0 + f.h - (v / hi) * f.h;
80
+ const bars = [];
81
+ labels.forEach((label, i) => {
82
+ const bx = f.x0 + i * band + (band - inner) / 2;
83
+ let acc = 0;
84
+ series.forEach((s, si) => {
85
+ const v = s.data[i] ?? 0;
86
+ const color = colorAt(si, s);
87
+ if (o.stacked) {
88
+ const y = sy(acc + v);
89
+ bars.push({ x: bx, y, width: inner, height: sy(acc) - y, color, value: v, series: s.name, label });
90
+ acc += v;
91
+ }
92
+ else {
93
+ const w = inner / series.length;
94
+ const y = sy(v);
95
+ bars.push({ x: bx + si * w, y, width: Math.max(1, w - 2), height: f.y0 + f.h - y, color, value: v, series: s.name, label });
96
+ }
97
+ });
98
+ });
99
+ return {
100
+ width: box.width,
101
+ height: box.height,
102
+ plot: { x: f.x0, y: f.y0, width: f.w, height: f.h },
103
+ yTicks: ticks.map((value) => ({ value, y: sy(value) })),
104
+ xLabels: labels.map((label, i) => ({ label, x: f.x0 + i * band + band / 2 })),
105
+ bars: bars.map((b) => ({ ...b, x: +b.x.toFixed(2), y: +b.y.toFixed(2), width: +b.width.toFixed(2), height: +Math.max(0, b.height).toFixed(2) })),
106
+ };
107
+ }
108
+ /** Arc path for pie/donut slices. Angles in radians, 0 = 12 o'clock, clockwise. */
109
+ export function arcPath(cx, cy, r, inner, a0, a1) {
110
+ const full = a1 - a0 >= Math.PI * 2 - 1e-6;
111
+ if (full)
112
+ a1 = a0 + Math.PI * 2 - 1e-4;
113
+ const pt = (rad, a) => [cx + rad * Math.sin(a), cy - rad * Math.cos(a)].map((v) => +v.toFixed(3));
114
+ const large = a1 - a0 > Math.PI ? 1 : 0;
115
+ const [x0, y0] = pt(r, a0), [x1, y1] = pt(r, a1);
116
+ if (inner <= 0)
117
+ return `M${cx},${cy} L${x0},${y0} A${r},${r} 0 ${large} 1 ${x1},${y1} Z`;
118
+ const [x2, y2] = pt(inner, a1), [x3, y3] = pt(inner, a0);
119
+ return `M${x0},${y0} A${r},${r} 0 ${large} 1 ${x1},${y1} L${x2},${y2} A${inner},${inner} 0 ${large} 0 ${x3},${y3} Z`;
120
+ }
121
+ /** Pie / donut. */
122
+ export function pieChart(data, size, o = {}) {
123
+ const total = data.reduce((a, d) => a + Math.max(0, d.value), 0) || 1;
124
+ const r = size / 2, cx = r, cy = r, inner = (o.donut ?? 0) * r;
125
+ let a = 0;
126
+ return data.map((d, i) => {
127
+ const sweep = (Math.max(0, d.value) / total) * Math.PI * 2;
128
+ const a0 = a + (o.padAngle ?? 0) / 2, a1 = a + sweep - (o.padAngle ?? 0) / 2;
129
+ const mid = a + sweep / 2;
130
+ a += sweep;
131
+ const lr = inner ? (r + inner) / 2 : r * 0.62;
132
+ return {
133
+ label: d.label,
134
+ value: d.value,
135
+ percent: (d.value / total) * 100,
136
+ color: colorAt(i, d),
137
+ path: arcPath(cx, cy, r, inner, a0, Math.max(a0, a1)),
138
+ labelX: +(cx + lr * Math.sin(mid)).toFixed(2),
139
+ labelY: +(cy - lr * Math.cos(mid)).toFixed(2),
140
+ };
141
+ });
142
+ }
143
+ /** Ring gauge: returns stroke-dasharray values for a circle. */
144
+ export function gauge(value, max = 100, size = 120, stroke = 8) {
145
+ const r = (size - stroke) / 2;
146
+ const c = 2 * Math.PI * r;
147
+ const pct = Math.max(0, Math.min(1, value / (max || 1)));
148
+ return { r, cx: size / 2, cy: size / 2, circumference: +c.toFixed(3), offset: +(c * (1 - pct)).toFixed(3), percent: Math.round(pct * 100) };
149
+ }
150
+ /** Sparkline (line & bar). */
151
+ export function sparkline(data, width, height, type = 'line') {
152
+ const min = Math.min(...data), max = Math.max(...data);
153
+ const sy = (v) => +(height - 2 - ((v - min) / (max - min || 1)) * (height - 4)).toFixed(2);
154
+ if (type === 'bar') {
155
+ const w = width / data.length;
156
+ return {
157
+ bars: data.map((v, i) => ({
158
+ x: +(i * w + 1).toFixed(2),
159
+ y: sy(v),
160
+ width: +Math.max(1, w - 2).toFixed(2),
161
+ height: +(height - sy(v)).toFixed(2),
162
+ value: v,
163
+ })),
164
+ line: '',
165
+ area: '',
166
+ };
167
+ }
168
+ const pts = data.map((v, i) => [+((i / Math.max(1, data.length - 1)) * width).toFixed(2), sy(v)]);
169
+ const line = linePath(pts);
170
+ return { bars: [], line, area: `${line} L${width},${height} L0,${height} Z`, last: pts[pts.length - 1] };
171
+ }
172
+ export const formatTick = (v, locale) => new Intl.NumberFormat(locale, { notation: Math.abs(v) >= 10000 ? 'compact' : 'standard', maximumFractionDigits: 1 }).format(v);
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@thl-ui/charts",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "thl-ui charts: dependency-free SVG geometry for line, bar, pie and sparkline charts.",
5
+ "keywords": [
6
+ "thl-ui",
7
+ "ui",
8
+ "components",
9
+ "admin",
10
+ "dashboard",
11
+ "charts",
12
+ "svg",
13
+ "sparkline"
14
+ ],
15
+ "license": "MIT",
16
+ "homepage": "https://thl.tandd.dev/components/charts",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://gitlab.com/tandd.packages/thl-ui.git",
20
+ "directory": "packages/charts"
21
+ },
22
+ "bugs": {
23
+ "url": "https://gitlab.com/tandd.packages/thl-ui/-/issues"
24
+ },
25
+ "type": "module",
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "sideEffects": false,
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.json",
40
+ "typecheck": "tsc -p tsconfig.json --noEmit"
41
+ }
42
+ }