const drawCanvas = (canvasId, _this, dataObj) => {
const data = JSON.parse(JSON.stringify(dataObj))
demoList[0].value = data.levelOne ?? 0
demoList[1].value = data.levelTwo ?? 0
demoList[2].value = data.levelThree ?? 0
uni.createSelectorQuery().select(canvasId).fields({
node: true,
size: true
}).exec(res => {
const {
devicePixelRatio,
screenWidth
} = uni.getSystemInfoSync()
const rpx = devicePixelRatio
const canvas = res[0].node
let ctx = canvas.getContext('2d')
canvas.width = res[0].width * rpx
canvas.height = res[0].height * rpx
const x = res[0].width / 2 * rpx;
const y = res[0].height / 2 * rpx;
const lineWidth = 20 * rpx;
const radius = (res[0].height / 2) * rpx - (lineWidth / 2)
const sumResult = canvasId === '#chart' ? data.errorTotal : data.alertTotal;
let startAngle = 0;
demoList.map(item => {
ctx.beginPath();
console.log('sum',sumResult)
let angle = (item.value / sumResult) * 2 * Math.PI
ctx.arc(x, y, radius, startAngle, startAngle + angle);
ctx.lineWidth = lineWidth;
ctx.strokeStyle = item.color;
ctx.stroke();
startAngle += angle
})
ctx.fillStyle = "#1D2129"
ctx.font = `${30 * rpx}px sans-serif`
ctx.textAlign = 'center'
ctx.fillText(sumResult, canvas.width / 2, y)
ctx.fillStyle = "#86909C"
ctx.font = `${12 * rpx}px sans-serif`
ctx.textAlign = 'center'
ctx.fillText('故障总计(台)', canvas.width / 2, y + (20 * rpx))
uni.canvasToTempFilePath({
canvas: canvas,
success: function(res) {
if (canvasId === '#chart') {
canvasSrc1.value = res.tempFilePath;
} else {
canvasSrc2.value = res.tempFilePath;
}
},
fail: function(res) {
console.log(222, res)
}
})
})
}
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
// 页面部分 // 是这么调用的,目前数据部分先写死了 const getCard2Date = async (id) => { const { data } = await getDeviceOverview2(id) card2Data = Object.assign(card2Data, { errorTotal: 80, levelOne: 10, levelTwo: 40, levelThree: 30 }) drawCanvas('#chart', this, card2Data) } const getCard3Date = async (id) => { const { data } = await getDeviceOverview3(id) card3Data = Object.assign(card3Data, { alertTotal: 100, levelOne: 24, levelThree: 0, levelTwo: 76 }) drawCanvas('#chart2', this, card3Data) }
感觉两个值会互相影响结果不知道为什么,代码上感觉已经独立开了呀。。