Files
EmailBill/.doc/chart-grid-lines-issue.md
SunCheng 3402ffaae2
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 3m13s
Docker Build & Deploy / Deploy to Production (push) Successful in 8s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
fix
2026-02-19 11:04:05 +08:00

104 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Doughnut/Pie 图表显示网格线问题修复
author: AI Assistant
date: 2026-02-19
status: final
category: 技术修复
---
# Doughnut/Pie 图表显示网格线问题修复
## 问题描述
在使用 Chart.js 的 Doughnut环形图或 Pie饼图图表中不应该显示笛卡尔坐标系的网格线但在某些情况下会错误地显示出来。
## 问题根源
`useChartTheme.ts` 中的 `baseChartOptions` 包含了 `scales.x``scales.y` 配置(第 82-108 行),这些配置适用于折线图、柱状图等**笛卡尔坐标系图表**,但不适用于 Doughnut/Pie 这类**极坐标图表**。
当使用 `getChartOptions()` 合并配置时,这些默认的 `scales` 配置会被带入到圆形图表中,导致显示网格线。
## 修复方案
### 方案 1在具体组件中显式禁用已应用
在使用 Doughnut/Pie 图表的组件中,调用 `getChartOptions()` 时显式传入 `scales` 配置:
```javascript
const chartOptions = computed(() => {
return getChartOptions({
cutout: '65%',
// 显式禁用笛卡尔坐标系Doughnut 图表不需要)
scales: {
x: { display: false },
y: { display: false }
},
plugins: {
// ...其他插件配置
}
})
})
```
### 方案 2BaseChart 组件自动处理(已优化)
优化 `BaseChart.vue` 组件(第 106-128 行),使其能够自动检测圆形图表并强制禁用坐标轴:
```javascript
const mergedOptions = computed(() => {
const isCircularChart = props.type === 'pie' || props.type === 'doughnut'
const merged = getChartOptions(props.options)
if (isCircularChart) {
if (!props.options?.scales) {
// 用户完全没传 scales直接删除
delete merged.scales
} else {
// 用户传了 scales确保 display 设置为 false
if (merged.scales) {
if (merged.scales.x) merged.scales.x.display = false
if (merged.scales.y) merged.scales.y.display = false
}
}
}
return merged
})
```
## 已修复的文件
1. **Web/src/views/statisticsV2/modules/ExpenseCategoryCard.vue**
-`chartOptions` 中添加了显式的 `scales` 禁用配置(第 321-324 行)
2. **Web/src/components/Charts/BaseChart.vue**
- 优化了圆形图表的 `scales` 处理逻辑(第 106-128 行)
## 已验证的文件(无需修改)
1. **Web/src/components/Budget/BudgetChartAnalysis.vue**
- `monthGaugeOptions``yearGaugeOptions` 已经包含正确的 `scales` 配置
## 预防措施
1. **新增 Doughnut/Pie 图表时**:始终显式设置 `scales: { x: { display: false }, y: { display: false } }`
2. **使用 BaseChart 组件**:依赖其自动处理逻辑(已优化)
3. **代码审查**:检查所有圆形图表配置,确保不包含笛卡尔坐标系配置
## Chart.js 图表类型说明
| 图表类型 | 坐标系 | 是否需要 scales |
|---------|--------|----------------|
| Line | 笛卡尔 | ✓ 需要 x/y |
| Bar | 笛卡尔 | ✓ 需要 x/y |
| Pie | 极坐标 | ✗ 不需要 |
| Doughnut| 极坐标 | ✗ 不需要 |
| Radar | 极坐标 | ✗ 不需要 |
## 相关资源
- Chart.js 官方文档https://www.chartjs.org/docs/latest/
- 项目主题配置:`Web/src/composables/useChartTheme.ts`
- 图表基础组件:`Web/src/components/Charts/BaseChart.vue`