fix: 改用原生 canvas 绘制收益曲线,移除 qiun-data-charts 依赖

- 微信小程序对 qiun-data-charts 兼容性差
- 原生 canvas 更轻量、更可靠
- 实现平滑曲线 + 渐变填充 + 网格线
- Y轴自适应数据范围
This commit is contained in:
claw_bot 2026-03-15 07:13:19 +00:00
parent 632b5b6f6d
commit 97149ca59e

View File

@ -81,13 +81,11 @@
<!-- 收益曲线 --> <!-- 收益曲线 -->
<view v-else class="nav-chart-wrapper"> <view v-else class="nav-chart-wrapper">
<qiun-data-charts <canvas
type="line" canvas-id="navChart"
:opts="chartOpts" id="navChart"
:chartData="chartData" class="nav-canvas"
:canvas2d="true" ></canvas>
canvasId="navChartCanvas"
/>
</view> </view>
<!-- 统计指标 --> <!-- 统计指标 -->
@ -434,7 +432,7 @@
</template> </template>
<script setup> <script setup>
import { ref, onMounted, getCurrentInstance, computed } from 'vue'; import { ref, onMounted, getCurrentInstance, nextTick } from 'vue';
import { api } from '../../utils/api'; import { api } from '../../utils/api';
// u-toast // u-toast
@ -483,51 +481,95 @@ const navPeriod = ref('30d');
const navHistory = ref([]); const navHistory = ref([]);
const navStatistics = ref(null); const navStatistics = ref(null);
// // 线
const chartData = computed(() => { const drawNavChart = () => {
if (!navHistory.value || navHistory.value.length === 0) { const data = navHistory.value;
return { categories: [], series: [] }; if (!data || data.length === 0) return;
nextTick(() => {
const ctx = uni.createCanvasContext('navChart');
// (rpx -> px)
const width = 320;
const height = 200;
const padding = { top: 20, right: 20, bottom: 30, left: 45 };
const chartWidth = width - padding.left - padding.right;
const chartHeight = height - padding.top - padding.bottom;
//
const values = data.map(item => item.nav);
const minVal = Math.min(...values) * 0.98;
const maxVal = Math.max(...values) * 1.02;
const range = maxVal - minVal || 1;
//
ctx.clearRect(0, 0, width, height);
// 线
ctx.setStrokeStyle('#E5E7EB');
ctx.setLineWidth(0.5);
for (let i = 0; i <= 4; i++) {
const y = padding.top + (chartHeight / 4) * i;
ctx.beginPath();
ctx.moveTo(padding.left, y);
ctx.lineTo(width - padding.right, y);
ctx.stroke();
} }
return { // Y
categories: navHistory.value.map(item => item.date.split('-').slice(1).join('/')), ctx.setFillStyle('#9CA3AF');
series: [{ ctx.setFontSize(10);
name: '净值', ctx.setTextAlign('right');
data: navHistory.value.map(item => item.nav) for (let i = 0; i <= 4; i++) {
}] const y = padding.top + (chartHeight / 4) * i;
}; const val = maxVal - (range / 4) * i;
}); ctx.fillText(val.toFixed(2), padding.left - 5, y + 3);
}
// //
const chartOpts = ref({ const points = data.map((item, index) => ({
color: ['#064E3B'], x: padding.left + (chartWidth / (data.length - 1 || 1)) * index,
padding: [15, 15, 0, 15], y: padding.top + chartHeight - ((item.nav - minVal) / range) * chartHeight
enableScroll: false, }));
legend: {
show: false //
}, ctx.beginPath();
xAxis: { points.forEach((p, i) => {
disableGrid: true, if (i === 0) ctx.moveTo(p.x, p.y);
axisLine: false, else ctx.lineTo(p.x, p.y);
fontSize: 10, });
fontColor: '#6B7280' ctx.lineTo(points[points.length - 1].x, padding.top + chartHeight);
}, ctx.lineTo(points[0].x, padding.top + chartHeight);
yAxis: { ctx.closePath();
data: [{ min: 0 }], ctx.setFillStyle('rgba(6, 78, 59, 0.15)');
gridColor: '#E5E7EB', ctx.fill();
gridType: 'dash',
dashLength: 4, // 线
fontSize: 10, ctx.beginPath();
fontColor: '#6B7280' ctx.setStrokeStyle('#064E3B');
}, ctx.setLineWidth(2);
extra: { points.forEach((p, i) => {
line: { if (i === 0) ctx.moveTo(p.x, p.y);
type: 'curve', else ctx.lineTo(p.x, p.y);
width: 2, });
activeType: 'hollow' ctx.stroke();
// X
ctx.setFillStyle('#9CA3AF');
ctx.setFontSize(10);
ctx.setTextAlign('center');
const labelCount = Math.min(5, data.length);
const step = Math.floor(data.length / labelCount) || 1;
for (let i = 0; i < labelCount; i++) {
const idx = Math.min(i * step, data.length - 1);
const x = padding.left + (chartWidth / (data.length - 1 || 1)) * idx;
const dateStr = data[idx].date.split('-').slice(1).join('/');
ctx.fillText(dateStr, x, height - 8);
} }
}
}); ctx.draw();
});
};
// //
const showTransactionForm = ref(false); const showTransactionForm = ref(false);
@ -697,6 +739,11 @@ const fetchNavHistory = async () => {
if (response.code === 200 && response.data) { if (response.code === 200 && response.data) {
navHistory.value = response.data.navHistory || []; navHistory.value = response.data.navHistory || [];
navStatistics.value = response.data.statistics || null; navStatistics.value = response.data.statistics || null;
//
if (navHistory.value.length > 0) {
drawNavChart();
}
} }
} catch (error) { } catch (error) {
console.error('获取净值历史失败:', error); console.error('获取净值历史失败:', error);
@ -1291,6 +1338,14 @@ const deletePortfolio = async () => {
position: relative; position: relative;
width: 100%; width: 100%;
height: 400rpx; height: 400rpx;
display: flex;
justify-content: center;
align-items: center;
}
.nav-canvas {
width: 640rpx;
height: 400rpx;
} }
.period-tabs { .period-tabs {