uniapp 圆环图 一个页面两个图表 图表不显示

 项目中  一个页面  tab切换  两张ucharts图表显示隐藏    

    

                                     图 1                                                                                    图2  

点击ucharts1   显示图1     点击ucharts2  显示图2      

ucharts1   显示 图1 的代码:

template:

// 图1 的canvas-id 是 canvasRing  就是官网down下来的源代码 
<view class="qiun-charts">
	<canvas canvas-id="canvasRing" id="canvasRing" class="charts"></canvas>
</view>

// 图2 的canvas-id 是 canvasRings  单词后面加了s  以区分 图1  图2  
<view class="qiun-charts">
	<canvas canvas-id="canvasRings" id="canvasRings" class="charts"></canvas>
</view>
 

js:

// 引用 ucharts 图表	
   import uCharts from '@/components/u-charts/u-charts.js';
   var _self;
   var canvaRing = null;     // 图1 canvaRing
   var canvaRings = null;    // 图2 canvaRings
  // 图1  图2 公用的data  写一个就行
    data(){   
		return{
		    cWidth: '',  
			cHeight: '',
			pixelRatio: 1,
			serverData: '',
		}
	},

    onLoad() {
	    if( 选中ucharts1 ){
			_self = this;
			this.cWidth = uni.upx2px(750);     //  图1 宽度  可修改括号里数值
			this.cHeight = uni.upx2px(800);    //  图1 高度  可修改括号里数值
			// this.getServerData();           //  调接口渲染数据的方法
			this.showRing('canvasRing');       //  加载 图1 canvasRing
  		}else if( 选中ucharts2 ){
			_self = this;
			this.cWidth = uni.upx2px(750);   // 同上
			this.cHeight = uni.upx2px(800);
			// this.getServerData();
			this.showRings('canvasRings')    // 图2 的是 canvasRings
		}
	    },

        methods:{
         // 选 ucharts1 的方法
         checkb:function(){
			_self = this;
			this.cWidth = uni.upx2px(750);
			this.cHeight = uni.upx2px(800);
			// this.getServerData();
			this.showRing('canvasRing');    // 切换tab的时候 要点两下图表才出现 所以加了个定时器
			setTimeout(() =>{
				this.showRing('canvasRing');
			},300)
			},
            
        // 选 ucharts2 的方法   同上
		checkc:function(){
			_self = this;
			this.cWidth = uni.upx2px(750);
			this.cHeight = uni.upx2px(800);
			// this.getServerData();
			this.showRings('canvasRings');
			setTimeout(() =>{
				this.showRings('canvasRings');
			},300)
			},

        // 图1 的数据
			showRing(canvasId) {
				var chartData = {
					series :[{
						"name": "图1",
						"data": 20
					  }, {
						"name": "图1",
						"data": 30
					  }, {
						"name": "图1",
						"data": 20
					  }, {
						"name": "图1",
						"data": 10
					  }, {
						"name": "图1",
						"data": 5
					  }, {
						"name": "图1",
						"data": 5
					  }, {
						"name": "图1",
						"data": 5
					  }]
				};
				canvaRing = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'ring',
					fontSize: 13,
					legend: {   // 这个是 图例的相关配置参数  其他的参数见官网
						show:true,
						fontSize:20,
						lineHeight:30,
						float:'center',
						position:'bottom'
					},
					extra: {
						pie: {
							offsetAngle: -45,
							ringWidth: 40 * _self.pixelRatio,
							labelWidth: 15
						}
					},
					background: '#fff',
					pixelRatio: _self.pixelRatio,
					series: chartData.series,  // 渲染上面的数据
					animation: false, 
					width: _self.cWidth * _self.pixelRatio,
					height: _self.cHeight * _self.pixelRatio,
					disablePieStroke: true,
					dataLabel: true,  
				});
			},



            // 图2 的数据  注:图2的 有s  如:showRings、chartDatas  区别 图1 和图2 
			showRings(canvasId) {
				var chartDatas = {
					series: [{
						"name": "图2",
						"data": 50
					}, {
						"name": "图2",
						"data": 30
					}, {
						"name": "图2",
						"data": 20
					}, {
						"name": "图2",
						"data": 18
					}, {
						"name": "图2",
						"data": 8
					}]
				};
				canvaRings = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'ring',
					fontSize: 11,
					legend: {
						show:true,
						fontSize:20,
						lineHeight:30,
						float:'center',
						position:'bottom'
					},
					extra: {
						pie: {
							offsetAngle: -45,
							ringWidth: 40 * _self.pixelRatio,
							labelWidth: 15
						}
					},
					background: '#fff',
					pixelRatio: _self.pixelRatio,
					series: chartDatas.series,
					animation: false,
					width: _self.cWidth * _self.pixelRatio,
					height: _self.cHeight * _self.pixelRatio,
					disablePieStroke: true,
					dataLabel: true,
				});
			},
        }

再强调一遍!! 图1 是官网源码 没有s     图 2  是为了区分  加了s     不区分会有冲突  某个图表不显示或者都不显示或者出现其他的问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp一个基于Vue.js的跨平台开发框架,可以用于开发iOS、Android、H5等多个平台的应用程序。要实现一个圆环进度条,可以按照以下步骤进行: 1. 在UniApp项目中创建一个组件,可以命名为CircleProgress。 2. 在CircleProgress组件的template中,使用canvas标签来绘制圆环。设置canvas的宽度和高度,并通过样式设置圆环的颜色、宽度等属性。 3. 在CircleProgress组件的script中,定义一个data属性,用于存储进度条的数值。可以设置默认值为0。 4. 在CircleProgress组件的mounted生命周期钩子函数中,获取canvas的上下文对象,并通过上下文对象的方法来绘制圆环。可以使用arc方法来绘制圆弧,再使用stroke方法来描边。 5. 在CircleProgress组件的watch属性中,监听进度条数值的变化,并在变化时重新绘制圆环。 6. 在需要使用圆环进度条的页面中,引入CircleProgress组件,并通过v-model指令来绑定进度条数值。 下面是一个简单的示例代码: ```html <template> <view class="circle-progress"> <canvas canvas-id="progressCanvas" style="width: 200px; height: 200px;"></canvas> </view> </template> <script> export default { data() { return { progress: 0, // 进度条数值 }; }, mounted() { this.drawProgress(); }, watch: { progress() { this.drawProgress(); }, }, methods: { drawProgress() { const ctx = uni.createCanvasContext('progressCanvas', this); const centerX = 100; // 圆心x坐标 const centerY = 100; // 圆心y坐标 const radius = 80; // 圆环半径 const startAngle = -Math.PI / 2; // 起始角度(-90度) const endAngle = startAngle + (this.progress / 100) * (Math.PI * 2); // 结束角度 ctx.clearRect(0, 0, 200, 200); // 清空画布 ctx.setLineWidth(10); // 设置圆环宽度 ctx.setStrokeStyle('#ff0000'); // 设置圆环颜色 ctx.setLineCap('round'); // 设置圆环端点样式为圆形 ctx.beginPath(); ctx.arc(centerX, centerY, radius, startAngle, endAngle, false); ctx.stroke(); ctx.draw(); }, }, }; </script> <style> .circle-progress { display: flex; justify-content: center; align-items: center; } </style> ``` 在上述示例代码中,我们使用canvas标签来绘制圆环,并通过uni.createCanvasContext方法获取canvas的上下文对象。然后,根据进度条数值计算起始角度和结束角度,并使用arc方法绘制圆弧,再使用stroke方法描边。最后,通过调用draw方法来绘制圆环

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值