• uniapp使用renderjs实现echarts


    一、(1)插件市场,导入使用renderjs

    // static文件夹中生成一个echarts.js文件
    //使用
    <!-- 列表页 -->
    <template>
    	<view>
    		<!-- 2 start -->
    		<view class="sales-view">
    			<sale-column-chart :chartData="chartDataObj02"></sale-column-chart>
    		</view>
    		<!-- 2 end -->
    	</view>
    </template>
    
    <script>
    //(1),引入组件
    import saleColumnChart from '@/components/echarts/index.vue';
    export default {
    	//(2),挂载
    	components: {
    		saleColumnChart
    	},
    	data(){
    		return{
    			chartDataObj02:{
    				categories:['1','2','3','4','5','6','7','8','9','10','11','12'],
    				series:[
    				  {
    					barWidth: 10,  
    					color: "#4B7ED4",
    					name: "实收",
    					stack: "Ad",
    					type: "bar",
    					yAxisIndex: 1,
    					data:["0.00","0.00","0.00","20724.97","30529.00","43640.88","13428.60","0.00","0.00","0.00" ,"0.00" ,"0.00"],
    					itemStyle:{barBorderRadius:[2,2,0,0]}
    				  },
    				  {
    					barWidth: 10,  
    					color: "#D5E2F7",
    					name: "应收",
    					stack: "Ad",
    					type: "bar",
    					yAxisIndex: 1,
    					data:["0.00","0.00","0.00","20724.97","30529.00","43640.88","13428.60","0.00","0.00","0.00" ,"0.00" ,"0.00"],
    					itemStyle:{barBorderRadius:[2,2,0,0]}
    				  },
    				  {
    					barWidth: 10,  
    					color: "#47D1E3",
    					name: "回款率",
    					stack: "Ad",
    					type: "line",
    					yAxisIndex: 0,
    					data:["0.00","0.00","0.00","0.00","1060.15","485.31","451.82","136.41","0.00","0.00" ,"0.00" ,"0.00"],
    					itemStyle:{barBorderRadius:[2,2,0,0]}
    				  },
    				]
    			},
    			//
    		}
    	},
    	onLoad() {
    		
    	},
    	methods:{
    		getTreeIndexTap(row){
    			console.log('----',row)
    		}
    	},
    }
    </script>
    
    <style lang="scss" scoped>
    	page{
    		background-color: #f6f7fb;
    	}
    	.sales-view{
    		margin: 20rpx;
    		border-radius: 16rpx;
    		background-color: white;
    		padding: 20rpx;
    		.title{
    			font-size: 26rpx;
    		}
    	}
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    一、(2)封装组件

    // An highlighted block
    <!-- uniapp插件市场 renderjs-echarts-demo -->
    <template>
    	<view class="content">
    		<!-- #ifdef APP-PLUS || H5 -->
    		<view v-if="Object.keys(option).length > 0" @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view>
    		<!-- <button @click="changeOption">更新数据</button> -->
    		<!-- #endif -->
    	</view>
    </template>
    
    <script>
    export default {
    	props: {
    		chartData: {
    			type: Object
    		},
    		axisLabelShow: {
    			type: Boolean,
    			default: true
    		},
    		rotate: {
    			type: [String, Number],
    			default: 0
    		}
    	},
    	data() {
    		return {
    			option: {}
    		};
    	},
    	mounted() {
    		this.$nextTick(() => {
    			
    			if (Object.keys(this.chartData).length > 0) {
    				this.option = {
    					tooltip: {
    						show: false,
    						trigger: 'axis',
    						axisPointer: {
    							type: 'shadow'
    						}
    					},
    					legend: {
    						icon: 'circle',
    						textStyle: {
    							//图例文字的样式
    							color: '#BEC1CD', // 单独设置某一个图列的颜色
    							fontSize: 12
    						},
    						x: 'right'
    					},
    					grid: {
    						left: '3%',
    						right: '4%',
    						bottom: '3%',
    						containLabel: true
    					},
    					xAxis: [
    						{
    							axisTick: {
    								//x轴刻度线
    								show: false
    							},
    							type: 'category',
    							splitLine: { show: false },
    							data: this.chartData.categories,
    							axisLine: {
    								show: true,
    								lineStyle: {
    									color: '#F0F0F0',
    									fontSize: 12
    								}
    							},
    							axisLabel: {
    								//x轴文字的配置
    								show: true,
    								rotate: this.rotate,
    								interval: 0, //强制显示
    								textStyle: {
    									color: '#B7BAC6'
    								}
    							}
    						}
    					],
    					yAxis: [
    						{
    							type: 'value',
    							// name: '(元)',
    							position: 'left',
    							alignTicks: true,
    							// nameGap: 18,
    							// nameLocation: 'start',
    							axisTick: {
    								//x轴刻度线
    								show: false
    							},
    							splitLine: {
    								lineStyle: {
    									type: 'border', //虚线
    									color: '#EEEEEE'
    								},
    								show: true //隐藏
    							},
    							axisLine: {
    								show: false,
    								lineStyle: {
    									color: '#BEC1CD',
    									fontSize: 12
    								}
    							},
    							axisLabel: {
    								formatter: this.axisLabelShow ? '{value}%' : '{value}'
    							}
    						},
    						{
    							type: 'value',
    							// name: '(次)',
    							position: 'right',
    							alignTicks: true,
    							axisTick: {
    								//x轴刻度线
    								show: false
    							},
    							axisLine: {
    								show: false,
    								lineStyle: {
    									color: '#BEC1CD'
    								}
    							},
    							splitLine: {
    								lineStyle: {
    									color: '#EEEEEE',
    									type: 'border' //虚线
    								},
    								show: false //隐藏
    							},
    							axisLabel: {
    								show: this.axisLabelShow,
    								formatter: this.axisLabelShow ? '{value}万' : '{value}'
    							}
    						}
    					],
    					series: this.chartData.series
    				};
    			}
    			 // console.log('1---',this.option)
    		});
    		
    	},
    	methods: {
    		changeOption() {
    			const data = this.option.series[0].data;
    			// 随机更新示例数据
    			data.forEach((item, index) => {
    				data.splice(index, 1, Math.random() * 40);
    			});
    		},
    		onViewClick(options) {
    			console.log(options);
    		}
    	}
    };
    </script>
    
    <script module="echarts" lang="renderjs">
    let myChart
    export default {
    	mounted() {
    		if (typeof window.echarts === 'function') {
    				this.initEcharts()
    		} else {
    			// 动态引入较大类库避免影响页面展示
    			const script = document.createElement('script')
    			// view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
    			script.src = 'static/echarts.js'
    			script.onload = this.initEcharts.bind(this)
    			document.head.appendChild(script)
    		}
    	},
    	methods: {
    		initEcharts() {
    			myChart = echarts.init(document.getElementById('echarts'))
    				myChart.setOption(this.option)
    			// 观测更新的数据在 view 层可以直接访问到
    		},
    		updateEcharts(newValue, oldValue, ownerInstance, instance) {
    			 if(myChart){
    				 myChart.setOption(newValue)
    			 }
    			// 监听 service 层数据变更
    		},
    		onClick(event, ownerInstance) {
    			// 调用 service 层的方法
    			ownerInstance.callMethod('onViewClick', {
    				option: this.option
    			})
    		}
    	}
    }
    </script>
    
    <style>
    .content {
    	display: flex;
    	flex-direction: column;
    	align-items: center;
    	justify-content: center;
    }
    
    .echarts {
    	width: 100%;
    	height: 300px;
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
  • 相关阅读:
    在vue3的开发环境中为什么使用vite而不是用webpack
    Linux故障排查思路
    Java 多线程:并发编程的三大特性
    1 快速了解Paimon数据湖核心原理及架构
    Lecture 12 Memory Management(内存管理)
    字节技术面都过了,薪资都谈好了20K*13结果还是被刷了,问HR,原因是。。
    GBase 8s数据库级别权限
    TensorFlow入门(十一、图的基本操作)
    数据结构(C语言版)01
    Kafka开篇
  • 原文地址:https://blog.csdn.net/weixin_46899191/article/details/126348405