记录uCharts---uniapp中遇到的一些问题----仪表盘

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,
				});
			},

这是最终的效果
在这里插入图片描述

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

供你活着

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值