pdd-skills 3.0.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/README.md +1478 -0
- package/bin/pdd.js +354 -0
- package/config/bpmn-rules.yaml +166 -0
- package/config/checkstyle.xml +105 -0
- package/config/eslint.config.js +48 -0
- package/config/pmd.xml +91 -0
- package/config/prd-rules.yaml +113 -0
- package/config/ruff.toml +45 -0
- package/config/sqlfluff.cfg +82 -0
- package/hooks/hook-executor.js +332 -0
- package/index.js +43 -0
- package/lib/api-routes.js +750 -0
- package/lib/api-server.js +408 -0
- package/lib/cache/cache-config.js +209 -0
- package/lib/cache/system-cache.js +852 -0
- package/lib/config-manager.js +373 -0
- package/lib/generate.js +528 -0
- package/lib/grpc/grpc-routes.js +1134 -0
- package/lib/grpc/grpc-server.js +912 -0
- package/lib/grpc/proto-definitions.js +1033 -0
- package/lib/init.js +172 -0
- package/lib/iteration/auto-fixer.js +1025 -0
- package/lib/iteration/auto-reviewer.js +923 -0
- package/lib/iteration/controller.js +577 -0
- package/lib/list.js +130 -0
- package/lib/mcp-server.js +548 -0
- package/lib/openclaw/api-integration.js +535 -0
- package/lib/openclaw/cli-integration.js +567 -0
- package/lib/openclaw/data-sync.js +845 -0
- package/lib/openclaw/openclaw-adapter.js +783 -0
- package/lib/plugin/example-plugins/code-stats/index.js +332 -0
- package/lib/plugin/example-plugins/code-stats/plugin.json +1 -0
- package/lib/plugin/example-plugins/custom-linter/index.js +472 -0
- package/lib/plugin/example-plugins/custom-linter/plugin.json +1 -0
- package/lib/plugin/example-plugins/hello-world/index.js +86 -0
- package/lib/plugin/example-plugins/hello-world/plugin.json +1 -0
- package/lib/plugin/plugin-manager.js +655 -0
- package/lib/plugin/plugin-sdk.js +565 -0
- package/lib/plugin/sandbox.js +627 -0
- package/lib/quality/rules/maintainability.js +418 -0
- package/lib/quality/rules/performance.js +498 -0
- package/lib/quality/rules/readability.js +441 -0
- package/lib/quality/rules/robustness.js +504 -0
- package/lib/quality/rules/security.js +444 -0
- package/lib/quality/scorer.js +576 -0
- package/lib/report.js +669 -0
- package/lib/sdk-base.js +301 -0
- package/lib/sdk-js.js +446 -0
- package/lib/sdk-python/README.md +546 -0
- package/lib/sdk-python/examples/basic_usage.py +450 -0
- package/lib/sdk-python/pdd_sdk/__init__.py +180 -0
- package/lib/sdk-python/pdd_sdk/client.py +1170 -0
- package/lib/sdk-python/pdd_sdk/events.py +423 -0
- package/lib/sdk-python/pdd_sdk/exceptions.py +158 -0
- package/lib/sdk-python/pdd_sdk/models.py +518 -0
- package/lib/sdk-python/pdd_sdk/utils.py +759 -0
- package/lib/token/budget-alert.js +367 -0
- package/lib/token/budget-manager.js +485 -0
- package/lib/update.js +54 -0
- package/lib/utils/logger.js +88 -0
- package/lib/verify.js +741 -0
- package/lib/version.js +52 -0
- package/lib/vm/README.md +102 -0
- package/lib/vm/dashboard/api-routes.js +669 -0
- package/lib/vm/dashboard/server.js +391 -0
- package/lib/vm/dashboard/sse.js +358 -0
- package/lib/vm/dashboard/static/css/dashboard.css +1378 -0
- package/lib/vm/dashboard/static/index.html +118 -0
- package/lib/vm/dashboard/static/js/app.js +949 -0
- package/lib/vm/dashboard/static/js/charts.js +913 -0
- package/lib/vm/dashboard/static/js/kanban-view.js +1053 -0
- package/lib/vm/dashboard/static/js/pipeline-view.js +463 -0
- package/lib/vm/dashboard/static/js/quality-view.js +598 -0
- package/lib/vm/dashboard/static/js/system-view.js +1021 -0
- package/lib/vm/data-provider.js +1191 -0
- package/lib/vm/event-bus.js +402 -0
- package/lib/vm/hooks/extract-hook.js +307 -0
- package/lib/vm/hooks/generate-hook.js +374 -0
- package/lib/vm/hooks/hook-interface.js +458 -0
- package/lib/vm/hooks/report-hook.js +331 -0
- package/lib/vm/hooks/verify-hook.js +454 -0
- package/lib/vm/models.js +1003 -0
- package/lib/vm/reconciler.js +855 -0
- package/lib/vm/scanner.js +988 -0
- package/lib/vm/state-schema.js +955 -0
- package/lib/vm/state-store.js +733 -0
- package/lib/vm/tui/components/card.js +339 -0
- package/lib/vm/tui/components/progress-bar.js +368 -0
- package/lib/vm/tui/components/sparkline.js +327 -0
- package/lib/vm/tui/components/status-light.js +294 -0
- package/lib/vm/tui/components/table.js +370 -0
- package/lib/vm/tui/input.js +335 -0
- package/lib/vm/tui/renderer.js +548 -0
- package/lib/vm/tui/screens/kanban-screen.js +397 -0
- package/lib/vm/tui/screens/overview-screen.js +357 -0
- package/lib/vm/tui/screens/quality-screen.js +336 -0
- package/lib/vm/tui/screens/system-screen.js +379 -0
- package/lib/vm/tui/tui.js +805 -0
- package/package.json +1 -0
- package/scripts/cso-analyzer.js +198 -0
- package/scripts/eval-runner.js +359 -0
- package/scripts/i18n-checker.js +109 -0
- package/scripts/linter/activiti-linter.js +272 -0
- package/scripts/linter/prd-linter.js +162 -0
- package/scripts/linter/report-generator.js +207 -0
- package/scripts/linter/run-linters.js +285 -0
- package/scripts/linter/sql-linter.js +166 -0
- package/scripts/token-analyzer.js +162 -0
- package/scripts/vm-test.js +180 -0
- package/skills/core/official-doc-writer/LICENSE +21 -0
- package/skills/core/official-doc-writer/README.md +232 -0
- package/skills/core/official-doc-writer/SKILL.md +475 -0
- package/skills/core/official-doc-writer/_meta.json +1 -0
- package/skills/core/official-doc-writer/document_generator.py +580 -0
- package/skills/core/official-doc-writer/evals/default-evals.json +1 -0
- package/skills/core/official-doc-writer/examples.md +150 -0
- package/skills/core/official-doc-writer/fonts/FONTS_LIST.md +45 -0
- package/skills/core/official-doc-writer/fonts/README.md +141 -0
- package/skills/core/official-doc-writer/fonts/SIMFANG.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMHEI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMKAI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMSUN.TTC +0 -0
- package/skills/core/official-doc-writer/fonts//346/226/271/346/255/243/345/260/217/346/240/207/345/256/213GBK.TTF +0 -0
- package/skills/core/official-doc-writer/references/GBT_9704-2012_/345/205/232/346/224/277/346/234/272/345/205/263/345/205/254/346/226/207/346/240/274/345/274/217.md +422 -0
- package/skills/core/official-doc-writer/scripts/__pycache__/generate_official_doc.cpython-313.pyc +0 -0
- package/skills/core/official-doc-writer/scripts/dialog_manager.py +564 -0
- package/skills/core/official-doc-writer/scripts/generate_official_doc.py +252 -0
- package/skills/core/official-doc-writer/scripts/install_fonts.py +390 -0
- package/skills/core/official-doc-writer/scripts/smart_prompts.py +363 -0
- package/skills/core/pdd-ba/SKILL.md +305 -0
- package/skills/core/pdd-ba/_meta.json +1 -0
- package/skills/core/pdd-ba/evals/default-evals.json +1 -0
- package/skills/core/pdd-code-reviewer/SKILL.md +378 -0
- package/skills/core/pdd-code-reviewer/_meta.json +1 -0
- package/skills/core/pdd-code-reviewer/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-change/SKILL.md +350 -0
- package/skills/core/pdd-doc-change/_meta.json +1 -0
- package/skills/core/pdd-doc-change/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-gardener/SKILL.md +248 -0
- package/skills/core/pdd-doc-gardener/_meta.json +1 -0
- package/skills/core/pdd-doc-gardener/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/SKILL.md +360 -0
- package/skills/core/pdd-entropy-reduction/_meta.json +1 -0
- package/skills/core/pdd-entropy-reduction/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/references/entropy-report-template.md +287 -0
- package/skills/core/pdd-entropy-reduction/references/golden-principles.md +573 -0
- package/skills/core/pdd-entropy-reduction/scripts/entropy_scan.py +712 -0
- package/skills/core/pdd-extract-features/SKILL.md +320 -0
- package/skills/core/pdd-extract-features/_meta.json +1 -0
- package/skills/core/pdd-extract-features/evals/default-evals.json +1 -0
- package/skills/core/pdd-generate-spec/SKILL.md +418 -0
- package/skills/core/pdd-generate-spec/_meta.json +1 -0
- package/skills/core/pdd-generate-spec/evals/default-evals.json +1 -0
- package/skills/core/pdd-implement-feature/SKILL.md +332 -0
- package/skills/core/pdd-implement-feature/_meta.json +1 -0
- package/skills/core/pdd-implement-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/SKILL.md +540 -0
- package/skills/core/pdd-main/_meta.json +1 -0
- package/skills/core/pdd-main/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/evals/evals.json +215 -0
- package/skills/core/pdd-verify-feature/SKILL.md +474 -0
- package/skills/core/pdd-verify-feature/_meta.json +1 -0
- package/skills/core/pdd-verify-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-vm/evals/default-evals.json +1 -0
- package/skills/core/traffic-accident-assessor/LICENSE +29 -0
- package/skills/core/traffic-accident-assessor/SKILL.md +439 -0
- package/skills/core/traffic-accident-assessor/evals/evals.json +1 -0
- package/skills/core/traffic-accident-assessor/references/accident-types.md +369 -0
- package/skills/core/traffic-accident-assessor/references/liability-rules.md +287 -0
- package/skills/core/traffic-accident-assessor/references/traffic-laws.md +226 -0
- package/skills/core/traffic-accident-assessor/references//351/253/230/345/260/224/345/244/253/350/257/264/346/230/216/344/271/246.pdf +32576 -106
- package/skills/core/traffic-accident-assessor/scripts/generate_official_statement.py +588 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_report.py +495 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_statement.py +528 -0
- package/skills/core/traffic-accident-assessor.zip +0 -0
- package/skills/entropy/expert-arch-enforcer/SKILL.md +292 -0
- package/skills/entropy/expert-arch-enforcer/_meta.json +1 -0
- package/skills/entropy/expert-arch-enforcer/evals/default-evals.json +1 -0
- package/skills/entropy/expert-auto-refactor/SKILL.md +327 -0
- package/skills/entropy/expert-auto-refactor/_meta.json +1 -0
- package/skills/entropy/expert-auto-refactor/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/SKILL.md +468 -0
- package/skills/entropy/expert-code-quality/_meta.json +1 -0
- package/skills/entropy/expert-code-quality/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/evals/evals.json +109 -0
- package/skills/entropy/expert-code-quality/references/code-smells.md +605 -0
- package/skills/entropy/expert-code-quality/references/design-patterns.md +1111 -0
- package/skills/entropy/expert-code-quality/references/refactoring-catalog.md +1281 -0
- package/skills/entropy/expert-code-quality/references/solid-principles.md +524 -0
- package/skills/entropy/expert-entropy-auditor/SKILL.md +276 -0
- package/skills/entropy/expert-entropy-auditor/_meta.json +1 -0
- package/skills/entropy/expert-entropy-auditor/evals/default-evals.json +1 -0
- package/skills/expert/expert-activiti/SKILL.md +497 -0
- package/skills/expert/expert-activiti/_meta.json +1 -0
- package/skills/expert/expert-mysql/SKILL.md +832 -0
- package/skills/expert/expert-mysql/_meta.json +1 -0
- package/skills/expert/expert-performance/SKILL.md +379 -0
- package/skills/expert/expert-performance/_meta.json +1 -0
- package/skills/expert/expert-performance/evals/default-evals.json +1 -0
- package/skills/expert/expert-ruoyi/SKILL.md +472 -0
- package/skills/expert/expert-ruoyi/_meta.json +1 -0
- package/skills/expert/expert-security/SKILL.md +1341 -0
- package/skills/expert/expert-security/_meta.json +1 -0
- package/skills/expert/expert-security/evals/default-evals.json +1 -0
- package/skills/expert/software-architect/SKILL.md +350 -0
- package/skills/expert/software-architect/_meta.json +1 -0
- package/skills/expert/software-engineer/SKILL.md +437 -0
- package/skills/expert/software-engineer/_meta.json +1 -0
- package/skills/expert/software-engineer/architecture.md +130 -0
- package/skills/expert/software-engineer/patterns.md +151 -0
- package/skills/expert/software-engineer/testing.md +135 -0
- package/skills/expert/system-architect/SKILL.md +628 -0
- package/skills/expert/system-architect/_meta.json +1 -0
- package/skills/expert/system-architect/assets/templates/ARCHITECTURE.md +25 -0
- package/skills/expert/system-architect/assets/templates/README.md +44 -0
- package/skills/expert/system-architect/references/js-ts-standards.md +18 -0
- package/skills/expert/system-architect/references/python-standards.md +19 -0
- package/skills/expert/system-architect/references/scaffolding.md +61 -0
- package/skills/expert/system-architect/references/security-checklist.md +21 -0
- package/skills/openspec/openspec-apply-change/SKILL.md +156 -0
- package/skills/openspec/openspec-apply-change/_meta.json +1 -0
- package/skills/openspec/openspec-archive-change/SKILL.md +114 -0
- package/skills/openspec/openspec-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-bulk-archive-change/SKILL.md +246 -0
- package/skills/openspec/openspec-bulk-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-continue-change/SKILL.md +118 -0
- package/skills/openspec/openspec-continue-change/_meta.json +1 -0
- package/skills/openspec/openspec-explore/SKILL.md +288 -0
- package/skills/openspec/openspec-explore/_meta.json +1 -0
- package/skills/openspec/openspec-ff-change/SKILL.md +101 -0
- package/skills/openspec/openspec-ff-change/_meta.json +1 -0
- package/skills/openspec/openspec-new-change/SKILL.md +74 -0
- package/skills/openspec/openspec-new-change/_meta.json +1 -0
- package/skills/openspec/openspec-onboard/SKILL.md +554 -0
- package/skills/openspec/openspec-onboard/_meta.json +1 -0
- package/skills/openspec/openspec-sync-specs/SKILL.md +138 -0
- package/skills/openspec/openspec-sync-specs/_meta.json +1 -0
- package/skills/openspec/openspec-verify-change/SKILL.md +168 -0
- package/skills/openspec/openspec-verify-change/_meta.json +1 -0
- package/skills/pr/pdd-multi-review/SKILL.md +534 -0
- package/skills/pr/pdd-multi-review/_meta.json +1 -0
- package/skills/pr/pdd-pr-batch/SKILL.md +303 -0
- package/skills/pr/pdd-pr-batch/_meta.json +1 -0
- package/skills/pr/pdd-pr-create/SKILL.md +344 -0
- package/skills/pr/pdd-pr-create/_meta.json +1 -0
- package/skills/pr/pdd-pr-merge/SKILL.md +286 -0
- package/skills/pr/pdd-pr-merge/_meta.json +1 -0
- package/skills/pr/pdd-pr-review/SKILL.md +217 -0
- package/skills/pr/pdd-pr-review/_meta.json +1 -0
- package/skills/pr/pdd-task-manager/SKILL.md +636 -0
- package/skills/pr/pdd-task-manager/_meta.json +1 -0
- package/skills/pr/pdd-template-engine/SKILL.md +306 -0
- package/skills/pr/pdd-template-engine/_meta.json +1 -0
- package/templates/behavior-shaping/iron-law-template.md +87 -0
- package/templates/behavior-shaping/rationalization-template.md +62 -0
- package/templates/behavior-shaping/red-flags-template.md +70 -0
- package/templates/bilingual-template.md +139 -0
- package/templates/config/default.yaml +47 -0
- package/templates/project/default/README.md +31 -0
- package/templates/project/frontend/README.md +46 -0
- package/templates/project/java/README.md +48 -0
|
@@ -0,0 +1,913 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Charts - Canvas 图表引擎
|
|
3
|
+
* 提供雷达图、直方图、环形仪表盘、折线图、水平条形图等可视化组件
|
|
4
|
+
* 纯原生 Canvas API,无外部依赖
|
|
5
|
+
*
|
|
6
|
+
* @module Charts
|
|
7
|
+
* @version 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const Charts = {
|
|
11
|
+
// ==================== 通用工具方法 ====================
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 清空画布
|
|
15
|
+
* @param {string} canvasId - Canvas 元素 ID
|
|
16
|
+
*/
|
|
17
|
+
clearCanvas(canvasId) {
|
|
18
|
+
const canvas = document.getElementById(canvasId);
|
|
19
|
+
if (!canvas) return;
|
|
20
|
+
const ctx = canvas.getContext('2d');
|
|
21
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 根据评分获取颜色
|
|
26
|
+
* @param {number} score - 评分 (0-100)
|
|
27
|
+
* @returns {string} 颜色值
|
|
28
|
+
*/
|
|
29
|
+
getColor(score) {
|
|
30
|
+
if (score >= 90) return '#27ae60'; // S - 绿色
|
|
31
|
+
if (score >= 80) return '#2980b9'; // A - 蓝色
|
|
32
|
+
if (score >= 70) return '#f39c12'; // B - 黄色
|
|
33
|
+
if (score >= 60) return '#e67e22'; // C - 橙色
|
|
34
|
+
if (score >= 40) return '#e74c3c'; // D - 红色
|
|
35
|
+
return '#c0392b'; // F - 深红
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 获取等级标签
|
|
40
|
+
* @param {number} score - 评分
|
|
41
|
+
* @returns {string} 等级 (S/A/B/C/D/F)
|
|
42
|
+
*/
|
|
43
|
+
getGrade(score) {
|
|
44
|
+
if (score >= 90) return 'S';
|
|
45
|
+
if (score >= 80) return 'A';
|
|
46
|
+
if (score >= 70) return 'B';
|
|
47
|
+
if (score >= 60) return 'C';
|
|
48
|
+
if (score >= 40) return 'D';
|
|
49
|
+
return 'F';
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 动画插值
|
|
54
|
+
* @param {number} start - 起始值
|
|
55
|
+
* @param {number} end - 结束值
|
|
56
|
+
* @param {number} progress - 进度 (0-1)
|
|
57
|
+
* @param {string} easing - 缓动函数类型
|
|
58
|
+
* @returns {number}
|
|
59
|
+
*/
|
|
60
|
+
_ease(start, end, progress, easing = 'easeOutCubic') {
|
|
61
|
+
const delta = end - start;
|
|
62
|
+
switch (easing) {
|
|
63
|
+
case 'linear':
|
|
64
|
+
return start + delta * progress;
|
|
65
|
+
case 'easeOutCubic':
|
|
66
|
+
return start + delta * (1 - Math.pow(1 - progress, 3));
|
|
67
|
+
case 'easeInOutCubic':
|
|
68
|
+
return start + delta * (progress < 0.5
|
|
69
|
+
? 4 * progress * progress * progress
|
|
70
|
+
: 1 - Math.pow(-2 * progress + 2, 3) / 2);
|
|
71
|
+
default:
|
|
72
|
+
return start + delta * progress;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
// ==================== 雷达图 ====================
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 绘制五维雷达图
|
|
80
|
+
* @param {string} canvasId - Canvas 元素 ID
|
|
81
|
+
* @param {Object} data - 数据对象
|
|
82
|
+
* @param {string[]} data.labels - 标签数组(如 ['可读性','可维护性','健壮性','性能','安全性'])
|
|
83
|
+
* @param {number[]} data.values - 值数组 (0-100)
|
|
84
|
+
* @param {Object} options - 配置选项
|
|
85
|
+
* @param {number} [options.maxVal=100] - 最大值
|
|
86
|
+
* @param {string[]} [options.colors] - 颜色数组
|
|
87
|
+
* @param {number} [options.fillAlpha=0.25] - 填充透明度
|
|
88
|
+
* @param {boolean} [options.animate=true] - 是否启用动画
|
|
89
|
+
* @param {Function} [options.onHover] - 悬停回调
|
|
90
|
+
*/
|
|
91
|
+
radar(canvasId, data, options = {}) {
|
|
92
|
+
const canvas = document.getElementById(canvasId);
|
|
93
|
+
if (!canvas || !data.labels || !data.values) return;
|
|
94
|
+
|
|
95
|
+
const ctx = canvas.getContext('2d');
|
|
96
|
+
const width = canvas.width;
|
|
97
|
+
const height = canvas.height;
|
|
98
|
+
const centerX = width / 2;
|
|
99
|
+
const centerY = height / 2;
|
|
100
|
+
const radius = Math.min(width, height) / 2 - 50;
|
|
101
|
+
|
|
102
|
+
const {
|
|
103
|
+
maxVal = 100,
|
|
104
|
+
colors = ['#3498db', '#2ecc71'],
|
|
105
|
+
fillAlpha = 0.25,
|
|
106
|
+
animate = true,
|
|
107
|
+
onHover = null
|
|
108
|
+
} = options;
|
|
109
|
+
|
|
110
|
+
const numPoints = data.labels.length;
|
|
111
|
+
const angleStep = (Math.PI * 2) / numPoints;
|
|
112
|
+
|
|
113
|
+
let animationProgress = animate ? 0 : 1;
|
|
114
|
+
let hoveredIndex = -1;
|
|
115
|
+
|
|
116
|
+
// 绘制背景网格
|
|
117
|
+
function drawGrid() {
|
|
118
|
+
ctx.strokeStyle = '#ecf0f1';
|
|
119
|
+
ctx.lineWidth = 1;
|
|
120
|
+
|
|
121
|
+
// 同心多边形网格
|
|
122
|
+
for (let level = 1; level <= 5; level++) {
|
|
123
|
+
const r = (radius / 5) * level;
|
|
124
|
+
ctx.beginPath();
|
|
125
|
+
for (let i = 0; i < numPoints; i++) {
|
|
126
|
+
const angle = angleStep * i - Math.PI / 2;
|
|
127
|
+
const x = centerX + Math.cos(angle) * r;
|
|
128
|
+
const y = centerY + Math.sin(angle) * r;
|
|
129
|
+
if (i === 0) ctx.moveTo(x, y);
|
|
130
|
+
else ctx.lineTo(x, y);
|
|
131
|
+
}
|
|
132
|
+
ctx.closePath();
|
|
133
|
+
ctx.stroke();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 从中心到各顶点的轴线
|
|
137
|
+
for (let i = 0; i < numPoints; i++) {
|
|
138
|
+
const angle = angleStep * i - Math.PI / 2;
|
|
139
|
+
ctx.beginPath();
|
|
140
|
+
ctx.moveTo(centerX, centerY);
|
|
141
|
+
ctx.lineTo(
|
|
142
|
+
centerX + Math.cos(angle) * radius,
|
|
143
|
+
centerY + Math.sin(angle) * radius
|
|
144
|
+
);
|
|
145
|
+
ctx.stroke();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 标签
|
|
149
|
+
ctx.fillStyle = '#34495e';
|
|
150
|
+
ctx.font = '12px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif';
|
|
151
|
+
ctx.textAlign = 'center';
|
|
152
|
+
ctx.textBaseline = 'middle';
|
|
153
|
+
|
|
154
|
+
for (let i = 0; i < numPoints; i++) {
|
|
155
|
+
const angle = angleStep * i - Math.PI / 2;
|
|
156
|
+
const labelRadius = radius + 25;
|
|
157
|
+
const x = centerX + Math.cos(angle) * labelRadius;
|
|
158
|
+
const y = centerY + Math.sin(angle) * labelRadius;
|
|
159
|
+
ctx.fillText(data.labels[i], x, y);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// 绘制数据区域
|
|
164
|
+
function drawData(progress) {
|
|
165
|
+
ctx.beginPath();
|
|
166
|
+
for (let i = 0; i < numPoints; i++) {
|
|
167
|
+
const value = this._ease(0, data.values[i], progress);
|
|
168
|
+
const normalizedValue = Math.min(value, maxVal) / maxVal;
|
|
169
|
+
const angle = angleStep * i - Math.PI / 2;
|
|
170
|
+
const r = radius * normalizedValue;
|
|
171
|
+
const x = centerX + Math.cos(angle) * r;
|
|
172
|
+
const y = centerY + Math.sin(angle) * r;
|
|
173
|
+
|
|
174
|
+
if (i === 0) ctx.moveTo(x, y);
|
|
175
|
+
else ctx.lineTo(x, y);
|
|
176
|
+
}
|
|
177
|
+
ctx.closePath();
|
|
178
|
+
|
|
179
|
+
// 填充
|
|
180
|
+
ctx.fillStyle = colors[0] + Math.round(fillAlpha * 255).toString(16).padStart(2, '0');
|
|
181
|
+
ctx.fill();
|
|
182
|
+
|
|
183
|
+
// 边框
|
|
184
|
+
ctx.strokeStyle = colors[0];
|
|
185
|
+
ctx.lineWidth = 2;
|
|
186
|
+
ctx.stroke();
|
|
187
|
+
|
|
188
|
+
// 数据点
|
|
189
|
+
for (let i = 0; i < numPoints; i++) {
|
|
190
|
+
const value = this._ease(0, data.values[i], progress);
|
|
191
|
+
const normalizedValue = Math.min(value, maxVal) / maxVal;
|
|
192
|
+
const angle = angleStep * i - Math.PI / 2;
|
|
193
|
+
const r = radius * normalizedValue;
|
|
194
|
+
const x = centerX + Math.cos(angle) * r;
|
|
195
|
+
const y = centerY + Math.sin(angle) * r;
|
|
196
|
+
|
|
197
|
+
ctx.beginPath();
|
|
198
|
+
ctx.arc(x, y, i === hoveredIndex ? 6 : 4, 0, Math.PI * 2);
|
|
199
|
+
ctx.fillStyle = colors[0];
|
|
200
|
+
ctx.fill();
|
|
201
|
+
ctx.strokeStyle = '#fff';
|
|
202
|
+
ctx.lineWidth = 2;
|
|
203
|
+
ctx.stroke();
|
|
204
|
+
|
|
205
|
+
// 显示数值
|
|
206
|
+
if (progress >= 1 || i === hoveredIndex) {
|
|
207
|
+
ctx.fillStyle = '#2c3e50';
|
|
208
|
+
ctx.font = 'bold 11px -apple-system, sans-serif';
|
|
209
|
+
ctx.textAlign = 'center';
|
|
210
|
+
ctx.fillText(Math.round(data.values[i]), x, y - 12);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}.bind(this);
|
|
214
|
+
|
|
215
|
+
// 主绘制函数
|
|
216
|
+
function render() {
|
|
217
|
+
ctx.clearRect(0, 0, width, height);
|
|
218
|
+
drawGrid();
|
|
219
|
+
drawData(animationProgress);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 动画循环
|
|
223
|
+
if (animate && animationProgress < 1) {
|
|
224
|
+
const startTime = performance.now();
|
|
225
|
+
const duration = 800;
|
|
226
|
+
|
|
227
|
+
function animateFrame(currentTime) {
|
|
228
|
+
animationProgress = Math.min((currentTime - startTime) / duration, 1);
|
|
229
|
+
render();
|
|
230
|
+
|
|
231
|
+
if (animationProgress < 1) {
|
|
232
|
+
requestAnimationFrame(animateFrame);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
requestAnimationFrame(animateFrame);
|
|
237
|
+
} else {
|
|
238
|
+
render();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 鼠标悬停事件
|
|
242
|
+
if (onHover) {
|
|
243
|
+
canvas.addEventListener('mousemove', (e) => {
|
|
244
|
+
const rect = canvas.getBoundingClientRect();
|
|
245
|
+
const mouseX = e.clientX - rect.left;
|
|
246
|
+
const mouseY = e.clientY - rect.top;
|
|
247
|
+
|
|
248
|
+
let newHoveredIndex = -1;
|
|
249
|
+
for (let i = 0; i < numPoints; i++) {
|
|
250
|
+
const normalizedValue = Math.min(data.values[i], maxVal) / maxVal;
|
|
251
|
+
const angle = angleStep * i - Math.PI / 2;
|
|
252
|
+
const r = radius * normalizedValue;
|
|
253
|
+
const x = centerX + Math.cos(angle) * r;
|
|
254
|
+
const y = centerY + Math.sin(angle) * r;
|
|
255
|
+
|
|
256
|
+
const distance = Math.sqrt(Math.pow(mouseX - x, 2) + Math.pow(mouseY - y, 2));
|
|
257
|
+
if (distance < 10) {
|
|
258
|
+
newHoveredIndex = i;
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (newHoveredIndex !== hoveredIndex) {
|
|
264
|
+
hoveredIndex = newHoveredIndex;
|
|
265
|
+
render();
|
|
266
|
+
onHover(hoveredIndex, hoveredIndex >= 0 ? {
|
|
267
|
+
label: data.labels[hoveredIndex],
|
|
268
|
+
value: data.values[hoveredIndex]
|
|
269
|
+
} : null);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
canvas.style.cursor = hoveredIndex >= 0 ? 'pointer' : 'default';
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
canvas.addEventListener('mouseleave', () => {
|
|
276
|
+
hoveredIndex = -1;
|
|
277
|
+
render();
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
// ==================== 直方图 ====================
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 绘制直方图/柱状图
|
|
286
|
+
* @param {string} canvasId - Canvas 元素 ID
|
|
287
|
+
* @param {Object} data - 数据对象
|
|
288
|
+
* @param {string[]} data.labels - 标签数组
|
|
289
|
+
* @param {number[]} data.values - 值数组
|
|
290
|
+
* @param {Object} options - 配置选项
|
|
291
|
+
* @param {string[]} [options.colors] - 柱子颜色数组
|
|
292
|
+
* @param {boolean} [options.animate=true] - 是否动画
|
|
293
|
+
* @param {Function} [options.onClick] - 点击回调
|
|
294
|
+
* @param {string} [options.label=''] - Y轴标签
|
|
295
|
+
*/
|
|
296
|
+
histogram(canvasId, data, options = {}) {
|
|
297
|
+
const canvas = document.getElementById(canvasId);
|
|
298
|
+
if (!canvas || !data.labels || !data.values) return;
|
|
299
|
+
|
|
300
|
+
const ctx = canvas.getContext('2d');
|
|
301
|
+
const width = canvas.width;
|
|
302
|
+
const height = canvas.height;
|
|
303
|
+
const padding = { top: 30, right: 20, bottom: 50, left: 50 };
|
|
304
|
+
|
|
305
|
+
const {
|
|
306
|
+
colors = ['#27ae60', '#2980b9', '#f39c12', '#e67e22', '#e74c3c', '#c0392b'],
|
|
307
|
+
animate = true,
|
|
308
|
+
onClick = null,
|
|
309
|
+
label = ''
|
|
310
|
+
} = options;
|
|
311
|
+
|
|
312
|
+
const chartWidth = width - padding.left - padding.right;
|
|
313
|
+
const chartHeight = height - padding.top - padding.bottom;
|
|
314
|
+
const barCount = data.labels.length;
|
|
315
|
+
const barWidth = chartWidth / barCount * 0.7;
|
|
316
|
+
const barGap = chartWidth / barCount * 0.3;
|
|
317
|
+
const maxValue = Math.max(...data.values, 1);
|
|
318
|
+
|
|
319
|
+
let animationProgress = animate ? 0 : 1;
|
|
320
|
+
|
|
321
|
+
function render() {
|
|
322
|
+
ctx.clearRect(0, 0, width, height);
|
|
323
|
+
|
|
324
|
+
// Y轴网格线和刻度
|
|
325
|
+
ctx.strokeStyle = '#ecf0f1';
|
|
326
|
+
ctx.lineWidth = 1;
|
|
327
|
+
ctx.fillStyle = '#7f8c8d';
|
|
328
|
+
ctx.font = '11px -apple-system, sans-serif';
|
|
329
|
+
ctx.textAlign = 'right';
|
|
330
|
+
|
|
331
|
+
const gridLines = 5;
|
|
332
|
+
for (let i = 0; i <= gridLines; i++) {
|
|
333
|
+
const y = padding.top + (chartHeight / gridLines) * i;
|
|
334
|
+
const value = Math.round(maxValue - (maxValue / gridLines) * i);
|
|
335
|
+
|
|
336
|
+
ctx.beginPath();
|
|
337
|
+
ctx.moveTo(padding.left, y);
|
|
338
|
+
ctx.lineTo(width - padding.right, y);
|
|
339
|
+
ctx.stroke();
|
|
340
|
+
|
|
341
|
+
ctx.fillText(value.toString(), padding.left - 10, y + 4);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Y轴标签
|
|
345
|
+
if (label) {
|
|
346
|
+
ctx.save();
|
|
347
|
+
ctx.translate(15, height / 2);
|
|
348
|
+
ctx.rotate(-Math.PI / 2);
|
|
349
|
+
ctx.textAlign = 'center';
|
|
350
|
+
ctx.fillStyle = '#34495e';
|
|
351
|
+
ctx.font = '12px -apple-system, sans-serif';
|
|
352
|
+
ctx.fillText(label, 0, 0);
|
|
353
|
+
ctx.restore();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// 绘制柱子
|
|
357
|
+
data.values.forEach((value, index) => {
|
|
358
|
+
const barHeight = (value / maxValue) * chartHeight * animationProgress;
|
|
359
|
+
const x = padding.left + (chartWidth / barCount) * index + barGap / 2;
|
|
360
|
+
const y = padding.top + chartHeight - barHeight;
|
|
361
|
+
const color = colors[index % colors.length];
|
|
362
|
+
|
|
363
|
+
// 柱子渐变
|
|
364
|
+
const gradient = ctx.createLinearGradient(x, y, x, y + barHeight);
|
|
365
|
+
gradient.addColorStop(0, color);
|
|
366
|
+
gradient.addColorStop(1, this._adjustBrightness(color, -20));
|
|
367
|
+
|
|
368
|
+
ctx.fillStyle = gradient;
|
|
369
|
+
ctx.beginPath();
|
|
370
|
+
this._roundRect(ctx, x, y, barWidth, barHeight, 4);
|
|
371
|
+
ctx.fill();
|
|
372
|
+
|
|
373
|
+
// 数值标签
|
|
374
|
+
if (animationProgress >= 1) {
|
|
375
|
+
ctx.fillStyle = '#2c3e50';
|
|
376
|
+
ctx.font = 'bold 12px -apple-system, sans-serif';
|
|
377
|
+
ctx.textAlign = 'center';
|
|
378
|
+
ctx.fillText(value.toString(), x + barWidth / 2, y - 8);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// X轴标签
|
|
382
|
+
ctx.fillStyle = '#34495e';
|
|
383
|
+
ctx.font = '12px -apple-system, sans-serif';
|
|
384
|
+
ctx.textAlign = 'center';
|
|
385
|
+
ctx.fillText(data.labels[index], x + barWidth / 2, height - padding.bottom + 20);
|
|
386
|
+
});
|
|
387
|
+
}.bind(this);
|
|
388
|
+
|
|
389
|
+
// 动画
|
|
390
|
+
if (animate) {
|
|
391
|
+
const startTime = performance.now();
|
|
392
|
+
const duration = 600;
|
|
393
|
+
|
|
394
|
+
function animateFrame(currentTime) {
|
|
395
|
+
animationProgress = Math.min((currentTime - startTime) / duration, 1);
|
|
396
|
+
render();
|
|
397
|
+
|
|
398
|
+
if (animationProgress < 1) {
|
|
399
|
+
requestAnimationFrame(animateFrame);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
requestAnimationFrame(animateFrame);
|
|
404
|
+
} else {
|
|
405
|
+
render();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// 点击事件
|
|
409
|
+
if (onClick) {
|
|
410
|
+
canvas.addEventListener('click', (e) => {
|
|
411
|
+
const rect = canvas.getBoundingClientRect();
|
|
412
|
+
const mouseX = e.clientX - rect.left;
|
|
413
|
+
|
|
414
|
+
if (mouseX >= padding.left && mouseX <= width - padding.right) {
|
|
415
|
+
const index = Math.floor((mouseX - padding.left) / (chartWidth / barCount));
|
|
416
|
+
if (index >= 0 && index < barCount) {
|
|
417
|
+
onClick(index, data.labels[index], data.values[index]);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
canvas.style.cursor = 'pointer';
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
|
|
426
|
+
// ==================== 环形进度仪表盘 ====================
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 绘制环形进度仪表盘
|
|
430
|
+
* @param {string} canvasId - Canvas 元素 ID
|
|
431
|
+
* @param {number} percent - 百分比 (0-100)
|
|
432
|
+
* @param {Object} options - 配置选项
|
|
433
|
+
* @param {string} [options.color='#3498db'] - 进度颜色
|
|
434
|
+
* @param {string} [options.bgColor='#ecf0f1'] - 背景颜色
|
|
435
|
+
* @param {number} [options.lineWidth=12] - 线宽
|
|
436
|
+
* @param {string} [options.label=''] - 中心标签文字
|
|
437
|
+
* @param {Object} [options.thresholdColors] - 阈值颜色配置
|
|
438
|
+
* @param {boolean} [options.animate=true] - 是否动画
|
|
439
|
+
*/
|
|
440
|
+
gauge(canvasId, percent, options = {}) {
|
|
441
|
+
const canvas = document.getElementById(canvasId);
|
|
442
|
+
if (!canvas) return;
|
|
443
|
+
|
|
444
|
+
const ctx = canvas.getContext('2d');
|
|
445
|
+
const width = canvas.width;
|
|
446
|
+
const height = canvas.height;
|
|
447
|
+
const centerX = width / 2;
|
|
448
|
+
const centerY = height / 2;
|
|
449
|
+
const radius = Math.min(width, height) / 2 - 20;
|
|
450
|
+
|
|
451
|
+
const {
|
|
452
|
+
color = '#3498db',
|
|
453
|
+
bgColor = '#ecf0f1',
|
|
454
|
+
lineWidth = 12,
|
|
455
|
+
label = '',
|
|
456
|
+
thresholdColors = { warning: '#f39c12', danger: '#e74c3c', warningThreshold: 80, dangerThreshold: 95 },
|
|
457
|
+
animate = true
|
|
458
|
+
} = options;
|
|
459
|
+
|
|
460
|
+
const clampedPercent = Math.min(100, Math.max(0, percent));
|
|
461
|
+
let animationProgress = animate ? 0 : 1;
|
|
462
|
+
|
|
463
|
+
// 根据阈值选择颜色
|
|
464
|
+
function getGaugeColor(pct) {
|
|
465
|
+
if (pct >= thresholdColors.dangerThreshold) return thresholdColors.danger;
|
|
466
|
+
if (pct >= thresholdColors.warningThreshold) return thresholdColors.warning;
|
|
467
|
+
return color;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function render() {
|
|
471
|
+
ctx.clearRect(0, 0, width, height);
|
|
472
|
+
|
|
473
|
+
const currentPercent = clampedPercent * animationProgress;
|
|
474
|
+
const startAngle = -Math.PI / 2;
|
|
475
|
+
const endAngle = startAngle + (Math.PI * 2 * currentPercent / 100);
|
|
476
|
+
|
|
477
|
+
// 背景圆环
|
|
478
|
+
ctx.beginPath();
|
|
479
|
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
480
|
+
ctx.strokeStyle = bgColor;
|
|
481
|
+
ctx.lineWidth = lineWidth;
|
|
482
|
+
ctx.lineCap = 'round';
|
|
483
|
+
ctx.stroke();
|
|
484
|
+
|
|
485
|
+
// 进度圆环
|
|
486
|
+
if (currentPercent > 0) {
|
|
487
|
+
ctx.beginPath();
|
|
488
|
+
ctx.arc(centerX, centerY, radius, startAngle, endAngle);
|
|
489
|
+
ctx.strokeStyle = getGaugeColor(clampedPercent);
|
|
490
|
+
ctx.lineWidth = lineWidth;
|
|
491
|
+
ctx.lineCap = 'round';
|
|
492
|
+
ctx.stroke();
|
|
493
|
+
|
|
494
|
+
// 渐变端点圆
|
|
495
|
+
const endX = centerX + Math.cos(endAngle) * radius;
|
|
496
|
+
const endY = centerY + Math.sin(endAngle) * radius;
|
|
497
|
+
ctx.beginPath();
|
|
498
|
+
ctx.arc(endX, endY, lineWidth / 2, 0, Math.PI * 2);
|
|
499
|
+
ctx.fillStyle = getGaugeColor(clampedPercent);
|
|
500
|
+
ctx.fill();
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// 中心文字
|
|
504
|
+
const displayPercent = Math.round(clampedPercent * animationProgress);
|
|
505
|
+
ctx.fillStyle = '#2c3e50';
|
|
506
|
+
ctx.font = `bold ${radius * 0.35}px -apple-system, sans-serif`;
|
|
507
|
+
ctx.textAlign = 'center';
|
|
508
|
+
ctx.textBaseline = 'middle';
|
|
509
|
+
ctx.fillText(`${displayPercent}%`, centerX, centerY - (label ? 10 : 0));
|
|
510
|
+
|
|
511
|
+
// 标签
|
|
512
|
+
if (label) {
|
|
513
|
+
ctx.fillStyle = '#7f8c8d';
|
|
514
|
+
ctx.font = `${radius * 0.14}px -apple-system, sans-serif`;
|
|
515
|
+
ctx.fillText(label, centerX, centerY + radius * 0.2);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// 动画
|
|
520
|
+
if (animate) {
|
|
521
|
+
const startTime = performance.now();
|
|
522
|
+
const duration = 1000;
|
|
523
|
+
|
|
524
|
+
function animateFrame(currentTime) {
|
|
525
|
+
animationProgress = Math.min((currentTime - startTime) / duration, 1);
|
|
526
|
+
// 使用 easeOutElastic 效果
|
|
527
|
+
animationProgress = animationProgress === 1 ? 1 :
|
|
528
|
+
Math.pow(2, -10 * animationProgress) * Math.sin((animationTime - 0.075) * (2 * Math.PI) / 0.3) + 1;
|
|
529
|
+
render();
|
|
530
|
+
|
|
531
|
+
if (animationProgress < 1) {
|
|
532
|
+
requestAnimationFrame(animateFrame);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
let animationTime = 0;
|
|
537
|
+
function wrapper(currentTime) {
|
|
538
|
+
if (!startTime) startTime = currentTime;
|
|
539
|
+
animationTime = (currentTime - startTime) / duration;
|
|
540
|
+
animateFrame(currentTime);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
requestAnimationFrame(wrapper);
|
|
544
|
+
} else {
|
|
545
|
+
render();
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
// ==================== 折线图 ====================
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* 绘制折线图
|
|
553
|
+
* @param {string} canvasId - Canvas 元素 ID
|
|
554
|
+
* @param {Object} data - 数据对象
|
|
555
|
+
* @param {string[]} data.labels - X轴标签
|
|
556
|
+
* @param {Array} data.datasets - 数据集数组,每项包含 {label, values[], color}
|
|
557
|
+
* @param {Object} options - 配置选项
|
|
558
|
+
* @param {number} [options.threshold] - 收敛阈值线
|
|
559
|
+
* @param {string} [options.thresholdLabel=''] - 阈值标签
|
|
560
|
+
* @param {boolean} [options.animate=true] - 是否动画
|
|
561
|
+
* @param {boolean} [options.showPoints=true] - 是否显示数据点
|
|
562
|
+
* @param {boolean} [options.fillArea=false] - 是否填充区域
|
|
563
|
+
*/
|
|
564
|
+
lineChart(canvasId, data, options = {}) {
|
|
565
|
+
const canvas = document.getElementById(canvasId);
|
|
566
|
+
if (!canvas || !data.labels || !data.datasets) return;
|
|
567
|
+
|
|
568
|
+
const ctx = canvas.getContext('2d');
|
|
569
|
+
const width = canvas.width;
|
|
570
|
+
const height = canvas.height;
|
|
571
|
+
const padding = { top: 30, right: 20, bottom: 50, left: 50 };
|
|
572
|
+
|
|
573
|
+
const {
|
|
574
|
+
threshold = null,
|
|
575
|
+
thresholdLabel = '',
|
|
576
|
+
animate = true,
|
|
577
|
+
showPoints = true,
|
|
578
|
+
fillArea = false
|
|
579
|
+
} = options;
|
|
580
|
+
|
|
581
|
+
const chartWidth = width - padding.left - padding.right;
|
|
582
|
+
const chartHeight = height - padding.top - padding.bottom;
|
|
583
|
+
const pointCount = data.labels.length;
|
|
584
|
+
|
|
585
|
+
// 计算所有数据集的最大最小值
|
|
586
|
+
let allValues = [];
|
|
587
|
+
data.datasets.forEach(ds => allValues.push(...ds.values));
|
|
588
|
+
if (threshold) allValues.push(threshold);
|
|
589
|
+
const maxValue = Math.max(...allValues, 1);
|
|
590
|
+
const minValue = Math.min(...allValues, 0);
|
|
591
|
+
const valueRange = maxValue - minValue || 1;
|
|
592
|
+
|
|
593
|
+
let animationProgress = animate ? 0 : 1;
|
|
594
|
+
|
|
595
|
+
function getX(index) {
|
|
596
|
+
return padding.left + (index / (pointCount - 1 || 1)) * chartWidth;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function getY(value) {
|
|
600
|
+
return padding.top + chartHeight - ((value - minValue) / valueRange) * chartHeight;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function render() {
|
|
604
|
+
ctx.clearRect(0, 0, width, height);
|
|
605
|
+
|
|
606
|
+
// 网格和Y轴
|
|
607
|
+
ctx.strokeStyle = '#ecf0f1';
|
|
608
|
+
ctx.lineWidth = 1;
|
|
609
|
+
ctx.fillStyle = '#7f8c8d';
|
|
610
|
+
ctx.font = '11px -apple-system, sans-serif';
|
|
611
|
+
ctx.textAlign = 'right';
|
|
612
|
+
|
|
613
|
+
const gridLines = 5;
|
|
614
|
+
for (let i = 0; i <= gridLines; i++) {
|
|
615
|
+
const y = padding.top + (chartHeight / gridLines) * i;
|
|
616
|
+
const value = maxValue - ((maxValue - minValue) / gridLines) * i;
|
|
617
|
+
|
|
618
|
+
ctx.beginPath();
|
|
619
|
+
ctx.moveTo(padding.left, y);
|
|
620
|
+
ctx.lineTo(width - padding.right, y);
|
|
621
|
+
ctx.stroke();
|
|
622
|
+
|
|
623
|
+
ctx.fillText(Math.round(value).toString(), padding.left - 10, y + 4);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// X轴标签
|
|
627
|
+
ctx.textAlign = 'center';
|
|
628
|
+
ctx.fillStyle = '#34495e';
|
|
629
|
+
data.labels.forEach((label, i) => {
|
|
630
|
+
ctx.fillText(label, getX(i), height - padding.bottom + 20);
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
// 阈值线
|
|
634
|
+
if (threshold !== null) {
|
|
635
|
+
const thresholdY = getY(threshold);
|
|
636
|
+
ctx.beginPath();
|
|
637
|
+
ctx.setLineDash([6, 4]);
|
|
638
|
+
ctx.moveTo(padding.left, thresholdY);
|
|
639
|
+
ctx.lineTo(width - padding.right, thresholdY);
|
|
640
|
+
ctx.strokeStyle = '#e74c3c';
|
|
641
|
+
ctx.lineWidth = 2;
|
|
642
|
+
ctx.stroke();
|
|
643
|
+
ctx.setLineDash([]);
|
|
644
|
+
|
|
645
|
+
// 阈值标签
|
|
646
|
+
ctx.fillStyle = '#e74c3c';
|
|
647
|
+
ctx.font = '11px -apple-system, sans-serif';
|
|
648
|
+
ctx.textAlign = 'left';
|
|
649
|
+
ctx.fillText(thresholdLabel || `Threshold: ${threshold}`, width - padding.right - 80, thresholdY - 8);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// 绘制每个数据集
|
|
653
|
+
data.datasets.forEach((dataset, dsIndex) => {
|
|
654
|
+
const color = dataset.color || `hsl(${dsIndex * 60}, 70%, 50%)`;
|
|
655
|
+
const animatedLength = Math.floor(pointCount * animationProgress);
|
|
656
|
+
|
|
657
|
+
// 填充区域
|
|
658
|
+
if (fillArea && animatedLength > 0) {
|
|
659
|
+
ctx.beginPath();
|
|
660
|
+
ctx.moveTo(getX(0), getY(0));
|
|
661
|
+
|
|
662
|
+
for (let i = 0; i < animatedLength; i++) {
|
|
663
|
+
ctx.lineTo(getX(i), getY(dataset.values[i]));
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
ctx.lineTo(getX(animatedLength - 1), getY(minValue));
|
|
667
|
+
ctx.lineTo(getX(0), getY(minValue));
|
|
668
|
+
ctx.closePath();
|
|
669
|
+
|
|
670
|
+
ctx.fillStyle = color + '20';
|
|
671
|
+
ctx.fill();
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// 折线
|
|
675
|
+
ctx.beginPath();
|
|
676
|
+
for (let i = 0; i < animatedLength; i++) {
|
|
677
|
+
const x = getX(i);
|
|
678
|
+
const y = getY(dataset.values[i]);
|
|
679
|
+
|
|
680
|
+
if (i === 0) ctx.moveTo(x, y);
|
|
681
|
+
else ctx.lineTo(x, y);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
ctx.strokeStyle = color;
|
|
685
|
+
ctx.lineWidth = 2.5;
|
|
686
|
+
ctx.lineCap = 'round';
|
|
687
|
+
ctx.lineJoin = 'round';
|
|
688
|
+
ctx.stroke();
|
|
689
|
+
|
|
690
|
+
// 数据点
|
|
691
|
+
if (showPoints) {
|
|
692
|
+
for (let i = 0; i < animatedLength; i++) {
|
|
693
|
+
const x =getX(i);
|
|
694
|
+
const y = getY(dataset.values[i]);
|
|
695
|
+
|
|
696
|
+
ctx.beginPath();
|
|
697
|
+
ctx.arc(x, y, 4, 0, Math.PI * 2);
|
|
698
|
+
ctx.fillStyle = '#fff';
|
|
699
|
+
ctx.fill();
|
|
700
|
+
ctx.strokeStyle = color;
|
|
701
|
+
ctx.lineWidth = 2;
|
|
702
|
+
ctx.stroke();
|
|
703
|
+
|
|
704
|
+
// 数值标签
|
|
705
|
+
if (animationProgress >= 1) {
|
|
706
|
+
ctx.fillStyle = '#2c3e50';
|
|
707
|
+
ctx.font = '10px -apple-system, sans-serif';
|
|
708
|
+
ctx.textAlign = 'center';
|
|
709
|
+
ctx.fillText(dataset.values[i].toString(), x, y - 10);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// 动画
|
|
717
|
+
if (animate) {
|
|
718
|
+
const startTime = performance.now();
|
|
719
|
+
const duration = 800;
|
|
720
|
+
|
|
721
|
+
function animateFrame(currentTime) {
|
|
722
|
+
animationProgress = Math.min((currentTime - startTime) / duration, 1);
|
|
723
|
+
render();
|
|
724
|
+
|
|
725
|
+
if (animationProgress < 1) {
|
|
726
|
+
requestAnimationFrame(animateFrame);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
requestAnimationFrame(animateFrame);
|
|
731
|
+
} else {
|
|
732
|
+
render();
|
|
733
|
+
}
|
|
734
|
+
},
|
|
735
|
+
|
|
736
|
+
// ==================== 水平条形图 ====================
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* 绘制水平条形图
|
|
740
|
+
* @param {string} canvasId - Canvas 元素 ID
|
|
741
|
+
* @param {Object} data - 数据对象
|
|
742
|
+
* @param {string[]} data.labels - 标签数组
|
|
743
|
+
* @param {number[]} data.values - 值数组
|
|
744
|
+
* @param {Object} options - 配置选项
|
|
745
|
+
* @param {string[]} [options.colors] - 条形颜色
|
|
746
|
+
* @param {boolean} [options.animate=true] - 是否动画
|
|
747
|
+
* @param {Function} [options.onClick] - 点击回调
|
|
748
|
+
* @param {boolean} [options.showPercent=true] - 显示百分比
|
|
749
|
+
*/
|
|
750
|
+
horizontalBar(canvasId, data, options = {}) {
|
|
751
|
+
const canvas = document.getElementById(canvasId);
|
|
752
|
+
if (!canvas || !data.labels || !data.values) return;
|
|
753
|
+
|
|
754
|
+
const ctx = canvas.getContext('2d');
|
|
755
|
+
const width = canvas.width;
|
|
756
|
+
const height = canvas.height;
|
|
757
|
+
const padding = { top: 20, right: 60, bottom: 20, left: 100 };
|
|
758
|
+
|
|
759
|
+
const {
|
|
760
|
+
colors = ['#e74c3c', '#f39c12', '#3498db', '#9b59b6', '#1abc9c'],
|
|
761
|
+
animate = true,
|
|
762
|
+
onClick = null,
|
|
763
|
+
showPercent = true
|
|
764
|
+
} = options;
|
|
765
|
+
|
|
766
|
+
const chartWidth = width - padding.left - padding.right;
|
|
767
|
+
const chartHeight = height - padding.top - padding.bottom;
|
|
768
|
+
const barCount = data.labels.length;
|
|
769
|
+
const barHeight = chartHeight / barCount * 0.65;
|
|
770
|
+
const barGap = chartHeight / barCount * 0.35;
|
|
771
|
+
const maxValue = Math.max(...data.values, 1);
|
|
772
|
+
const total = data.values.reduce((a, b) => a + b, 0);
|
|
773
|
+
|
|
774
|
+
let animationProgress = animate ? 0 : 1;
|
|
775
|
+
|
|
776
|
+
function render() {
|
|
777
|
+
ctx.clearRect(0, 0, width, height);
|
|
778
|
+
|
|
779
|
+
// 网格线
|
|
780
|
+
ctx.strokeStyle = '#ecf0f1';
|
|
781
|
+
ctx.lineWidth = 1;
|
|
782
|
+
|
|
783
|
+
for (let i = 0; i <= 4; i++) {
|
|
784
|
+
const x = padding.left + (chartWidth / 4) * i;
|
|
785
|
+
ctx.beginPath();
|
|
786
|
+
ctx.moveTo(x, padding.top);
|
|
787
|
+
ctx.lineTo(x, height - padding.bottom);
|
|
788
|
+
ctx.stroke();
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// 条形
|
|
792
|
+
data.values.forEach((value, index) => {
|
|
793
|
+
const y = padding.top + (chartHeight / barCount) * index + barGap / 2;
|
|
794
|
+
const barWidthAnimated = (value / maxValue) * chartWidth * animationProgress;
|
|
795
|
+
const color = colors[index % colors.length];
|
|
796
|
+
|
|
797
|
+
// 背景
|
|
798
|
+
ctx.fillStyle = '#f8f9fa';
|
|
799
|
+
ctx.beginPath();
|
|
800
|
+
this._roundRect(ctx, padding.left, y, chartWidth, barHeight, 3);
|
|
801
|
+
ctx.fill();
|
|
802
|
+
|
|
803
|
+
// 前景
|
|
804
|
+
if (barWidthAnimated > 0) {
|
|
805
|
+
const gradient = ctx.createLinearGradient(padding.left, y, padding.left + barWidthAnimated, y);
|
|
806
|
+
gradient.addColorStop(0, color);
|
|
807
|
+
gradient.addColorStop(1, this._adjustBrightness(color, -15));
|
|
808
|
+
|
|
809
|
+
ctx.fillStyle = gradient;
|
|
810
|
+
ctx.beginPath();
|
|
811
|
+
this._roundRect(ctx, padding.left, y, barWidthAnimated, barHeight, 3);
|
|
812
|
+
ctx.fill();
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// 标签
|
|
816
|
+
ctx.fillStyle = '#34495e';
|
|
817
|
+
ctx.font = '12px -apple-system, sans-serif';
|
|
818
|
+
ctx.textAlign = 'right';
|
|
819
|
+
ctx.textBaseline = 'middle';
|
|
820
|
+
ctx.fillText(data.labels[index], padding.left - 10, y + barHeight / 2);
|
|
821
|
+
|
|
822
|
+
// 数值和百分比
|
|
823
|
+
if (animationProgress >= 1) {
|
|
824
|
+
ctx.fillStyle = '#2c3e50';
|
|
825
|
+
ctx.font = 'bold 11px -apple-system, sans-serif';
|
|
826
|
+
ctx.textAlign = 'left';
|
|
827
|
+
|
|
828
|
+
const textX = padding.left + barWidthAnimated + 8;
|
|
829
|
+
let labelText = value.toString();
|
|
830
|
+
if (showPercent && total > 0) {
|
|
831
|
+
labelText += ` (${Math.round(value / total * 100)}%)`;
|
|
832
|
+
}
|
|
833
|
+
ctx.fillText(labelText, textX, y + barHeight / 2);
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
}.bind(this);
|
|
837
|
+
|
|
838
|
+
// 动画
|
|
839
|
+
if (animate) {
|
|
840
|
+
const startTime = performance.now();
|
|
841
|
+
const duration = 600;
|
|
842
|
+
|
|
843
|
+
function animateFrame(currentTime) {
|
|
844
|
+
animationProgress = Math.min((currentTime - startTime) / duration, 1);
|
|
845
|
+
render();
|
|
846
|
+
|
|
847
|
+
if (animationProgress < 1) {
|
|
848
|
+
requestAnimationFrame(animateFrame);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
requestAnimationFrame(animateFrame);
|
|
853
|
+
} else {
|
|
854
|
+
render();
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// 点击事件
|
|
858
|
+
if (onClick) {
|
|
859
|
+
canvas.addEventListener('click', (e) => {
|
|
860
|
+
const rect = canvas.getBoundingClientRect();
|
|
861
|
+
const mouseY = e.clientY - rect.top;
|
|
862
|
+
|
|
863
|
+
if (mouseY >= padding.top && mouseY <= height - padding.bottom) {
|
|
864
|
+
const index = Math.floor((mouseY - padding.top) / (chartHeight / barCount));
|
|
865
|
+
if (index >= 0 && index < barCount) {
|
|
866
|
+
onClick(index, data.labels[index], data.values[index]);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
canvas.style.cursor = 'pointer';
|
|
872
|
+
}
|
|
873
|
+
},
|
|
874
|
+
|
|
875
|
+
// ==================== 内部工具方法 ====================
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* 绘制圆角矩形路径
|
|
879
|
+
* @param {CanvasRenderingContext2D} ctx - Canvas上下文
|
|
880
|
+
* @param {number} x - X坐标
|
|
881
|
+
* @param {number} y - Y坐标
|
|
882
|
+
* @param {number} w - 宽度
|
|
883
|
+
* @param {number} h - 高度
|
|
884
|
+
* @param {number} r - 圆角半径
|
|
885
|
+
*/
|
|
886
|
+
_roundRect(ctx, x, y, w, h, r) {
|
|
887
|
+
if (w < 2 * r) r = w / 2;
|
|
888
|
+
if (h < 2 * r) r = h / 2;
|
|
889
|
+
ctx.moveTo(x + r, y);
|
|
890
|
+
ctx.arcTo(x + w, y, x + w, y + h, r);
|
|
891
|
+
ctx.arcTo(x + w, y + h, x, y + h, r);
|
|
892
|
+
ctx.arcTo(x, y + h, x, y + r, r);
|
|
893
|
+
ctx.arcTo(x, y, x + w, y, r);
|
|
894
|
+
ctx.closePath();
|
|
895
|
+
},
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* 调整颜色亮度
|
|
899
|
+
* @param {string} color - 十六进制颜色
|
|
900
|
+
* @param {number} amount - 调整量 (-255 到 255)
|
|
901
|
+
* @returns {string}
|
|
902
|
+
*/
|
|
903
|
+
_adjustBrightness(color, amount) {
|
|
904
|
+
const hex = color.replace('#', '');
|
|
905
|
+
const r = Math.max(0, Math.min(255, parseInt(hex.substr(0, 2), 16) + amount));
|
|
906
|
+
const g = Math.max(0, Math.min(255, parseInt(hex.substr(2, 2), 16) + amount));
|
|
907
|
+
const b = Math.max(0, Math.min(255, parseInt(hex.substr(4, 2), 16) + amount));
|
|
908
|
+
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
// 导出全局使用
|
|
913
|
+
window.Charts = Charts;
|