@aura-react-comp/charts 0.0.0-stage → 0.3.1
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 +21 -0
- package/README.md +90 -2
- package/esm/charts/area-chart/index.d.ts +19 -0
- package/esm/charts/area-chart/index.d.ts.map +1 -0
- package/esm/charts/area-chart/index.js +31 -0
- package/esm/charts/bar-chart/index.d.ts +16 -0
- package/esm/charts/bar-chart/index.d.ts.map +1 -0
- package/esm/charts/bar-chart/index.js +28 -0
- package/esm/charts/candlestick-chart/index.d.ts +47 -0
- package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
- package/esm/charts/candlestick-chart/index.js +347 -0
- package/esm/charts/combo-chart/index.d.ts +17 -0
- package/esm/charts/combo-chart/index.d.ts.map +1 -0
- package/esm/charts/combo-chart/index.js +26 -0
- package/esm/charts/funnel-chart/index.d.ts +35 -0
- package/esm/charts/funnel-chart/index.d.ts.map +1 -0
- package/esm/charts/funnel-chart/index.js +200 -0
- package/esm/charts/gauge-chart/index.d.ts +41 -0
- package/esm/charts/gauge-chart/index.d.ts.map +1 -0
- package/esm/charts/gauge-chart/index.js +217 -0
- package/esm/charts/graph-chart/index.d.ts +35 -0
- package/esm/charts/graph-chart/index.d.ts.map +1 -0
- package/esm/charts/graph-chart/index.js +224 -0
- package/esm/charts/heatmap-chart/index.d.ts +47 -0
- package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
- package/esm/charts/heatmap-chart/index.js +281 -0
- package/esm/charts/line-chart/index.d.ts +23 -0
- package/esm/charts/line-chart/index.d.ts.map +1 -0
- package/esm/charts/line-chart/index.js +33 -0
- package/esm/charts/pie-chart/index.d.ts +38 -0
- package/esm/charts/pie-chart/index.d.ts.map +1 -0
- package/esm/charts/pie-chart/index.js +272 -0
- package/esm/charts/radar-chart/index.d.ts +44 -0
- package/esm/charts/radar-chart/index.d.ts.map +1 -0
- package/esm/charts/radar-chart/index.js +263 -0
- package/esm/charts/ring-progress/index.d.ts +38 -0
- package/esm/charts/ring-progress/index.d.ts.map +1 -0
- package/esm/charts/ring-progress/index.js +137 -0
- package/esm/charts/sankey-chart/index.d.ts +34 -0
- package/esm/charts/sankey-chart/index.d.ts.map +1 -0
- package/esm/charts/sankey-chart/index.js +248 -0
- package/esm/charts/scatter-chart/index.d.ts +34 -0
- package/esm/charts/scatter-chart/index.d.ts.map +1 -0
- package/esm/charts/scatter-chart/index.js +305 -0
- package/esm/charts/sparkline/index.d.ts +31 -0
- package/esm/charts/sparkline/index.d.ts.map +1 -0
- package/esm/charts/sparkline/index.js +152 -0
- package/esm/charts/tree-chart/index.d.ts +34 -0
- package/esm/charts/tree-chart/index.d.ts.map +1 -0
- package/esm/charts/tree-chart/index.js +200 -0
- package/esm/charts/treemap-chart/index.d.ts +35 -0
- package/esm/charts/treemap-chart/index.d.ts.map +1 -0
- package/esm/charts/treemap-chart/index.js +239 -0
- package/esm/charts/waterfall-chart/index.d.ts +41 -0
- package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
- package/esm/charts/waterfall-chart/index.js +281 -0
- package/esm/components/Legend.d.ts +24 -0
- package/esm/components/Legend.d.ts.map +1 -0
- package/esm/components/Legend.js +50 -0
- package/esm/components/Tooltip.d.ts +24 -0
- package/esm/components/Tooltip.d.ts.map +1 -0
- package/esm/components/Tooltip.js +85 -0
- package/esm/components/chart/index.d.ts +71 -0
- package/esm/components/chart/index.d.ts.map +1 -0
- package/esm/components/chart/index.js +142 -0
- package/esm/components/chart.less +223 -0
- package/esm/core/animation.d.ts +49 -0
- package/esm/core/animation.d.ts.map +1 -0
- package/esm/core/animation.js +93 -0
- package/esm/core/force.d.ts +43 -0
- package/esm/core/force.d.ts.map +1 -0
- package/esm/core/force.js +134 -0
- package/esm/core/format.d.ts +34 -0
- package/esm/core/format.d.ts.map +1 -0
- package/esm/core/format.js +81 -0
- package/esm/core/hierarchy.d.ts +77 -0
- package/esm/core/hierarchy.d.ts.map +1 -0
- package/esm/core/hierarchy.js +233 -0
- package/esm/core/layout.d.ts +67 -0
- package/esm/core/layout.d.ts.map +1 -0
- package/esm/core/layout.js +121 -0
- package/esm/core/sampling.d.ts +32 -0
- package/esm/core/sampling.d.ts.map +1 -0
- package/esm/core/sampling.js +124 -0
- package/esm/core/sankey.d.ts +67 -0
- package/esm/core/sankey.d.ts.map +1 -0
- package/esm/core/sankey.js +136 -0
- package/esm/core/scale.d.ts +98 -0
- package/esm/core/scale.d.ts.map +1 -0
- package/esm/core/scale.js +207 -0
- package/esm/core/shape.d.ts +60 -0
- package/esm/core/shape.d.ts.map +1 -0
- package/esm/core/shape.js +246 -0
- package/esm/global.d.ts +6 -0
- package/esm/hooks/useChartTheme.d.ts +11 -0
- package/esm/hooks/useChartTheme.d.ts.map +1 -0
- package/esm/hooks/useChartTheme.js +35 -0
- package/esm/hooks/useChartTransition.d.ts +20 -0
- package/esm/hooks/useChartTransition.d.ts.map +1 -0
- package/esm/hooks/useChartTransition.js +120 -0
- package/esm/hooks/useElementSize.d.ts +17 -0
- package/esm/hooks/useElementSize.d.ts.map +1 -0
- package/esm/hooks/useElementSize.js +47 -0
- package/esm/index.d.ts +62 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +48 -0
- package/esm/internal/CartesianChart.d.ts +16 -0
- package/esm/internal/CartesianChart.d.ts.map +1 -0
- package/esm/internal/CartesianChart.js +811 -0
- package/esm/internal/model.d.ts +67 -0
- package/esm/internal/model.d.ts.map +1 -0
- package/esm/internal/model.js +146 -0
- package/esm/scene/canvas.d.ts +18 -0
- package/esm/scene/canvas.d.ts.map +1 -0
- package/esm/scene/canvas.js +138 -0
- package/esm/scene/svg.d.ts +12 -0
- package/esm/scene/svg.d.ts.map +1 -0
- package/esm/scene/svg.js +116 -0
- package/esm/scene/types.d.ts +89 -0
- package/esm/scene/types.d.ts.map +1 -0
- package/esm/scene/types.js +21 -0
- package/esm/style.css +196 -0
- package/esm/theme/index.d.ts +127 -0
- package/esm/theme/index.d.ts.map +1 -0
- package/esm/theme/index.js +300 -0
- package/esm/types.d.ts +168 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +2 -0
- package/package.json +64 -3
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数值与文本格式化。
|
|
3
|
+
*
|
|
4
|
+
* 图表里最容易出「廉价感」的地方就是刻度:`12000` 不如 `1.2万`,
|
|
5
|
+
* `0.30000000000000004` 更要命。这里统一收口。
|
|
6
|
+
*/
|
|
7
|
+
export interface NumberFormatOptions {
|
|
8
|
+
/** 最多保留几位小数,默认按步长自动推断 */
|
|
9
|
+
decimals?: number;
|
|
10
|
+
/** 是否使用紧凑单位(万 / 亿),默认 false */
|
|
11
|
+
compact?: boolean;
|
|
12
|
+
/** 前缀 / 后缀,如 % 或 元 */
|
|
13
|
+
prefix?: string;
|
|
14
|
+
suffix?: string;
|
|
15
|
+
/** 千分位,默认 true */
|
|
16
|
+
thousands?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** 根据数值量级推断合适的小数位 */
|
|
19
|
+
export declare function inferDecimals(values: readonly number[]): number;
|
|
20
|
+
/** 紧凑单位:中文场景用「万 / 亿」,符合国内看板习惯 */
|
|
21
|
+
export declare function formatCompact(value: number, decimals?: number): string;
|
|
22
|
+
export declare function formatNumber(value: number, options?: NumberFormatOptions): string;
|
|
23
|
+
export declare function formatPercent(value: number, total: number, decimals?: number): string;
|
|
24
|
+
/**
|
|
25
|
+
* 粗略估算文本像素宽度。
|
|
26
|
+
*
|
|
27
|
+
* 为什么要估算而不是真实测量:布局阶段(计算 margin、判断标签是否重叠)
|
|
28
|
+
* 发生在渲染之前,此时没有 DOM 可测量。用「CJK 记 1em、其余记 0.58em」
|
|
29
|
+
* 的经验值足够可靠,误差被 8px 的留白吸收。
|
|
30
|
+
*/
|
|
31
|
+
export declare function estimateTextWidth(text: string, fontSize: number): number;
|
|
32
|
+
/** 数值补零宽度,用于让刻度标签右对齐时视觉整齐 */
|
|
33
|
+
export declare function padNumberLabels(labels: string[]): string[];
|
|
34
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["format.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,mBAAmB;IAClC,yBAAyB;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+BAA+B;IAC/B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,sBAAsB;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAOD,qBAAqB;AACrB,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAS/D;AAED,iCAAiC;AACjC,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAI,GAAG,MAAM,CAOjE;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,mBAAwB,GAChC,MAAM,CAsBR;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,QAAQ,SAAI,GACX,MAAM,CAGR;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAcxE;AAED,6BAA6B;AAC7B,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAM1D"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* 数值与文本格式化。
|
|
4
|
+
*
|
|
5
|
+
* 图表里最容易出「廉价感」的地方就是刻度:`12000` 不如 `1.2万`,
|
|
6
|
+
* `0.30000000000000004` 更要命。这里统一收口。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** 去掉小数末尾多余的 0 */
|
|
10
|
+
function trimZeros(text) {
|
|
11
|
+
return text.includes('.') ? text.replace(/\.?0+$/, '') : text;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 根据数值量级推断合适的小数位 */
|
|
15
|
+
export function inferDecimals(values) {
|
|
16
|
+
let decimals = 0;
|
|
17
|
+
for (const value of values) {
|
|
18
|
+
if (!Number.isFinite(value)) continue;
|
|
19
|
+
const text = String(value);
|
|
20
|
+
const dot = text.indexOf('.');
|
|
21
|
+
if (dot >= 0) decimals = Math.max(decimals, text.length - dot - 1);
|
|
22
|
+
}
|
|
23
|
+
return Math.min(decimals, 4);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 紧凑单位:中文场景用「万 / 亿」,符合国内看板习惯 */
|
|
27
|
+
export function formatCompact(value, decimals = 1) {
|
|
28
|
+
const abs = Math.abs(value);
|
|
29
|
+
const sign = value < 0 ? '-' : '';
|
|
30
|
+
if (abs >= 1e8) return `${sign}${trimZeros((abs / 1e8).toFixed(decimals))}亿`;
|
|
31
|
+
if (abs >= 1e4) return `${sign}${trimZeros((abs / 1e4).toFixed(decimals))}万`;
|
|
32
|
+
if (abs >= 1e3) return `${sign}${trimZeros((abs / 1e3).toFixed(decimals))}k`;
|
|
33
|
+
return `${sign}${trimZeros(String(abs))}`;
|
|
34
|
+
}
|
|
35
|
+
export function formatNumber(value, options = {}) {
|
|
36
|
+
if (!Number.isFinite(value)) return '—';
|
|
37
|
+
const {
|
|
38
|
+
decimals,
|
|
39
|
+
compact = false,
|
|
40
|
+
prefix = '',
|
|
41
|
+
suffix = '',
|
|
42
|
+
thousands = true
|
|
43
|
+
} = options;
|
|
44
|
+
if (compact) return `${prefix}${formatCompact(value, decimals ?? 1)}${suffix}`;
|
|
45
|
+
const fixed = decimals === undefined ? trimZeros(String(value)) : value.toFixed(decimals);
|
|
46
|
+
if (!thousands) return `${prefix}${fixed}${suffix}`;
|
|
47
|
+
const [intPart, decimalPart] = fixed.split('.');
|
|
48
|
+
const withSeparator = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
49
|
+
return `${prefix}${decimalPart ? `${withSeparator}.${decimalPart}` : withSeparator}${suffix}`;
|
|
50
|
+
}
|
|
51
|
+
export function formatPercent(value, total, decimals = 1) {
|
|
52
|
+
if (!total) return '0%';
|
|
53
|
+
return `${trimZeros((value / total * 100).toFixed(decimals))}%`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 粗略估算文本像素宽度。
|
|
58
|
+
*
|
|
59
|
+
* 为什么要估算而不是真实测量:布局阶段(计算 margin、判断标签是否重叠)
|
|
60
|
+
* 发生在渲染之前,此时没有 DOM 可测量。用「CJK 记 1em、其余记 0.58em」
|
|
61
|
+
* 的经验值足够可靠,误差被 8px 的留白吸收。
|
|
62
|
+
*/
|
|
63
|
+
export function estimateTextWidth(text, fontSize) {
|
|
64
|
+
let width = 0;
|
|
65
|
+
for (const char of text) {
|
|
66
|
+
const code = char.codePointAt(0) ?? 0;
|
|
67
|
+
// CJK 统一表意文字、全角标点、假名、韩文
|
|
68
|
+
const isWide = code >= 0x2e80 && code <= 0x9fff || code >= 0xac00 && code <= 0xd7af || code >= 0xf900 && code <= 0xfaff || code >= 0xff00 && code <= 0xff60 || code >= 0xffe0 && code <= 0xffe6;
|
|
69
|
+
width += isWide ? fontSize : fontSize * 0.58;
|
|
70
|
+
}
|
|
71
|
+
return width;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 数值补零宽度,用于让刻度标签右对齐时视觉整齐 */
|
|
75
|
+
export function padNumberLabels(labels) {
|
|
76
|
+
const width = labels.reduce((max, item) => Math.max(max, item.length), 0);
|
|
77
|
+
if (width === 0) return labels;
|
|
78
|
+
// 只在全部为纯数字(含符号/千分位)时补零,避免把「1月」也补成「01月」
|
|
79
|
+
if (!labels.every(item => /^-?[\d,.]+$/.test(item))) return labels;
|
|
80
|
+
return labels.map(item => item.padStart(width, ' '));
|
|
81
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ChartTheme } from '../theme/index.js';
|
|
2
|
+
/** 层级数据:叶子节点用 value,分支节点可不填(由子节点求和) */
|
|
3
|
+
export interface HierarchyNode {
|
|
4
|
+
name: string;
|
|
5
|
+
value?: number;
|
|
6
|
+
children?: HierarchyNode[];
|
|
7
|
+
color?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Rect {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}
|
|
15
|
+
/** 递归求和:分支节点取子节点之和,叶子取自身 value */
|
|
16
|
+
export declare function sumValues(node: HierarchyNode): number;
|
|
17
|
+
/** 叶子总数,用于树图的纵向排布 */
|
|
18
|
+
export declare function countLeaves(node: HierarchyNode): number;
|
|
19
|
+
/** 树的最大深度(根为 0) */
|
|
20
|
+
export declare function maxDepth(node: HierarchyNode, depth?: number): number;
|
|
21
|
+
export interface TreemapCell extends Rect {
|
|
22
|
+
node: HierarchyNode;
|
|
23
|
+
/** 层级深度,根的直接子节点为 0 */
|
|
24
|
+
depth: number;
|
|
25
|
+
/** 归一化数值(0~1),用于颜色深浅 */
|
|
26
|
+
ratio: number;
|
|
27
|
+
/** 该格最终使用的颜色(顶层取色板,子层继承父色的浅化版) */
|
|
28
|
+
color: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Squarified Treemap 布局(Bruls, Huizing & van Wijk, 2000)。
|
|
32
|
+
*
|
|
33
|
+
* 为什么不用「交替横竖切」(slice-and-dice):那个算法会产生极端细长的矩形,
|
|
34
|
+
* 面积虽然对得上,但细条既放不下文字、也难以目测比较。
|
|
35
|
+
* squarified 通过逐行控制最差宽高比,让每块尽量接近正方形。
|
|
36
|
+
*/
|
|
37
|
+
export declare function squarify(values: number[], rect: Rect): Rect[];
|
|
38
|
+
export interface TreemapOptions {
|
|
39
|
+
/** 最多展开到第几层(0 表示只画根的直接子节点) */
|
|
40
|
+
maxDepth?: number;
|
|
41
|
+
theme: ChartTheme;
|
|
42
|
+
/**
|
|
43
|
+
* 取色函数。depth 为 0 时返回顶层颜色;
|
|
44
|
+
* 更深层通常返回父色的浅化版本。
|
|
45
|
+
*/
|
|
46
|
+
colorOf?: (depth: number, index: number, parentColor?: string) => string;
|
|
47
|
+
/**
|
|
48
|
+
* 会继续展开的格子,在顶部预留的标题条高度。
|
|
49
|
+
* 不留这条,父级名称会被子块直接盖住。
|
|
50
|
+
*/
|
|
51
|
+
headerHeight?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 递归铺满:先对根的直接子节点做 squarify,再在每个矩形内部继续切分。
|
|
55
|
+
*/
|
|
56
|
+
export declare function buildTreemap(root: HierarchyNode, rect: Rect, options: TreemapOptions): TreemapCell[];
|
|
57
|
+
export interface TreeLayoutNode {
|
|
58
|
+
node: HierarchyNode;
|
|
59
|
+
depth: number;
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
/** 父节点在返回数组中的下标,根为 -1 */
|
|
63
|
+
parent: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 树图布局:横向展开(深度 → x),纵向按叶子顺序均分。
|
|
67
|
+
*
|
|
68
|
+
* 父节点的 y 取子节点 y 的均值,这样连线不会交叉,
|
|
69
|
+
* 也让「同一父节点下的兄弟」在视觉上聚成一簇。
|
|
70
|
+
*/
|
|
71
|
+
export declare function layoutTree(root: HierarchyNode, rect: Rect): {
|
|
72
|
+
nodes: TreeLayoutNode[];
|
|
73
|
+
depth: number;
|
|
74
|
+
};
|
|
75
|
+
/** 树图连线:正交折线(父 → 子),比贝塞尔更清晰地表达层级 */
|
|
76
|
+
export declare function treeLinkPath(parent: TreeLayoutNode, child: TreeLayoutNode, nodeWidth: number): string;
|
|
77
|
+
//# sourceMappingURL=hierarchy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hierarchy.d.ts","sourceRoot":"","sources":["hierarchy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,uCAAuC;AACvC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,IAAI;IACnB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,kCAAkC;AAClC,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM,CAKrD;AAED,qBAAqB;AACrB,wBAAgB,WAAW,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM,CAGvD;AAED,mBAAmB;AACnB,wBAAgB,QAAQ,CAAC,IAAI,EAAE,aAAa,EAAE,KAAK,SAAI,GAAG,MAAM,CAG/D;AAED,MAAM,WAAW,WAAY,SAAQ,IAAI;IACvC,IAAI,EAAE,aAAa,CAAC;IACpB,sBAAsB;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,wBAAwB;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,CAgG7D;AAuBD,MAAM,WAAW,cAAc;IAC7B,8BAA8B;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,UAAU,CAAC;IAClB;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IACzE;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,cAAc,GACtB,WAAW,EAAE,CA8Df;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,yBAAyB;IACzB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,IAAI,GACT;IAAE,KAAK,EAAE,cAAc,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CA+B5C;AAED,oCAAoC;AACpC,wBAAgB,YAAY,CAC1B,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,cAAc,EACrB,SAAS,EAAE,MAAM,GAChB,MAAM,CAOR"}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/** 层级数据:叶子节点用 value,分支节点可不填(由子节点求和) */
|
|
3
|
+
|
|
4
|
+
/** 递归求和:分支节点取子节点之和,叶子取自身 value */
|
|
5
|
+
export function sumValues(node) {
|
|
6
|
+
if (node.children?.length) {
|
|
7
|
+
return node.children.reduce((sum, child) => sum + sumValues(child), 0);
|
|
8
|
+
}
|
|
9
|
+
return Math.max(0, node.value ?? 0);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** 叶子总数,用于树图的纵向排布 */
|
|
13
|
+
export function countLeaves(node) {
|
|
14
|
+
if (!node.children?.length) return 1;
|
|
15
|
+
return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 树的最大深度(根为 0) */
|
|
19
|
+
export function maxDepth(node, depth = 0) {
|
|
20
|
+
if (!node.children?.length) return depth;
|
|
21
|
+
return Math.max(...node.children.map(child => maxDepth(child, depth + 1)));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Squarified Treemap 布局(Bruls, Huizing & van Wijk, 2000)。
|
|
25
|
+
*
|
|
26
|
+
* 为什么不用「交替横竖切」(slice-and-dice):那个算法会产生极端细长的矩形,
|
|
27
|
+
* 面积虽然对得上,但细条既放不下文字、也难以目测比较。
|
|
28
|
+
* squarified 通过逐行控制最差宽高比,让每块尽量接近正方形。
|
|
29
|
+
*/
|
|
30
|
+
export function squarify(values, rect) {
|
|
31
|
+
const total = values.reduce((sum, value) => sum + value, 0);
|
|
32
|
+
if (total <= 0 || rect.width <= 0 || rect.height <= 0) {
|
|
33
|
+
return values.map(() => ({
|
|
34
|
+
x: rect.x,
|
|
35
|
+
y: rect.y,
|
|
36
|
+
width: 0,
|
|
37
|
+
height: 0
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 按面积降序,squarified 的前提是大块先摆
|
|
42
|
+
const order = values.map((value, index) => ({
|
|
43
|
+
value,
|
|
44
|
+
index
|
|
45
|
+
})).sort((a, b) => b.value - a.value);
|
|
46
|
+
const result = values.map(() => ({
|
|
47
|
+
x: 0,
|
|
48
|
+
y: 0,
|
|
49
|
+
width: 0,
|
|
50
|
+
height: 0
|
|
51
|
+
}));
|
|
52
|
+
const area = rect.width * rect.height;
|
|
53
|
+
const scale = area / total;
|
|
54
|
+
let free = {
|
|
55
|
+
...rect
|
|
56
|
+
};
|
|
57
|
+
let cursor = 0;
|
|
58
|
+
while (cursor < order.length) {
|
|
59
|
+
const shortSide = Math.min(free.width, free.height);
|
|
60
|
+
if (shortSide <= 0) break;
|
|
61
|
+
|
|
62
|
+
// 贪心地把当前行填到「宽高比不再变好」为止
|
|
63
|
+
const row = [];
|
|
64
|
+
let rowSum = 0;
|
|
65
|
+
let bestRatio = Number.POSITIVE_INFINITY;
|
|
66
|
+
while (cursor < order.length) {
|
|
67
|
+
const candidate = order[cursor];
|
|
68
|
+
const nextSum = rowSum + candidate.value;
|
|
69
|
+
const ratio = worstAspectRatio([...row.map(item => item.value), candidate.value], nextSum * scale, shortSide);
|
|
70
|
+
if (row.length === 0 || ratio <= bestRatio) {
|
|
71
|
+
row.push(candidate);
|
|
72
|
+
rowSum = nextSum;
|
|
73
|
+
bestRatio = ratio;
|
|
74
|
+
cursor += 1;
|
|
75
|
+
} else {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 摆放这一行
|
|
81
|
+
const rowArea = rowSum * scale;
|
|
82
|
+
const horizontal = free.width >= free.height;
|
|
83
|
+
// 沿短边铺:短边是高时竖着排,短边是宽时横着排
|
|
84
|
+
const thickness = rowArea / Math.max(1e-6, horizontal ? free.height : free.width);
|
|
85
|
+
let offset = 0;
|
|
86
|
+
for (const item of row) {
|
|
87
|
+
const length = item.value * scale / Math.max(1e-6, thickness);
|
|
88
|
+
if (horizontal) {
|
|
89
|
+
result[item.index] = {
|
|
90
|
+
x: free.x,
|
|
91
|
+
y: free.y + offset,
|
|
92
|
+
width: thickness,
|
|
93
|
+
height: length
|
|
94
|
+
};
|
|
95
|
+
} else {
|
|
96
|
+
result[item.index] = {
|
|
97
|
+
x: free.x + offset,
|
|
98
|
+
y: free.y,
|
|
99
|
+
width: length,
|
|
100
|
+
height: thickness
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
offset += length;
|
|
104
|
+
}
|
|
105
|
+
if (horizontal) {
|
|
106
|
+
free = {
|
|
107
|
+
x: free.x + thickness,
|
|
108
|
+
y: free.y,
|
|
109
|
+
width: Math.max(0, free.width - thickness),
|
|
110
|
+
height: free.height
|
|
111
|
+
};
|
|
112
|
+
} else {
|
|
113
|
+
free = {
|
|
114
|
+
x: free.x,
|
|
115
|
+
y: free.y + thickness,
|
|
116
|
+
width: free.width,
|
|
117
|
+
height: Math.max(0, free.height - thickness)
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 一行的最差宽高比。
|
|
126
|
+
*
|
|
127
|
+
* 公式来自原论文:给定行内各块面积、该行总面积与所在边的长度,
|
|
128
|
+
* 返回值越接近 1,矩形越接近正方形。
|
|
129
|
+
*/
|
|
130
|
+
function worstAspectRatio(row, rowArea, side) {
|
|
131
|
+
if (rowArea <= 0 || side <= 0) return Number.POSITIVE_INFINITY;
|
|
132
|
+
const max = Math.max(...row);
|
|
133
|
+
const min = Math.min(...row);
|
|
134
|
+
if (min <= 0) return Number.POSITIVE_INFINITY;
|
|
135
|
+
return Math.max(side * side * max / (rowArea * rowArea), rowArea * rowArea / (side * side * min));
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 递归铺满:先对根的直接子节点做 squarify,再在每个矩形内部继续切分。
|
|
139
|
+
*/
|
|
140
|
+
export function buildTreemap(root, rect, options) {
|
|
141
|
+
const {
|
|
142
|
+
maxDepth: depthLimit = 1,
|
|
143
|
+
theme,
|
|
144
|
+
colorOf,
|
|
145
|
+
headerHeight = 0
|
|
146
|
+
} = options;
|
|
147
|
+
const cells = [];
|
|
148
|
+
const total = sumValues(root) || 1;
|
|
149
|
+
const walk = (node, area, depth, parentColor) => {
|
|
150
|
+
const children = node.children ?? [];
|
|
151
|
+
// depth 是「本层要渲染的层级」:maxDepth=1 时只渲染 depth 0
|
|
152
|
+
if (!children.length || depth >= depthLimit) return;
|
|
153
|
+
const values = children.map(sumValues);
|
|
154
|
+
const rects = squarify(values, area);
|
|
155
|
+
children.forEach((child, index) => {
|
|
156
|
+
const box = rects[index];
|
|
157
|
+
if (box.width <= 0 || box.height <= 0) return;
|
|
158
|
+
const color = child.color ?? colorOf?.(depth, index, parentColor) ?? theme.palette[index % theme.palette.length];
|
|
159
|
+
cells.push({
|
|
160
|
+
node: child,
|
|
161
|
+
depth,
|
|
162
|
+
x: box.x,
|
|
163
|
+
y: box.y,
|
|
164
|
+
width: box.width,
|
|
165
|
+
height: box.height,
|
|
166
|
+
ratio: Math.min(1, sumValues(child) / total),
|
|
167
|
+
color
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// 该格还会继续展开时,把子块的区域向下让出标题条
|
|
171
|
+
const willExpand = Boolean(child.children?.length) && depth + 1 < depthLimit;
|
|
172
|
+
const inner = willExpand && headerHeight > 0 && box.height > headerHeight + 24 ? {
|
|
173
|
+
x: box.x,
|
|
174
|
+
y: box.y + headerHeight,
|
|
175
|
+
width: box.width,
|
|
176
|
+
height: box.height - headerHeight
|
|
177
|
+
} : box;
|
|
178
|
+
walk(child, inner, depth + 1, color);
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
walk(root, rect, 0);
|
|
182
|
+
return cells;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 树图布局:横向展开(深度 → x),纵向按叶子顺序均分。
|
|
186
|
+
*
|
|
187
|
+
* 父节点的 y 取子节点 y 的均值,这样连线不会交叉,
|
|
188
|
+
* 也让「同一父节点下的兄弟」在视觉上聚成一簇。
|
|
189
|
+
*/
|
|
190
|
+
export function layoutTree(root, rect) {
|
|
191
|
+
const depth = Math.max(1, maxDepth(root));
|
|
192
|
+
const leaves = Math.max(1, countLeaves(root));
|
|
193
|
+
const rowHeight = rect.height / leaves;
|
|
194
|
+
const columnWidth = depth > 0 ? rect.width / depth : rect.width;
|
|
195
|
+
const nodes = [];
|
|
196
|
+
let leafCursor = 0;
|
|
197
|
+
const walk = (node, level, parent) => {
|
|
198
|
+
const index = nodes.length;
|
|
199
|
+
const x = rect.x + level * columnWidth;
|
|
200
|
+
nodes.push({
|
|
201
|
+
node,
|
|
202
|
+
depth: level,
|
|
203
|
+
x,
|
|
204
|
+
y: 0,
|
|
205
|
+
parent
|
|
206
|
+
});
|
|
207
|
+
if (!node.children?.length) {
|
|
208
|
+
const y = rect.y + leafCursor * rowHeight + rowHeight / 2;
|
|
209
|
+
leafCursor += 1;
|
|
210
|
+
nodes[index].y = y;
|
|
211
|
+
return y;
|
|
212
|
+
}
|
|
213
|
+
const childYs = node.children.map(child => walk(child, level + 1, index));
|
|
214
|
+
const y = childYs.reduce((sum, value) => sum + value, 0) / Math.max(1, childYs.length);
|
|
215
|
+
nodes[index].y = y;
|
|
216
|
+
return y;
|
|
217
|
+
};
|
|
218
|
+
walk(root, 0, -1);
|
|
219
|
+
return {
|
|
220
|
+
nodes,
|
|
221
|
+
depth
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** 树图连线:正交折线(父 → 子),比贝塞尔更清晰地表达层级 */
|
|
226
|
+
export function treeLinkPath(parent, child, nodeWidth) {
|
|
227
|
+
const x1 = parent.x + nodeWidth;
|
|
228
|
+
const y1 = parent.y;
|
|
229
|
+
const x2 = child.x;
|
|
230
|
+
const y2 = child.y;
|
|
231
|
+
const midX = x1 + (x2 - x1) / 2;
|
|
232
|
+
return `M${x1},${y1}H${midX}V${y2}H${x2}`;
|
|
233
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ChartTheme } from '../theme/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* 布局计算:把「容器尺寸」换算成「绘图区」。
|
|
4
|
+
*
|
|
5
|
+
* 难点在于坐标轴文字会占用边缘空间,而可用的边缘空间又取决于容器大小 ——
|
|
6
|
+
* 这里采用业界通行的两遍法:
|
|
7
|
+
* 第一遍用完整容器高度估算刻度数量 → 得到刻度标签;
|
|
8
|
+
* 第二遍按标签实际宽度(估算)反推 margin。
|
|
9
|
+
* 两遍都是纯计算,不触碰 DOM,因此可在 SSR 与测试中直接跑。
|
|
10
|
+
*/
|
|
11
|
+
export interface Margin {
|
|
12
|
+
top: number;
|
|
13
|
+
right: number;
|
|
14
|
+
bottom: number;
|
|
15
|
+
left: number;
|
|
16
|
+
}
|
|
17
|
+
export interface PlotArea {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}
|
|
23
|
+
/** x 轴标签的排布方式 */
|
|
24
|
+
export type LabelLayout = 'horizontal' | 'rotate' | 'hide';
|
|
25
|
+
export interface XAxisLayout {
|
|
26
|
+
layout: LabelLayout;
|
|
27
|
+
/** 旋转角度(负值为逆时针) */
|
|
28
|
+
rotate: number;
|
|
29
|
+
/** 字号 */
|
|
30
|
+
fontSize: number;
|
|
31
|
+
/** 该轴需要占用的底部空间 */
|
|
32
|
+
height: number;
|
|
33
|
+
/** 每隔几个标签显示一个,0 表示全部显示 */
|
|
34
|
+
interval: number;
|
|
35
|
+
}
|
|
36
|
+
export interface ComputeLayoutInput {
|
|
37
|
+
width: number;
|
|
38
|
+
height: number;
|
|
39
|
+
/** y 轴刻度标签文本 */
|
|
40
|
+
yLabels: string[];
|
|
41
|
+
/** x 轴刻度标签文本 */
|
|
42
|
+
xLabels: string[];
|
|
43
|
+
theme: ChartTheme;
|
|
44
|
+
/** 是否显示 x 轴,false 时不预留底部空间 */
|
|
45
|
+
showXAxis?: boolean;
|
|
46
|
+
showYAxis?: boolean;
|
|
47
|
+
/** 用户强制指定的 margin 覆盖项 */
|
|
48
|
+
margin?: Partial<Margin>;
|
|
49
|
+
}
|
|
50
|
+
export interface LayoutResult {
|
|
51
|
+
plot: PlotArea;
|
|
52
|
+
margin: Margin;
|
|
53
|
+
xAxis: XAxisLayout;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 计算 x 轴标签布局。
|
|
57
|
+
*
|
|
58
|
+
* 判定顺序:
|
|
59
|
+
* 1. 全部横排放得下 → horizontal
|
|
60
|
+
* 2. 斜排 35° 放得下 → rotate
|
|
61
|
+
* 3. 都不行 → 抽样显示(interval),强制横排,避免出现密集的"斜线森林"
|
|
62
|
+
*/
|
|
63
|
+
export declare function computeXAxisLayout(labels: readonly string[], availableWidth: number, theme: ChartTheme): XAxisLayout;
|
|
64
|
+
export declare function computeLayout(input: ComputeLayoutInput): LayoutResult;
|
|
65
|
+
/** 数值轴刻度数量的经验值:约每 44px 一个刻度,限制在 3~8 之间 */
|
|
66
|
+
export declare function suggestTickCount(plotHeight: number): number;
|
|
67
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["layout.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C;;;;;;;;GAQG;AAEH,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,iBAAiB;AACjB,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE3D,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,WAAW,CAAC;IACpB,mBAAmB;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS;IACT,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,0BAA0B;IAC1B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,gBAAgB;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,gBAAgB;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,EAAE,UAAU,CAAC;IAClB,8BAA8B;IAC9B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,yBAAyB;IACzB,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,WAAW,CAAC;CACpB;AAID;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,cAAc,EAAE,MAAM,EACtB,KAAK,EAAE,UAAU,GAChB,WAAW,CAwDb;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,YAAY,CAoDrE;AAED,0CAA0C;AAC1C,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAE3D"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { estimateTextWidth } from "./format";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 布局计算:把「容器尺寸」换算成「绘图区」。
|
|
6
|
+
*
|
|
7
|
+
* 难点在于坐标轴文字会占用边缘空间,而可用的边缘空间又取决于容器大小 ——
|
|
8
|
+
* 这里采用业界通行的两遍法:
|
|
9
|
+
* 第一遍用完整容器高度估算刻度数量 → 得到刻度标签;
|
|
10
|
+
* 第二遍按标签实际宽度(估算)反推 margin。
|
|
11
|
+
* 两遍都是纯计算,不触碰 DOM,因此可在 SSR 与测试中直接跑。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** x 轴标签的排布方式 */
|
|
15
|
+
|
|
16
|
+
const AXIS_GAP = 8;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 计算 x 轴标签布局。
|
|
20
|
+
*
|
|
21
|
+
* 判定顺序:
|
|
22
|
+
* 1. 全部横排放得下 → horizontal
|
|
23
|
+
* 2. 斜排 35° 放得下 → rotate
|
|
24
|
+
* 3. 都不行 → 抽样显示(interval),强制横排,避免出现密集的"斜线森林"
|
|
25
|
+
*/
|
|
26
|
+
export function computeXAxisLayout(labels, availableWidth, theme) {
|
|
27
|
+
const fontSize = theme.font.size;
|
|
28
|
+
const count = labels.length;
|
|
29
|
+
if (count === 0) {
|
|
30
|
+
return {
|
|
31
|
+
layout: 'horizontal',
|
|
32
|
+
rotate: 0,
|
|
33
|
+
fontSize,
|
|
34
|
+
height: 0,
|
|
35
|
+
interval: 0
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const widths = labels.map(label => estimateTextWidth(label, fontSize));
|
|
39
|
+
const maxWidth = Math.max(...widths);
|
|
40
|
+
const slot = availableWidth / count;
|
|
41
|
+
if (maxWidth + 12 <= slot) {
|
|
42
|
+
return {
|
|
43
|
+
layout: 'horizontal',
|
|
44
|
+
rotate: 0,
|
|
45
|
+
fontSize,
|
|
46
|
+
height: fontSize + AXIS_GAP,
|
|
47
|
+
interval: 0
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const rotate = -35;
|
|
51
|
+
const rad = Math.abs(rotate) * Math.PI / 180;
|
|
52
|
+
const rotatedWidth = maxWidth * Math.cos(rad) + fontSize * Math.sin(rad);
|
|
53
|
+
if (rotatedWidth + 4 <= slot) {
|
|
54
|
+
const height = Math.ceil(maxWidth * Math.sin(rad) + fontSize * Math.cos(rad)) + AXIS_GAP;
|
|
55
|
+
return {
|
|
56
|
+
layout: 'rotate',
|
|
57
|
+
rotate,
|
|
58
|
+
fontSize,
|
|
59
|
+
height: Math.min(height, 72),
|
|
60
|
+
interval: 0
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 抽样:至少保证标签间距不小于 1.6 个字符宽
|
|
65
|
+
const interval = Math.max(1, Math.ceil(count / Math.max(1, Math.floor(availableWidth / (maxWidth + 12)))));
|
|
66
|
+
return {
|
|
67
|
+
layout: 'horizontal',
|
|
68
|
+
rotate: 0,
|
|
69
|
+
fontSize,
|
|
70
|
+
height: fontSize + AXIS_GAP,
|
|
71
|
+
interval
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export function computeLayout(input) {
|
|
75
|
+
const {
|
|
76
|
+
width,
|
|
77
|
+
height,
|
|
78
|
+
yLabels,
|
|
79
|
+
xLabels,
|
|
80
|
+
theme,
|
|
81
|
+
showXAxis = true,
|
|
82
|
+
showYAxis = true
|
|
83
|
+
} = input;
|
|
84
|
+
const maxYLabelWidth = yLabels.length ? Math.max(...yLabels.map(label => estimateTextWidth(label, theme.font.size))) : 0;
|
|
85
|
+
const left = showYAxis ? Math.ceil(maxYLabelWidth + theme.axis.tickSize + AXIS_GAP) : 12;
|
|
86
|
+
const right = 16;
|
|
87
|
+
|
|
88
|
+
// 第一遍:先按横向标签预留底部空间,得出可用宽度
|
|
89
|
+
const provisionalPlotWidth = Math.max(0, width - left - right);
|
|
90
|
+
const xAxis = showXAxis ? computeXAxisLayout(xLabels, provisionalPlotWidth, theme) : {
|
|
91
|
+
layout: 'horizontal',
|
|
92
|
+
rotate: 0,
|
|
93
|
+
fontSize: theme.font.size,
|
|
94
|
+
height: 0,
|
|
95
|
+
interval: 0
|
|
96
|
+
};
|
|
97
|
+
const bottom = showXAxis ? xAxis.height + 4 : 12;
|
|
98
|
+
const top = 12;
|
|
99
|
+
const margin = {
|
|
100
|
+
top: input.margin?.top ?? top,
|
|
101
|
+
right: input.margin?.right ?? right,
|
|
102
|
+
bottom: input.margin?.bottom ?? bottom,
|
|
103
|
+
left: input.margin?.left ?? left
|
|
104
|
+
};
|
|
105
|
+
const plot = {
|
|
106
|
+
x: margin.left,
|
|
107
|
+
y: margin.top,
|
|
108
|
+
width: Math.max(0, width - margin.left - margin.right),
|
|
109
|
+
height: Math.max(0, height - margin.top - margin.bottom)
|
|
110
|
+
};
|
|
111
|
+
return {
|
|
112
|
+
plot,
|
|
113
|
+
margin,
|
|
114
|
+
xAxis
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 数值轴刻度数量的经验值:约每 44px 一个刻度,限制在 3~8 之间 */
|
|
119
|
+
export function suggestTickCount(plotHeight) {
|
|
120
|
+
return Math.min(8, Math.max(3, Math.round(plotHeight / 44)));
|
|
121
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 降采样。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:数据点超过几百个后,屏幕像素已经不够区分相邻点了,
|
|
5
|
+
* 继续逐点渲染只会拖慢滚动而不会增加信息量。
|
|
6
|
+
* 但「每隔 N 个取一个」会丢掉尖峰——那些恰恰是读者最关心的。
|
|
7
|
+
*/
|
|
8
|
+
export interface SamplePoint {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number | null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Largest-Triangle-Three-Buckets(LTTB)。
|
|
14
|
+
*
|
|
15
|
+
* 把数据切成 threshold 个桶,每个桶里选一个点,使得它与前后选中点
|
|
16
|
+
* 构成的三角形面积最大——面积大意味着这个点「离直线最远」,
|
|
17
|
+
* 也就是最能代表该段形状特征的极值点。
|
|
18
|
+
*
|
|
19
|
+
* 复杂度 O(n),且能保住尖峰,是目前综合表现最好的保形降采样之一。
|
|
20
|
+
*
|
|
21
|
+
* @returns 选中点的原始下标(升序),首尾必选
|
|
22
|
+
*/
|
|
23
|
+
export declare function lttbIndices(points: readonly SamplePoint[], threshold: number): number[];
|
|
24
|
+
/**
|
|
25
|
+
* 对「多系列共享 x」的数据做采样。
|
|
26
|
+
*
|
|
27
|
+
* 关键点:**必须按 x 下标采样一次,再让所有系列共用这套下标**。
|
|
28
|
+
* 每个系列各自采样会得到不同的下标集合,折线之间就会错位,
|
|
29
|
+
* 看起来像两列数据在互相穿插。
|
|
30
|
+
*/
|
|
31
|
+
export declare function sampleSeriesIndices(values: readonly (number | null)[][], threshold: number): number[];
|
|
32
|
+
//# sourceMappingURL=sampling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sampling.d.ts","sourceRoot":"","sources":["sampling.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,WAAW;IAC1B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAClB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,SAAS,EAAE,MAAM,GAChB,MAAM,EAAE,CAuFV;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,EAAE,EACpC,SAAS,EAAE,MAAM,GAChB,MAAM,EAAE,CAoBV"}
|