@taylorwong/ichartjs 2.0.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.
Files changed (80) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/LICENSE +201 -0
  3. package/README.md +194 -0
  4. package/agent-recipes/diagrams/agent-orchestration.json +18 -0
  5. package/agent-recipes/diagrams/approval-process.json +16 -0
  6. package/agent-recipes/diagrams/responsibility-mapping.json +13 -0
  7. package/agent-recipes/diagrams/workflow.json +19 -0
  8. package/agent-recipes/foundational-analysis.json +18 -0
  9. package/agent-recipes/project-management.json +47 -0
  10. package/agent-recipes/trend-line.json +21 -0
  11. package/docs/agent/README.md +62 -0
  12. package/docs/agent/charting-scenario.md +82 -0
  13. package/docs/agent/coding-agent-integration.md +88 -0
  14. package/docs/agent/development/2.0-release-readiness.md +65 -0
  15. package/docs/agent/development/iteration-2.md +9 -0
  16. package/docs/agent/development/iteration-3.md +142 -0
  17. package/docs/agent/development/iteration-4.md +261 -0
  18. package/docs/agent/development/iteration-5.md +43 -0
  19. package/docs/agent/development/iteration-6.md +130 -0
  20. package/docs/agent/development/iteration-7.md +117 -0
  21. package/docs/agent/development/iteration-8-acceptance.md +68 -0
  22. package/docs/agent/development/iteration-8.md +116 -0
  23. package/docs/agent/development/iteration-9.md +44 -0
  24. package/docs/agent/development/playground-plan.md +143 -0
  25. package/docs/agent/development/prompt-contract.md +22 -0
  26. package/docs/agent/development/rc-1-acceptance.md +43 -0
  27. package/docs/agent/development/roadmap.md +259 -0
  28. package/docs/agent/development-guide.md +60 -0
  29. package/docs/agent/diagram-scenario.md +82 -0
  30. package/docs/agent/editing-contract.md +71 -0
  31. package/docs/agent/frontend-integration.md +66 -0
  32. package/docs/agent/project-scenario.md +89 -0
  33. package/docs/agent/quickstart.md +205 -0
  34. package/docs/agent/runtime-contract.md +132 -0
  35. package/docs/agent/theme-guide.md +59 -0
  36. package/docs/agent/zh-CN/README.md +26 -0
  37. package/docs/agent/zh-CN/charting-scenario.md +62 -0
  38. package/docs/agent/zh-CN/coding-agent-integration.md +36 -0
  39. package/docs/agent/zh-CN/development-guide.md +31 -0
  40. package/docs/agent/zh-CN/diagram-scenario.md +37 -0
  41. package/docs/agent/zh-CN/editing-contract.md +27 -0
  42. package/docs/agent/zh-CN/frontend-integration.md +31 -0
  43. package/docs/agent/zh-CN/project-scenario.md +40 -0
  44. package/docs/agent/zh-CN/quickstart.md +84 -0
  45. package/docs/agent/zh-CN/runtime-contract.md +85 -0
  46. package/docs/agent/zh-CN/theme-guide.md +50 -0
  47. package/docs/manifests/capabilities.json +79 -0
  48. package/docs/manifests/commands.json +30 -0
  49. package/docs/manifests/schemas.json +13 -0
  50. package/examples/agent-workflow.mjs +94 -0
  51. package/package.json +55 -0
  52. package/skills/ichartjs/SKILL.md +72 -0
  53. package/skills/ichartjs/agents/openai.yaml +4 -0
  54. package/skills/ichartjs/references/agent-contract.md +39 -0
  55. package/skills/ichartjs/references/chart-selection.md +22 -0
  56. package/src/capabilities.mjs +190 -0
  57. package/src/charts.mjs +234 -0
  58. package/src/command.mjs +93 -0
  59. package/src/data.mjs +75 -0
  60. package/src/diagram-interaction.mjs +195 -0
  61. package/src/diagram.mjs +135 -0
  62. package/src/edit-controller.mjs +192 -0
  63. package/src/edit.mjs +369 -0
  64. package/src/format.mjs +19 -0
  65. package/src/history.mjs +14 -0
  66. package/src/index.mjs +562 -0
  67. package/src/plugin.mjs +18 -0
  68. package/src/project-analytics.mjs +357 -0
  69. package/src/project-linking.mjs +72 -0
  70. package/src/project.mjs +370 -0
  71. package/src/recipes.mjs +18 -0
  72. package/src/renderer.mjs +77 -0
  73. package/src/scale.mjs +9 -0
  74. package/src/scene.mjs +18 -0
  75. package/src/schema.mjs +142 -0
  76. package/src/spec.mjs +127 -0
  77. package/src/theme.mjs +232 -0
  78. package/src/transforms.mjs +34 -0
  79. package/src/validation.mjs +106 -0
  80. package/types/index.d.ts +134 -0
@@ -0,0 +1,132 @@
1
+ # Runtime Contract
2
+
3
+ Shared iChart.js 2.0 Runtime rules used by all three scenarios.
4
+
5
+ ## Standard Flow
6
+
7
+ ```text
8
+ getCapabilities → inspectData → planChart → create Spec → validateSpec → createChart → explain and inspect state
9
+ ```
10
+
11
+ Agents should use `getCapabilities()` first instead of hard-coding undeclared types or operations.
12
+
13
+ Iteration 8 adds per-chart profiles through `getChartCapability(type)`. Each profile declares required data roles, supported interactions, renderers, feature status, exports, and practical limits. Unsupported behavior must be handled from this profile or from validation diagnostics rather than guessed.
14
+
15
+ `planChart(data, { intent, renderer })` returns a versioned planning result with a primary chart, alternatives, confidence, reasons, required fields, suggested encodings, assumptions, warnings, unsupported requests, safe next actions, and the selected capability profile. Planning never invents business meaning, units, dates, or missing fields.
16
+
17
+ ## Spec Rules
18
+
19
+ - Specs must be JSON-serializable.
20
+ - Call `validateSpec()` before rendering.
21
+ - Chart layout and data semantics are renderer-independent.
22
+ - `flow` and `swimlane` use `nodes/edges/lanes`; generic charts use `data.values`.
23
+
24
+ ## Renderer
25
+
26
+ - Use `svg` for DOM-level interaction, accessibility, and Diagram editing.
27
+ - Use `canvas` for many marks and lower DOM overhead.
28
+
29
+ ## Branding (Signature)
30
+
31
+ iChart.js ships with a low-contrast brand signature (`Powered by iChart.js`) in the bottom-right corner of every chart by default.
32
+
33
+ ### Configuration
34
+
35
+ Control via the `branding` field on Spec or Theme:
36
+ - `branding: true` (default): signature enabled.
37
+ - `branding: false`: signature disabled.
38
+ - `branding: { enabled: true }`: explicit object form for fine-grained control.
39
+
40
+ ### Consistency Guarantee
41
+
42
+ The on/off decision is resolved once inside `buildScene()` via a single gate, so these four surfaces are always synchronized:
43
+ 1. Browser Canvas / SVG live rendering.
44
+ 2. `chart.export({ type:'png|jpeg' })` raster output.
45
+ 3. `chart.export({ type:'svg' })` vector output.
46
+ 4. `chart.export({ type:'json' })` persisted `spec.branding` + `state`.
47
+
48
+ When `branding:false`, no signature text appears on the chart or any exported artifact, and the extra bottom padding is not reserved.
49
+
50
+ ## Exports and Downloads
51
+
52
+ iChart.js exports use a **dual-engine single-source architecture**. Every artifact shares the same Scene Graph produced by `buildScene()`, and the on-screen renderer is fully decoupled from the export backend:
53
+ 1. **PNG / JPEG (raster)**: always drawn by a `CanvasRenderer` replaying the shared Scene Graph synchronously. In headless Node, install the optional `canvas` npm package to enable.
54
+ 2. **SVG (vector)**: serialized from an `SVGRenderer` DOM tree (browser) or assembled as a pure string with zero dependencies (headless). Includes XML 1.0 header, expanded font properties, and accessibility attributes.
55
+ 3. **JSON (reconstructable)**: serializes the current `spec` plus `getState()`, used for persistence, agent self-check, and cross-environment rebuild.
56
+
57
+ ### Branding Consistency
58
+
59
+ The signature gate inside `buildScene()` guarantees that live rendering, SVG export, PNG export, and JSON state are 100% aligned.
60
+
61
+ ### Headless Support Matrix
62
+
63
+ | Type | Browser (any renderer) | Node headless, zero deps | Node headless + `canvas` dep |
64
+ |-------------|------------------------|--------------------------|------------------------------|
65
+ | JSON | ✅ | ✅ | ✅ |
66
+ | SVG | ✅ | ✅ | ✅ |
67
+ | PNG / JPEG | ✅ true raster, sync | ❌ structured `HEADLESS_EXPORT_UNSUPPORTED` | ✅ |
68
+
69
+ ### Public Export APIs
70
+
71
+ - `chart.toDataURL(type='image/png')` → data URL string or structured ExportError.
72
+ - `chart.toBlob(type='image/png')` → Blob or ExportError (headless returns `BLOB_HEADLESS`).
73
+ - `chart.export({ type, as })` → sync string / JSON object / Blob / ExportError. `as` accepts `string`, `dataurl`, `blob`, `object` (JSON only).
74
+ - `chart.exportAsync({ type, as })` → Promise wrapper for future async scenarios.
75
+ - `chart.download({ type })` / `downloadPNG()` / `downloadSVG()` / `downloadJSON()` → triggers a browser save-as. In headless, falls back to returning the raw string or structured error.
76
+
77
+ ### Structured Error Shape
78
+
79
+ All export/download methods return a stable `{ valid:false, code, message?, suggestion?, rasterCode? }` object on failure for reliable agent automation. Common `code` values:
80
+ - `HEADLESS_EXPORT_UNSUPPORTED`: raster export requested in headless without the optional `canvas` package.
81
+ - `BLOB_HEADLESS`: `toBlob` or `as=blob` used in headless (Blob requires a browser runtime).
82
+ - `DOWNLOAD_HEADLESS`: `chart.download*()` in headless; use `chart.export()` instead.
83
+ - `EXPORT_TYPE_UNSUPPORTED`: unrecognized export type.
84
+
85
+ ## Common APIs
86
+
87
+ ```text
88
+ inspectData(data)
89
+ normalizeData(data)
90
+ recommend(data, options)
91
+ planChart(data, options)
92
+ validateSpec(spec)
93
+ createChart(spec)
94
+ getCapabilities()
95
+ getChartCapability(type)
96
+ chart.describe()
97
+ chart.explain()
98
+ chart.getState()
99
+ chart.getSelectedData()
100
+ chart.export(options)
101
+ chart.exportAsync(options)
102
+ chart.toDataURL(type)
103
+ chart.toBlob(type)
104
+ chart.download(options)
105
+ chart.downloadPNG()
106
+ chart.downloadSVG()
107
+ chart.downloadJSON()
108
+ ```
109
+
110
+ Validation results contain separate `errors`, `warnings`, and `normalizations`. Diagnostics use stable codes, JSON-oriented paths, expected values where useful, and actionable suggestions. `chart.explain()` returns encodings, transforms, interactions, assumptions, warnings, stable record lineage, and an accessibility summary.
111
+
112
+ ## Interaction
113
+
114
+ Common interactions include Tooltip, Hover, Click, Selection, Zoom, Pan, Drag, Touch, and Keyboard; each chart's enabled interactions are controlled by its Spec and capability declaration.
115
+
116
+ ## Implementation Map
117
+
118
+ - Public runtime: `src/index.mjs`
119
+ - Spec: `src/spec.mjs`
120
+ - Scene: `src/scene.mjs`
121
+ - Renderers: `src/renderer.mjs`
122
+ - Charts: `src/charts.mjs`
123
+ - Capabilities: `src/capabilities.mjs`
124
+ - Plugins: `src/plugin.mjs`
125
+ - Scales: `src/scale.mjs`
126
+
127
+ ## Acceptance
128
+
129
+ - The Gallery covers every `getCapabilities().chartTypes` entry.
130
+ - Canvas and SVG use the same Scene data structure.
131
+ - Invalid Specs return structured errors with `code`, `path`, `message`, and `suggestion`.
132
+ - Branding visibility is 100% synchronized between on-screen rendering and every export format (PNG/SVG/JSON).
@@ -0,0 +1,59 @@
1
+ # Visual Style and Theme Guide
2
+
3
+ iChart.js 2.0 includes a lightweight renderer-neutral style system. Agents should request semantic style intent and let the runtime resolve concrete tokens instead of inventing colors, font sizes, or spacing per chart.
4
+
5
+ ## Style Model
6
+
7
+ The system composes three independent choices:
8
+
9
+ - `mode`: `auto`, `light`, `dark`, or `contrast`.
10
+ - `preset`: `auto`, `analysis`, `dashboard`, `report`, `presentation`, `project`, or `diagram`.
11
+ - `palette`: `auto`, `categorical`, `sequential`, `diverging`, or `status`.
12
+
13
+ `auto` is the recommended default. It uses chart family, intent, data semantics, and the host color scheme. Resolution is deterministic and exposed through `planStyle()`, `planChart().styleRecommendation`, `chart.getTheme()`, `chart.getState().style`, and `chart.explain().style`.
14
+
15
+ ## Agent Workflow
16
+
17
+ ```js
18
+ import { createChart, planChart, planStyle, validateSpec } from '@taylorwong/ichartjs';
19
+
20
+ const plan = planChart(rows, { intent: 'comparison', context: 'dashboard' });
21
+ const style = planStyle({ type: plan.primary, data: rows }, { context: 'dashboard' });
22
+ const spec = {
23
+ type: plan.primary,
24
+ data: { values: rows },
25
+ theme: { mode: 'auto', preset: style.preset, palette: style.palette }
26
+ };
27
+
28
+ const validation = validateSpec(spec);
29
+ if (!validation.valid) throw new Error(JSON.stringify(validation.errors));
30
+ const chart = createChart(validation.spec);
31
+ ```
32
+
33
+ Use explicit values only when the user requests a mode, presentation context, or semantic palette. A user override always wins over automatic matching.
34
+
35
+ ## Runtime Switching
36
+
37
+ Switch styles without recreating the chart:
38
+
39
+ ```js
40
+ chart.setTheme({ mode: 'dark', preset: 'dashboard', palette: 'categorical' });
41
+ const resolved = chart.getTheme();
42
+ ```
43
+
44
+ When `mode` is `auto`, mounted charts follow `prefers-color-scheme` changes. Explicit `colors`, `background`, and `padding` on the chart Spec remain preserved across theme changes.
45
+
46
+ ## Palette Rules
47
+
48
+ - Use `categorical` for distinct series or groups; prefer six or fewer simultaneous comparison colors.
49
+ - Use `sequential` for ordered magnitude such as Heatmap intensity.
50
+ - Use `diverging` when values have a meaningful midpoint and extend in both directions.
51
+ - Use `status` for named business states such as success, warning, danger, and information.
52
+ - Do not use color as the only carrier of meaning. Keep labels, shapes, line patterns, or text status where applicable.
53
+
54
+ ## Accessibility
55
+
56
+ Built-in modes expose text, muted text, axis, grid, focus, selection, missing-value, and status tokens. Call `validateThemeContrast(theme)` for custom token overrides. Do not suppress contrast warnings in Agent output.
57
+
58
+ Preview and acceptance: `http://localhost:3000/playground/theme-gallery.html`.
59
+
@@ -0,0 +1,26 @@
1
+ # iChart.js 2.0 Agent 指南(中文)
2
+
3
+ 这是 iChart.js 2.0 面向用户 Agent 的中文使用入口。默认先读取本文件,再按任务读取一份场景文档。
4
+
5
+ ## 选择场景
6
+
7
+ - [Agent 快速上手](quickstart.md)
8
+ - [编码 Agent 集成](coding-agent-integration.md)
9
+ - [前端项目集成](frontend-integration.md)
10
+ - [视觉样式与主题](theme-guide.md)
11
+ - [数据分析图表](charting-scenario.md)
12
+ - [项目管理图表](project-scenario.md)
13
+ - [交互式 Diagram](diagram-scenario.md)
14
+ - [Runtime 契约](runtime-contract.md)
15
+ - [编辑契约](editing-contract.md)
16
+ - 机器可读能力清单位于 `docs/manifests/`,供 Agent 按需读取。
17
+
18
+ ## 标准流程
19
+
20
+ ```text
21
+ getCapabilities → inspectData → planChart → 生成 Spec → validateSpec → createChart → explain/getState
22
+ ```
23
+
24
+ API 名称、字段名、命令名、错误码和 JSON Manifest 统一使用英文标识符;其语义以英文技术契约为标准,本文提供中文辅助说明。
25
+
26
+ 全部已支持图表的浏览入口:`playground/project-gallery.html`;主题样式验收入口:`playground/theme-gallery.html`。
@@ -0,0 +1,62 @@
1
+ # 场景:数据分析图表
2
+
3
+ 用于通用指标分析和数据展示的 Agent 使用与开发指南。
4
+
5
+ ## 图表选择
6
+
7
+ | 类型 | 适用场景 | 推荐 Renderer |
8
+ | --- | --- | --- |
9
+ | `line` | 趋势、时间变化 | `svg` |
10
+ | `area` | 趋势和累计量 | `svg` |
11
+ | `bar` | 分类对比、长标签 | `svg` |
12
+ | `column` | 紧凑分类对比 | `canvas` 或 `svg` |
13
+ | `pie` | 少量类别占比 | `svg` |
14
+ | `scatter` | 两个数值变量关系 | `canvas` |
15
+ | `funnel` | 转化阶段 | `svg` |
16
+ | `gauge` | 单指标完成度 | `canvas` |
17
+ | `heatmap` | 矩阵强度与日历模式 | `canvas` 或 `svg` |
18
+ | `radar` | 多维指标对比 | `svg` |
19
+
20
+ ## 基础模式
21
+
22
+ - Bar、Column、Area 使用 `stack: "stacked"` 或 `stack: "percent"`,不新增堆叠类型。
23
+ - Donut 使用 `type: "pie"` 配合 `innerRadius`。
24
+ - Combo 使用每个系列的 `mark: "column"` 或 `mark: "line"`,并可设置 `axis: "right"`。
25
+ - Histogram 使用 `transform: { type: "bin", field, thresholds | step, extent }`。
26
+ - Heatmap 通过 `colorScale.missing` 区分缺失值和数值零。
27
+ - Radar 应为每个指标声明 `min` 和 `max`;缺失域或混合单位会产生告警。
28
+
29
+ ## Agent 流程
30
+
31
+ 1. 使用 `inspectData(data)` 检查字段和缺失值。
32
+ 2. 根据意图选择图表,可使用 `recommend(data, { intent })`。
33
+ 3. 生成 JSON-serializable Chart Spec。
34
+ 4. 先调用 `validateSpec(spec)`,再调用 `createChart(spec)`。
35
+ 5. 使用 `chart.describe()` 和 `chart.getState()` 检查结果。
36
+
37
+ ## 最小 Spec
38
+
39
+ ```js
40
+ {
41
+ type: 'line',
42
+ renderer: 'svg',
43
+ data: { values: [{ month: 'Jan', sales: 120 }] },
44
+ encoding: {
45
+ x: { field: 'month', type: 'category' },
46
+ y: { field: 'sales', type: 'quantitative' }
47
+ }
48
+ }
49
+ ```
50
+
51
+ ## 开发位置与验收
52
+
53
+ - Spec:`src/spec.mjs`
54
+ - 数据:`src/data.mjs`
55
+ - Scene:`src/charts.mjs`
56
+ - Scale:`src/scale.mjs`
57
+ - Renderer:`src/renderer.mjs`
58
+ - 测试:`tests/core.test.mjs`
59
+ - 验收:`playground/project-gallery.html`
60
+ - Iteration 7 验收:`playground/foundational-gallery.html`
61
+
62
+ 新增图表必须同步更新代码、测试、Gallery、`docs/manifests/capabilities.json` 和本文件。
@@ -0,0 +1,36 @@
1
+ # 编码 Agent 集成
2
+
3
+ 本指南适用于 Codex、WorkBuddy 等能够读取仓库、编辑 JavaScript、运行测试并打开本地预览的编码 Agent。
4
+
5
+ ## 边界
6
+
7
+ iChart.js 是 JavaScript UI 图表组件库。编码 Agent 负责修改宿主前端,并调用统一的 `ichartjs` ESM API。Skill 只提供工作流指导,不是第二套 Runtime,也不是服务接口。
8
+
9
+ 普通图表开发不需要 CLI、MCP、HTTP API 或 Python 适配层。
10
+
11
+ ## 使用方式
12
+
13
+ ```bash
14
+ npm install @taylorwong/ichartjs@^2
15
+ ```
16
+
17
+ npm Registry 中无作用域的 `ichartjs` 当前是安全占位包,并非本项目。正式 npm scope 确认前请从 GitHub 安装。
18
+
19
+ Codex、WorkBuddy 和其他兼容 Agent Skills 的宿主可共同使用 `skills/ichartjs`。从仓库检出目录安装到 Codex:
20
+
21
+ ```bash
22
+ cp -R skills/ichartjs "${CODEX_HOME:-$HOME/.codex}/skills/"
23
+ ```
24
+
25
+ 推荐请求:
26
+
27
+ ```text
28
+ 使用 $ichartjs 检查这份数据,选择并校验合适图表,加入当前页面,
29
+ 运行相关测试,并返回准确预览地址。保留所有假设和数据质量警告。
30
+ ```
31
+
32
+ 如果宿主不使用 `$skill-name` 语法,则在其 Skill 界面中选择或指定 `ichartjs`。
33
+
34
+ Agent 应依次完成:读取项目约束、引用 `ichartjs`、发现能力、检查数据、规划图表、校验 Spec、挂载图表、自检 explanation/state、运行测试并返回预览地址。
35
+
36
+ 验收时确认没有引用 `src/` 内部文件,校验在渲染前通过,稳定 record ID 被保留,警告可见,并且替换页面时调用了 `destroy()`。
@@ -0,0 +1,31 @@
1
+ # 开发指南
2
+
3
+ 指导 Coding Agent 开发 iChart.js 2.0。
4
+
5
+ ## 标准流程
6
+
7
+ 1. 判断功能属于数据分析、项目管理还是 Diagram 场景。
8
+ 2. 阅读本目录对应场景文档和当前 `development/iteration-X.md`。
9
+ 3. 定位最小源码范围,遵守文件级模块注释规则。
10
+ 4. 增加测试并实现功能,不修改无关问题。
11
+ 5. 同步 Manifest、场景文档、Recipe 和 Gallery。
12
+ 6. 运行 `npm run agent:check`。
13
+ 7. 有可查看 Demo 时,立即提供 HTTP 地址和验收步骤。
14
+
15
+ ## 修改映射
16
+
17
+ - 通用图表:`src/spec.mjs`、`src/charts.mjs`、`src/data.mjs`。
18
+ - 项目能力:`src/project.mjs`,必要时更新 `src/schema.mjs`。
19
+ - Diagram 能力:`src/diagram.mjs`、`src/diagram-interaction.mjs`。
20
+ - 公共编辑:`src/command.mjs`、`src/edit.mjs`、`src/edit-controller.mjs`。
21
+
22
+ ## 文档规则
23
+
24
+ - 活动 `src/*.mjs` 必须有简短文件级注释。
25
+ - 公共 API 和复杂算法增加必要 JSDoc。
26
+ - 代码注释使用英文;中文文档是辅助版本。
27
+ - Manifest 和 API 标识符保持英文且稳定。
28
+ - `docs/agent/` 顶层核心文档统一使用英文,中文辅助版放在 `docs/agent/zh-CN/`;技术契约变更时同步更新两种语言。
29
+ - 开发历史放在 `docs/agent/development/`,不作为普通 Agent 默认上下文。
30
+
31
+ `npm run agent:check` 检查图表和命令清单、Schema 模型覆盖、双语文件存在性、英文核心文档中的中文残留以及 Gallery 类型覆盖,再运行语法检查和测试。它不证明翻译语义一致或浏览器交互正确;开发历史不受英文检查限制。
@@ -0,0 +1,37 @@
1
+ # 场景:交互式 Diagram
2
+
3
+ 用于流程建模、责任分工和可编辑 Diagram。
4
+
5
+ ## 类型
6
+
7
+ - `flow`:节点和边组成的流程图。
8
+ - `swimlane`:带责任泳道的流程图。
9
+
10
+ ## 当前能力
11
+
12
+ 已支持节点、边、泳道、Group、Port、四种布局、三种路由、节点拖动、多选、对齐、网格吸附、键盘移动和 Undo/Redo。
13
+
14
+ 当前限制:
15
+
16
+ - Group 只支持平级 Group,不支持嵌套。
17
+ - Port 可显示并参与路由,但 Port 拖拽连线尚未完成。
18
+ - Copy/Paste 和 Group 折叠展开尚未完成。
19
+
20
+ ## Agent 流程
21
+
22
+ 1. 为节点和边分配稳定 ID。
23
+ 2. 使用 `validateDiagram(spec)` 检查端点、Port、Group、Lane 和布局。
24
+ 3. 使用 `createChart(spec)` 创建图表。
25
+ 4. 使用 `moveNodes`、`alignNodes`、`snapNodes` 等命令编辑。
26
+ 5. 编辑遵循 `editing-contract.md` 的 Preview/Confirm/Commit 流程。
27
+
28
+ ## 开发位置与验收
29
+
30
+ - 模型、校验、布局、路由:`src/diagram.mjs`
31
+ - 交互:`src/diagram-interaction.mjs`
32
+ - Scene:`src/project.mjs`
33
+ - 命令事务:`src/command.mjs`、`src/edit-controller.mjs`
34
+ - 专用 Demo:`playground/diagram-editor.html`
35
+ - 全量 Gallery:`playground/project-gallery.html`
36
+
37
+ 节点移动后必须验证边、箭头和标签跟随;同时检查 Canvas 与 SVG 的一致性。
@@ -0,0 +1,27 @@
1
+ # 编辑契约
2
+
3
+ Agent 对业务数据和 Diagram 数据的安全编辑协议。
4
+
5
+ ## 必须遵循的流程
6
+
7
+ ```text
8
+ 读取 Schema → 构造 Command → Validate → Preview → Confirm → Commit → ChangeSet
9
+ ```
10
+
11
+ 不要直接修改业务数据对象,使用 Chart 编辑 API。
12
+
13
+ ```js
14
+ const preview = chart.previewEdit(command);
15
+ if (!preview.valid) return preview.errors;
16
+ const result = chart.applyEdit(command, { preview, confirmed: true, source: 'agent' });
17
+ ```
18
+
19
+ ## 规则
20
+
21
+ - 命令版本为 `1.0`,目标使用稳定 ID。
22
+ - Preview 和 Commit 的命令必须一致。
23
+ - 默认需要 Host 确认;确认不等同于授权。
24
+ - 成功提交产生 ChangeSet、审计信息、revision 和 Undo 历史。
25
+ - 外部持久化、权限和认证由 Host 应用负责。
26
+
27
+ Schema、命令、Preview/Commit、事务和历史的实现分别位于 `src/schema.mjs`、`src/command.mjs`、`src/edit.mjs`、`src/edit-controller.mjs` 和 `src/history.mjs`。
@@ -0,0 +1,31 @@
1
+ # 前端项目集成
2
+
3
+ iChart.js 应作为普通 JavaScript UI 组件运行在浏览器应用中。数据加载、登录、持久化、路由以及自然语言交互都由宿主应用负责。
4
+
5
+ ```bash
6
+ npm install @taylorwong/ichartjs@^2
7
+ ```
8
+
9
+ 无法访问 npm Registry 的环境请用 GitHub 源作为后备:`npm install github:wanghetommy/ichartjs#v2.0.1`。
10
+
11
+ ```js
12
+ import { createChart } from '@taylorwong/ichartjs';
13
+
14
+ const chart = createChart({
15
+ container: '#chart',
16
+ type: 'bar',
17
+ data: { values: rows },
18
+ encoding: {
19
+ x: { field: 'category' },
20
+ y: { field: 'value' }
21
+ }
22
+ });
23
+ ```
24
+
25
+ 容器创建后再实例化图表;数据变化使用 `setData()`;配置变化使用 `update()`;组件卸载前调用 `destroy()`。
26
+
27
+ 如果宿主页面提供“展示销售趋势”之类的 Agent 功能,仍然在 JavaScript 中调用 `inspectData()`、`planChart()`、`validateSpec()` 和 `createChart()`,并在 UI 中展示原因、假设和警告。
28
+
29
+ 不能执行或生成 JavaScript 的日常助手无法直接使用任何 JS UI 组件,其宿主应用应负责集成。iChart.js 核心不提供 CLI、MCP、HTTP 服务、Python Runtime、上传、认证、存储或分享系统。
30
+
31
+ 本仓库可运行 `npm run playground`,然后访问 `http://localhost:3000/playground/project-gallery.html` 验收全部图表。
@@ -0,0 +1,40 @@
1
+ # 场景:项目管理图表
2
+
3
+ 用于项目排期、交付进度和项目状态报告。
4
+
5
+ ## 图表选择
6
+
7
+ | 类型 | 适用场景 | 核心数据 |
8
+ | --- | --- | --- |
9
+ | `gantt` | 任务排期、依赖、关键路径 | `id/name/start/end` |
10
+ | `timeline` | 事件时间线 | `id/title/date` |
11
+ | `milestone` | 关键节点 | `id/title/date` |
12
+ | `burndown` | Sprint 剩余工作量 | `date/remaining` |
13
+
14
+ ## 业务规则
15
+
16
+ - 日期必须有效,Gantt 的 `end` 不得早于 `start`。
17
+ - `progress` 使用 `0–100` 百分比。
18
+ - `dependencies` 使用稳定任务 ID,依赖图不能有环。
19
+ - `scopeChange` 表示范围变化,不等于已完成工作量。
20
+ - Forecast 是估计结果,不能描述为承诺或事实。
21
+
22
+ ## 编辑流程
23
+
24
+ 项目编辑统一遵循:
25
+
26
+ ```text
27
+ Schema → Command → Validate → Preview → Confirm → Commit → ChangeSet
28
+ ```
29
+
30
+ 常用命令包括 `updateProgress`、`shiftTask`、`addDependency`、`removeDependency` 和 `updateMilestone`。
31
+
32
+ ## 开发位置与验收
33
+
34
+ - 项目 Scene 和 Tooltip:`src/project.mjs`
35
+ - Schema:`src/schema.mjs`
36
+ - 命令和编辑:`src/command.mjs`、`src/edit.mjs`
37
+ - 测试:`tests/core.test.mjs`
38
+ - 验收:`playground/project-gallery.html`
39
+
40
+ 需要覆盖日期错误、循环依赖、Scope Change、Forecast 和编辑历史。
@@ -0,0 +1,84 @@
1
+ # Agent 快速上手
2
+
3
+ 当 Agent 需要把业务数据、项目数据或流程数据转换为 iChart.js 可视化时,使用本指南。只依赖公开契约,不读取渲染器或图表内部实现来猜测能力。
4
+
5
+ ## 引用入口
6
+
7
+ ```js
8
+ import {
9
+ createChart,
10
+ getCapabilities,
11
+ getChartCapability,
12
+ inspectData,
13
+ planChart,
14
+ validateSpec
15
+ } from '@taylorwong/ichartjs';
16
+ ```
17
+
18
+ - `ichartjs`:统一的 Agent 规划与 Runtime API。
19
+ - `ichartjs/capabilities.json`:机器可读能力清单,包含逐图表导出、署名和交互声明。
20
+ - `ichartjs/recipes/*`:基础分析、项目管理和 Diagram Recipes。
21
+ - `skills/ichartjs/SKILL.md`:适用于 Codex、WorkBuddy 等 Agent Skills 兼容宿主的可选编排层。
22
+
23
+ Agent 与开发者使用同一个 ESM 入口。编码 Agent 的完整方式见 [编码 Agent 集成](coding-agent-integration.md),普通应用集成见 [前端项目集成](frontend-integration.md)。
24
+
25
+ ## 标准流程
26
+
27
+ ```text
28
+ 发现能力 → 检查数据 → 规划图表 → 构建 Spec → 校验 → 渲染 → 解释、导出和自检
29
+ ```
30
+
31
+ 1. 调用 `getCapabilities()`,不要自行创造图表类型或配置项。
32
+ 2. 调用 `inspectData()`,检查字段角色、标识符、维度、度量、时间范围、缺失值和警告。
33
+ 3. 调用 `planChart()`,同时读取主推荐、备选方案、置信度、缺失字段、假设、警告、不支持请求和 `styleRecommendation`。
34
+ 4. 当 `requiredFields` 非空时停止渲染,向用户请求数据或选择有依据的备选方案。
35
+ 5. 构建 JSON 可序列化的 Spec,并调用 `validateSpec()`。Spec 可通过 `branding: false` 显式关闭品牌署名;默认保留署名以提升项目可见性。
36
+ 6. 仅在 `validation.valid` 为 `true` 时调用 `createChart()`。
37
+ 7. 用 `chart.explain()`、`chart.getState()`、JSON/SVG/PNG export 完成自检。需要持久化或附件生成时使用 `chart.export({type:'json'|'svg'|'png'})`,在浏览器环境可调用 `chart.downloadPNG()` / `chart.downloadSVG()` / `chart.downloadJSON()` 触发保存,最后调用 `chart.destroy()`。
38
+
39
+ ## 品牌署名(Branding)默认行为
40
+
41
+ - 默认 `branding: true`:在画面与所有导出产物(PNG/SVG/JSON)右下角同步出现 `Powered by iChart.js` 低对比度署名。
42
+ - 关闭:在 Spec 或 Theme 中显式设置 `branding: false`,此时画面和所有导出产物都不会出现署名,同时不再额外预留底部 padding。
43
+ - 一致性:署名开关由 Scene Graph 总闸统一判定,画面渲染与导出 100% 同步。
44
+
45
+ ## 导出用例速览
46
+
47
+ ```js
48
+ // 1. JSON:可跨端重建 + agent 自检(全环境零依赖)
49
+ const json = chart.export({ type: 'json' });
50
+ const obj = chart.export({ type: 'json', as: 'object' });
51
+
52
+ // 2. SVG:矢量保真,浏览器 + 无头零依赖
53
+ const svg = chart.export({ type: 'svg' });
54
+ const svgDataUrl = chart.export({ type: 'svg', as: 'dataurl' });
55
+
56
+ // 3. PNG / JPEG:同步真光栅
57
+ // 浏览器:任意 renderer 均可
58
+ // 无头:需安装 npm i canvas,否则返回结构化错误 code=HEADLESS_EXPORT_UNSUPPORTED
59
+ const png = typeof document !== 'undefined' ? chart.toDataURL('image/png') : null;
60
+ ```
61
+
62
+ ## Agent 输出要求
63
+
64
+ 最终结果应包含:
65
+
66
+ - 选择的图表及原因;
67
+ - 使用的字段和转换;
68
+ - 假设、警告和不支持能力;
69
+ - 校验结果;
70
+ - 稳定 record ID 的 lineage;
71
+ - 品牌署名(Branding)开关状态,避免可见与导出不一致;
72
+ - 可访问的预览 URL 或导出产物(JSON/SVG/PNG/JPEG)路径。
73
+
74
+ 不要虚构字段、单位、日期、依赖关系、日历规则或预测置信度。Radar 使用混合单位时必须提供显式 domain;Heatmap 必须区分缺失值和零;高基数占比数据优先使用 Bar 而不是 Pie。
75
+
76
+ ## 完整示例
77
+
78
+ ```bash
79
+ npm run example:agent
80
+ ```
81
+
82
+ 完整代码位于 [`../../../examples/agent-workflow.mjs`](../../../examples/agent-workflow.mjs),包含 inspect→plan→validate→create→explain→export→destroy 的完整链路。交互验收可运行 `npm run playground`,然后打开 `http://localhost:3000/playground/agent-workbench.html`。在所有 Gallery 页面点击右上角导出按钮可验证 PNG / SVG / JSON 下载行为。
83
+
84
+ 样式自动匹配与用户切换见[视觉样式与主题](theme-guide.md),可在 `http://localhost:3000/playground/theme-gallery.html` 验收。
@@ -0,0 +1,85 @@
1
+ # Runtime 契约
2
+
3
+ 三个开发场景共用的 Runtime 规则。
4
+
5
+ ## 标准流程
6
+
7
+ ```text
8
+ getCapabilities → inspectData → planChart → 创建 Spec → validateSpec → createChart → 解释并检查状态
9
+ ```
10
+
11
+ Agent 应优先使用 `getCapabilities()`,不要硬编码未声明的图表类型或操作。
12
+
13
+ Iteration 8 通过 `getChartCapability(type)` 提供逐图表能力档案,包括必需数据角色、支持的交互、Renderer、功能状态、导出和建议限制。Agent 不应猜测未声明能力。
14
+
15
+ `planChart(data, { intent, renderer })` 返回版本化规划结果:主选图表、备选项、置信度、原因、缺失字段、建议编码、假设、警告、不支持请求和安全下一步。规划不会虚构业务含义、单位、日期或缺失字段。
16
+
17
+ `validateSpec()` 分开返回 `errors`、`warnings` 和 `normalizations`;诊断包含稳定代码、JSON 路径、期望值和修复建议。`chart.explain()` 返回编码、转换、交互、假设、警告、稳定记录血缘和无障碍摘要。
18
+
19
+ ## 关键规则
20
+
21
+ - Spec 必须是 JSON-serializable。
22
+ - 渲染前调用 `validateSpec()`。
23
+ - 布局和数据语义不依赖 Renderer。
24
+ - 通用图表使用 `data.values`;Flow/Swimlane 使用 `nodes/edges/lanes`。
25
+ - `svg` 适合 DOM 交互和可访问性;`canvas` 适合大量图元和绘制性能。
26
+
27
+ ## 品牌署名(Branding)
28
+
29
+ iChart.js 默认在所有图表右下角显示低对比度的品牌署名(`Powered by iChart.js`),用于提升项目可见性。
30
+
31
+ ### 配置项
32
+
33
+ - 在 Spec 或 Theme 中通过 `branding` 字段控制:
34
+ - `branding: true`(默认):开启署名。
35
+ - `branding: false`:关闭署名。
36
+ - `branding: { enabled: true }`:细粒度配置形式。
37
+
38
+ ### 一致性保证
39
+
40
+ 署名开关由 `buildScene()` 内统一总闸判定,以下四个面必然同步:
41
+ 1. 浏览器 Canvas/SVG 画面渲染。
42
+ 2. `chart.export({ type:'png|jpeg' })` 光栅导出。
43
+ 3. `chart.export({ type:'svg' })` 矢量导出。
44
+ 4. `chart.export({ type:'json' })` 中 `spec.branding` + `state` 持久化状态。
45
+
46
+ 关闭 `branding:false` 时,画面和所有导出产物都不会出现署名文字,同时不会再额外预留底部 padding。
47
+
48
+ ## 导出与下载
49
+
50
+ iChart.js 导出采用**双底层单源架构**,所有产物共享 `buildScene()` 生成的同一份 Scene Graph,**画面显示用的 renderer 和导出底层完全解耦**:
51
+ 1. **PNG/JPEG(光栅)**:底层用 `CanvasRenderer` 重绘 Scene Graph,同步输出真光栅文件;无头环境安装 `canvas` npm 包即可支持。
52
+ 2. **SVG(矢量)**:底层用 `SVGRenderer` DOM 序列化(浏览器)或纯字符串拼装(无头零依赖),支持 XML 1.0 头部、字体拆分、无障碍属性。
53
+ 3. **JSON(可重建)**:序列化当前 `spec` + `getState()` 结果,用于持久化、Agent 自检和跨端重建。
54
+
55
+ ### Branding 一致性
56
+
57
+ 开关由 `buildScene()` 内统一总闸判定,画面渲染 / SVG 导出 / PNG 导出 / JSON state 必然同步。
58
+
59
+ ### Headless 支持矩阵
60
+
61
+ | 类型 | 浏览器环境(任意 renderer) | Node 无头零依赖 | Node 无头 + `canvas` 依赖 |
62
+ |------------|----------------------------|----------------|--------------------------|
63
+ | JSON | ✅ | ✅ | ✅ |
64
+ | SVG | ✅ | ✅ | ✅ |
65
+ | PNG / JPEG | ✅ 同步真光栅 | ❌ 返回结构化 `HEADLESS_EXPORT_UNSUPPORTED` | ✅ |
66
+
67
+ ### 公共导出 API
68
+
69
+ - `chart.toDataURL(type='image/png')` → data URL 字符串或结构化 ExportError。
70
+ - `chart.toBlob(type='image/png')` → Blob 或 ExportError(无头返回 `BLOB_HEADLESS`)。
71
+ - `chart.export({ type, as })` → 同步返回字符串 / JSON 对象 / Blob / ExportError,`as` 支持 `string`、`dataurl`、`blob`、`object`(仅 JSON)。
72
+ - `chart.exportAsync({ type, as })` → Promise 包装,适配未来异步场景。
73
+ - `chart.download({ type })` / `downloadPNG()` / `downloadSVG()` / `downloadJSON()` → 触发浏览器保存(无头回落到返回字符串或结构化错误)。
74
+
75
+ ### 错误结构
76
+
77
+ 所有导出/下载方法失败时统一返回 `{ valid:false, code, message?, suggestion?, rasterCode? }` 稳定结构,便于 Agent 自动化判断,常见 `code`:
78
+ - `HEADLESS_EXPORT_UNSUPPORTED`:当前无头环境缺少光栅所需依赖(`canvas`)。
79
+ - `BLOB_HEADLESS`:`toBlob` / `as=blob` 需要浏览器 Blob。
80
+ - `DOWNLOAD_HEADLESS`:`chart.download*()` 仅在浏览器有 DOM 时可用,无头用 `export`。
81
+ - `EXPORT_TYPE_UNSUPPORTED`:不支持的导出类型。
82
+
83
+ 公共 API:`inspectData`、`normalizeData`、`planChart`、`recommend`、`validateSpec`、`createChart`、`getCapabilities`、`getChartCapability`、`chart.describe`、`chart.explain`、`chart.getState`、`chart.export`、`chart.exportAsync`、`chart.toDataURL`、`chart.toBlob`、`chart.download`、`chart.downloadPNG`、`chart.downloadSVG`、`chart.downloadJSON`。
84
+
85
+ 实现位置:`src/index.mjs`、`src/spec.mjs`、`src/scene.mjs`、`src/renderer.mjs`、`src/plugin.mjs`、`src/scale.mjs`、`src/charts.mjs`、`src/capabilities.mjs`。