fix: 微信小程序曲线图不显示问题修复。

This commit is contained in:
tianyongbao
2026-01-18 23:42:16 +08:00
parent 314f060359
commit 2b60a33440

View File

@@ -330,27 +330,20 @@
</view> </view>
<canvas <canvas
class="charts" class="charts"
type="2d"
:id=" :id="
'GdHQcFQYzLhfeEvZbEJnhDxJzBlbCxhE' + 'GdHQcFQYzLhfeEvZbEJnhDxJzBlbCxhE' +
String(doDev) + String(doDev) +
String(prDev) + String(prDev) +
String(dayDev) String(dayDev)
" "
:canvas-id=" @touchend="touchEnd"
'GdHQcFQYzLhfeEvZbEJnhDxJzBlbCxhE' + @touchstart="touchCandle"
String(doDev) + @touchmove="moveCandle"
String(prDev) +
String(dayDev)
"
@TouchEnd="touchEnd"
@TouchStart="touchCandle"
@TouchMove="moveCandle"
:style="{ :style="{
width: chartConf.cWidth, width: chartConf.cWidth + 'px',
height: chartConf.cHeight + 'px', height: chartConf.cHeight + 'px',
}" }"
:ontouch="true"
:disableScroll="false"
></canvas> ></canvas>
<!-- <nut-empty description="暂无数据" v-show="chartsDatas.r.length == 0"> <!-- <nut-empty description="暂无数据" v-show="chartsDatas.r.length == 0">
<template #image> <template #image>
@@ -1449,83 +1442,109 @@ function createCharts(x, series, ids) {
console.log('✅ H5 图表创建完成'); console.log('✅ H5 图表创建完成');
}, 300); }, 300);
} else { } else {
// 小程序环境 // 小程序环境 - 使用 canvas 2d 模式
console.log('📱 小程序环境,使用 canvasId 方式'); console.log('📱 小程序环境,使用 canvas 2d 获取 context');
uChartsOp.value[ids] = new uCharts({ // 通过 createSelectorQuery 获取 canvas 2d 节点
type: "line", const query = Taro.createSelectorQuery();
canvasId: ids, query.select('#' + ids)
canvas2d: false, .fields({ node: true, size: true })
width: cWidth * pixelRatio, .exec((res) => {
height: cHeight * pixelRatio, console.log('📝 canvas 节点查询结果:', res);
categories: x,
series: series, if (!res || !res[0] || !res[0].node) {
pixelRatio: pixelRatio, console.error('❌ 无法获取 canvas 节点');
dataPointShape: false, return;
animation: true, }
background: "#FFFFFF",
color: ["#17B4B2", "#5470c6", "#fac858"], const canvas = res[0].node;
padding: [15, 30, 35, 45], const ctx = canvas.getContext('2d');
enableScroll: false,
boundaryGap: "justify", if (!ctx) {
update: true, console.error('❌ 无法获取 canvas context');
legend: { return;
fontColor: "#222222", }
itemGap: 10,
position: "top", // 设置 canvas 实际尺寸
}, canvas.width = cWidth * pixelRatio;
dataLabel: false, canvas.height = cHeight * pixelRatio;
xAxis: {
disableGrid: true, console.log('✅ 获取 canvas 2d context 成功');
axisLine: true,
type: "grid", uChartsOp.value[ids] = new uCharts({
gridType: "dash", type: "line",
itemCount: 0, context: ctx,
scrollShow: false, width: cWidth * pixelRatio,
scrollAlign: "left", height: cHeight * pixelRatio,
labelCount: 7, categories: x,
rotateLabel: true, series: series,
rotateAngle: 25, pixelRatio: pixelRatio,
}, dataPointShape: false,
yAxis: { animation: true,
disableGrid: false, background: "#FFFFFF",
gridType: "dash", color: ["#17B4B2", "#5470c6", "#fac858"],
tofix: 2, padding: [15, 30, 35, 45],
}, enableScroll: false,
extra: { boundaryGap: "justify",
tooltip: { update: true,
showBox: true, legend: {
showArrow: true, fontColor: "#222222",
showCategory: true, itemGap: 10,
borderWidth: 0, position: "top",
borderRadius: 0, },
borderColor: "#000000", dataLabel: false,
borderOpacity: 0.7, xAxis: {
bgColor: "#000000", disableGrid: true,
bgOpacity: 0.7, axisLine: true,
gridType: "solid", type: "grid",
dashLength: 4, gridType: "dash",
gridColor: "#CCCCCC", itemCount: 0,
boxPadding: 3, scrollShow: false,
fontSize: 13, scrollAlign: "left",
lineHeight: 20, labelCount: 7,
fontColor: "#FFFFFF", rotateLabel: true,
legendShow: true, rotateAngle: 25,
legendShape: "auto", },
splitLine: true, yAxis: {
labelBgColor: "#FFFFFF", disableGrid: false,
labelBgOpacity: 0.7, gridType: "dash",
labelFontColor: "#666666", tofix: 2,
}, },
line: { extra: {
type: "curve", tooltip: {
width: 1, showBox: true,
activeType: "hollow", showArrow: true,
}, showCategory: true,
}, borderWidth: 0,
}); borderRadius: 0,
borderColor: "#000000",
console.log('✅ 小程序图表创建完成'); borderOpacity: 0.7,
bgColor: "#000000",
bgOpacity: 0.7,
gridType: "solid",
dashLength: 4,
gridColor: "#CCCCCC",
boxPadding: 3,
fontSize: 13,
lineHeight: 20,
fontColor: "#FFFFFF",
legendShow: true,
legendShape: "auto",
splitLine: true,
labelBgColor: "#FFFFFF",
labelBgOpacity: 0.7,
labelFontColor: "#666666",
},
line: {
type: "curve",
width: 1,
activeType: "hollow",
},
},
});
console.log('✅ 小程序图表创建完成');
});
} }
}); });
} }