在uniapp
中画仪表盘时候报错:[uCharts] 未获取到context!注意:v2.0版本后,需要自行获取canvas的绘图上下文并传入opts.context!
主要是组件没有获取到uniapp
上下文内容而导致!!!
解决问题关键代码:uni.createCanvasContext(canvasId, _self)
var _self;
var canvaGauge = null;
// 画布大小
this.cWidth = uni.upx2px(325);
this.cHeight = uni.upx2px(400);
// 在钩子函数 onLoad 中给 _self 变量把自己赋值 _self = this;
showGauge(canvasId, chartData) {
canvaGauge = new uCharts({
context: uni.createCanvasContext(canvasId, _self),
type: 'gauge',
fontSize: 11,
legend: false,
title: {
name: chartData.series[0].data,
color: '#00ff00',
fontSize: 25 * _self.pixelRatio,
offsetY: 50 * _self.pixelRatio, //新增参数,自定义调整Y轴文案距离
},
subtitle: {
name: chartData.series[0].name,
color: '#c5e8ff',
fontSize: 15 * _self.pixelRatio,
offsetY: -50 * _self.pixelRatio, //新增参数,自定义调整Y轴文案距离
},
extra: {
gauge: {
type: 'default',
width: _self.gaugeWidth * _self.pixelRatio, //仪表盘背景的宽度
startAngle: 0.75,
endAngle: 0.25,
startNumber: 0,
endNumber: 3,
splitLine: {
fixRadius: 0,
splitNumber: 6,
width: _self.gaugeWidth * _self.pixelRatio, //仪表盘背景的宽度
color: '#FFFFFF',
childNumber: 5,
childWidth: _self.gaugeWidth * 0.4 * _self.pixelRatio, //仪表盘背景的宽度
},
labelOffset: 7,
pointer: {
width: _self.gaugeWidth * 0.8 * _self.pixelRatio, //指针宽度
color: 'auto'
},
// 调整表盘显示
formatter: (val, index, opts) => {
let canvasid = opts.context.canvasId;
if ("canvasGaugeB" === canvasid) {
opts.title.name = _self.wvalue.toFixed(3);
} else if ("canvasGauge" === canvasid) {
opts.title.name = _self.pvalue.toFixed(3);
}
if ("canvasGaugeC" === canvasid) {
opts.title.name = _self.dvalue.toFixed(3);
} else if ("canvasGaugeD" === canvasid) {
opts.title.name = _self.xvalue.toFixed(3);
}
return val.toFixed(2);
}
}
},
background: '#FFFFFF',
pixelRatio: _self.pixelRatio,
categories: chartData.categories,
series: chartData.series,
animation: true,
width: _self.cWidth * _self.pixelRatio,
height: _self.cHeight * _self.pixelRatio,
dataLabel: true,
});
},
这是最终的效果