sk-chart-duo 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 KTBOY
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 ADDED
@@ -0,0 +1,156 @@
1
+ # sk-chart
2
+
3
+ [![npm version](https://img.shields.io/npm/v/sk-chart-duo.svg)](https://www.npmjs.com/package/sk-chart-duo)
4
+ [![license](https://img.shields.io/npm/l/sk-chart-duo.svg)](./LICENSE)
5
+
6
+ A lightweight, SVG-first chart library with handcrafted visual styles.
7
+
8
+ 首版提供 **FoldBarChart**:折纸漏斗柱状图 —— 渐变柱体由"折面"相连,闲置列呈条纹纸感,高亮列浮起 wash 与 tooltip。源自 `payments-fold-chart.html` 效果 3 的组件化实现。
9
+
10
+ - **零依赖**,gzip ~8KB
11
+ - **SVG 渲染**,viewBox 设计空间,任意容器宽度自适应
12
+ - **G2Plot 风格 API**:`new FoldBarChart(el, config)` + `update` / `resize` / `destroy` / `on`
13
+ - **多实例安全**:defs id 与样式按实例隔离
14
+ - **TypeScript strict**,几何与比例尺全部纯函数 + 单测覆盖
15
+
16
+ ## 安装
17
+
18
+ ```bash
19
+ npm install sk-chart-duo
20
+ ```
21
+
22
+ > npm 包名为 `sk-chart-duo`(`sk-chart` 已被 npm 相似度校验判定与既有包 `skchart` 冲突,无法使用),仓库名仍是 `sk-chart`。
23
+
24
+ 或直接引入构建产物(`dist/index.js` ESM / `dist/index.cjs` CJS)。
25
+
26
+ ## 快速上手
27
+
28
+ ```ts
29
+ import { FoldBarChart } from 'sk-chart-duo';
30
+
31
+ const chart = new FoldBarChart('#container', {
32
+ data: [
33
+ { label: '发起支付', value: 65.2 },
34
+ { label: '授权支付', value: 54.8 },
35
+ { label: '支付成功', value: 48.6 },
36
+ { label: '商户打款', value: 38.3 },
37
+ { label: '完成交易', value: 32.9 },
38
+ ],
39
+ scale: { exponent: 2 }, // 折纸漏斗轮廓;默认 1 = 线性
40
+ state: { defaultActive: 2 }, // 闲置时高亮第 3 列
41
+ title: { text: '支付' },
42
+ });
43
+
44
+ chart.on('column:click', ({ index, datum }) => console.log(index, datum));
45
+ chart.update({ data: nextData }); // 全量重绘
46
+ chart.resize(960, 430); // 变更 viewBox 设计空间
47
+ chart.destroy(); // 清理 DOM 与事件
48
+ ```
49
+
50
+ 本地 demo:
51
+
52
+ ```bash
53
+ npm install
54
+ npm run dev # Vite,打开 examples/
55
+ ```
56
+
57
+ ## API
58
+
59
+ ### `FoldBarChartConfig`
60
+
61
+ | 字段 | 类型 | 默认 | 说明 |
62
+ |------|------|------|------|
63
+ | `data` | `FoldBarDatum[]` | 必填 | `{ label, value, ...extra }` |
64
+ | `xField` / `yField` | `string` | `label` / `value` | 数据字段映射 |
65
+ | `width` / `height` | `number` | `860` / `386` | viewBox 设计空间 |
66
+ | `valueFormat` | `(v) => string` | `v => v.toFixed(1)+'k'` | 柱头数值格式 |
67
+ | `padding` | `Partial<{top,right,bottom,left}>` | `64/29/26/73` | 绘图区留白;启用 `xAxis.bottomLabels` 时底部自动扩高 |
68
+ | `stair` | `{ bottomOffset?, topOffset? }` | `30` / `74` | 柱顶阶梯锚点(value=0 与 max 的柱顶位置) |
69
+ | `scale.exponent` | `number` | `1` | 高度映射幂次;`2` 还原折纸漏斗轮廓 |
70
+ | `fold.run` | `number` | `20` | 折面水平跨度 |
71
+ | `fold.creaseColor/Width` | — | 白 / `1.2` | 折痕高光 |
72
+ | `axis.ticks` | `number[]` | 自动(nice) | y 轴刻度值;位置按 `barTopOf` 真实映射 |
73
+ | `axis.tickFormat` | `(v) => string` | `v => v+'k'` | 刻度文案 |
74
+ | `xAxis.labelFormat` | `(d, i, data) => string` | xField 值 | 顶部类目行文案 |
75
+ | `xAxis.bottomLabels` | `(d, i, data) => string \| string[]` | 无 | 底部语义行(如阶段序号 + 环节转化率),渲染在渐隐遮罩之外 |
76
+ | `xAxis.title` | `{ text?, x?, y? }` | 无 | X 轴标题,位于底部语义行之后的下一行,默认水平居中;`x`/`y` 可覆盖 |
77
+ | `xAxis.showLine/showTick` | `boolean` | `false` | 柱底基线 / 列中心刻度线(均在渐隐带下方) |
78
+ | `xAxis.showGrid` | `boolean` | `true` | 竖直分列线 |
79
+ | `tooltip.enabled` | `boolean` | `true` | |
80
+ | `tooltip.formatter` | `(datum, i, data) => TooltipPart[]` | 类目 + 数值 | 自定义 tooltip 内容 |
81
+ | `tooltip.fixedWidth` | `number` | — | 跳过文字测量(SSR/测试逃生口) |
82
+ | `state.defaultActive` | `number` | 最后一列 | 闲置高亮列 |
83
+ | `title` | `{ text?, x?, y? }` | 无标题 | 左上标题 |
84
+ | `style` | `FoldBarStyleConfig` | 原稿配色 | 条纹/渐变/pill/阴影/渐隐等全部可换肤;`stripePattern.enabled`、`pill.enabled`、`washEnabled`、`fadeMask.enabled` 可逐项关闭装饰,得到干净的普通柱状图观感 |
85
+ | `theme` | `'light' \| 'dark' \| ThemePack \| DeepPartialTokens` | 原稿外观(等价 `'light'`) | 预设名 / 内联主题包 / 旧版字体 token 局部,三种形态均可 |
86
+
87
+ SVG 本身透明、不画背景,底色由宿主页面控制(白底卡片或深色背景均可)。
88
+
89
+ ### 主题预设
90
+
91
+ ```ts
92
+ import { registerTheme, FoldBarChart } from 'sk-chart-duo';
93
+
94
+ registerTheme('brand', {
95
+ style: { barGradient: { normal: myStops } }, // 视觉皮肤
96
+ tokens: { title: { fill: '#0A0A0A' } }, // 字体/颜色/过渡 token
97
+ formats: { valueFormat: (v) => `${v}k` }, // 默认数值/刻度格式化
98
+ });
99
+
100
+ new FoldBarChart(el, { data, theme: 'dark' }); // 内置预设
101
+ new FoldBarChart(el, { data, theme: 'brand' }); // 自定义预设
102
+ new FoldBarChart(el, { data, theme: { tokens: { ... } } }); // 内联主题包
103
+ ```
104
+
105
+ - 内置 `light`(原稿折纸皮肤)与 `dark` 两个预设
106
+ - 解析顺序:内置默认 → 主题包 → config 显式字段(`style`、对象形态 `theme`、`valueFormat`、`axis.tickFormat`),后者优先
107
+ - 旧写法 `theme: { number: { fontSize: 22 } }`(直接传 token 局部)完全兼容
108
+ - 未知预设名回退默认并 `console.warn`
109
+
110
+ ### 事件
111
+
112
+ `column:enter` / `column:leave` / `column:click`,payload 为 `{ index, datum }`。
113
+
114
+ ### 方法
115
+
116
+ `update(partial)`、`resize(w, h)`、`destroy()`、`on/off`、`setActive(i)`、`activeIndex`。
117
+
118
+ 导出:
119
+
120
+ - `toSVGString(): string` — 独立 SVG 文本(内嵌样式与 defs,含 xmlns/宽高),可直接存 `.svg` 或内联
121
+ - `getDataURL({ type?: 'png' | 'svg', scale?: number, background?: string }): Promise<string>` — 默认 PNG 2x;`background` 缺省透明
122
+ - `download({ filename?, type?, scale?, background? }): Promise<void>` — 触发浏览器下载,默认 `sk-chart.png`
123
+
124
+ ### 交互与无障碍
125
+
126
+ 悬停/触摸切换高亮列;SVG 聚焦后 `←` / `→` / `Home` / `End` 导航,`Enter` / `Space` 触发 `column:click`;移出回落到 `defaultActive`。列具备 `role="listitem"` 与同步的 `aria-selected`;`prefers-reduced-motion: reduce` 下自动关闭过渡动画。
127
+
128
+ ## 开发
129
+
130
+ ```bash
131
+ npm test # vitest(jsdom)
132
+ npm run typecheck # tsc --noEmit
133
+ npm run lint # eslint
134
+ npm run build # tsup → ESM/CJS/d.ts
135
+ ```
136
+
137
+ ## 路线图
138
+
139
+ - **M1**(进行中):ResizeObserver 真实像素自适应、数据更新过渡动画、主题包;已交付:nice ticks 真刻度 Y 轴、xAxis 配置(顶部类目行 + 底部语义行)、装饰开关 + 中性默认文案、主题包注册(`registerTheme` + 内置 light/dark)
140
+ - **M2**:更多图表类型、框架封装(React/Vue)
141
+ - **M3**:文档站、视觉回归 CI、npm 发布
142
+
143
+ ## 发布(维护者)
144
+
145
+ 本仓库已接入 npm Trusted Publishing(GitHub Actions + OIDC,免 token),完整流程见 [RELEASING.md](./RELEASING.md)。日常发版:
146
+
147
+ ```bash
148
+ npm version patch # 或 minor / major
149
+ git push --follow-tags # 推 v* tag → CI 自动发布 npm + 创建 GitHub Release
150
+ ```
151
+
152
+ 或直接双击根目录 `release.bat`(本地校验 → 提交推送 → 打 tag 发版一条龙)。
153
+
154
+ ## License
155
+
156
+ MIT