uniapp微信小程序+echarts简单图表以及与后端交互

效果图:

 

需要的两个主要文件就是  echarts.min.js  和  echarts.vue 

1、echarts.min.js 可以去官网定制 链接   ECharts 在线构建   或者直接去GitHub - Tawesome666/echarts: echarts 下载

(注意:我这个只有柱状图和折线图)

2、echarts.vue  可以使用 Visual Studio Code  打开你的文件夹使用命令  npm install echarts mpvue-echarts   执行完了后在node-module  把mpvue-echarts 中 src 文件夹复制到 components 文件夹下 

3、上代码  这个是没与后端交互的 第四点是与后端交互的代码  (不与后端交互的可以直接扣下面代码就有效果了)

<template>
	<view>
		<view class="box1">
			<my-echarts id="main" ref="mapChart" :echarts="echarts" :onInit="init" />
		</view>
	</view>
</template>

<script>
	import * as echarts from '../../common/echarts.min.js'; 
	import myEcharts from '@/components/mpvue-echarts/src/echarts.vue';
	export default {
		data() {
			return {
				
			}
		},
		components:{
			myEcharts
		},
		methods: {
			init(canvas, width, height) {
				let chart = null
				chart = echarts.init(canvas, null, {
					width: width,
					height: height
				});
				let option = {
					xAxis: {
						type: 'category',
						data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
					},
					yAxis: {
						type: 'value'
					},
					series: [{
						data: [150, 230, 224, 218, 135, 147, 260],
						type: 'line'
					}]
				};
				chart.setOption(option);
				return chart; // 返回 chart 后可以自动绑定触摸操作
			},
        }
	}
</script>


<style lang="scss">
	.box1 {
		width: 94%;
		height: 500rpx;
		background-color: #fff;
		margin: 10px auto;
	}
</style>

 4、与后端交互的代码   主要实现就是在这个 init函数 内执行请求

<template>
	<view>
		<view class="box1">
			<my-echarts id="main" ref="mapChart" :echarts="echarts" :onInit="init" />
		</view>
	</view>
</template>


<script>
    import * as echarts from '../../common/echarts.min.js';
	import myEcharts from '@/components/mpvue-echarts/src/echarts.vue';
    export default {
		data() {
			return {
				Xdata:[],
				Ydata:[]
			}
		},
		components:{
			myEcharts
		},
		methods: {
			init(canvas, width, height) {
				const echarr = []
				const waringnum = []   
				this.$myHttp({ 					//与后端请求
					url: WarnLineGraph,
					method: "GET",
				}).then(res => {
					let arr = res.data.data
					arr.forEach(item => {
						echarr.push(item.warningTime)
						waringnum.push(item.warningNum)
					})
					this.Xdata = echarr
					this.Ydata = waringnum

					let chart = null
					chart = echarts.init(canvas, null, {
						width: width,
						height: height
					});
					let option = {
						xAxis: {
							type: 'category',
							data: this.Xdata,    //后端的数据
							axisTick: {
								//y轴刻度线
								show: false,
							},
							// "axisLabel": {
							// 	"interval": 0,
							// 	"rotate": 30
							// },
						},
						yAxis: [{
							type: 'value',
							axisTick: {
								show: false, //去除y轴刻度线
							},
						}, ],
						series: [{
							data:this.Ydata,   //后端的数据
							type: 'line',
							symbolSize: 6, // 圆点大小
							itemStyle: {
								// 线条样式
								normal: {
									color: "#0080ff",
									lineStyle: {
										color: "#0080ff",
									},
								},
							},
						}],

					};
					chart.setOption(option);
					return chart; // 返回 chart 后可以自动绑定触摸操作
				})
			},
		}
	}
    
</script>


<style lang="scss">
	.box1 {
		width: 94%;
		height: 500rpx;
		background-color: #fff;
		margin: 10px auto;
	}
</style>

5、报错处理  

 遇到 this.echarts.setCanvasCreator is not a function 报错

遇到 t.addEventListener is not a function 报错

去参照这位博主 它上面讲的很详细 uni-app微信小程序使用ECharts_柚大大的博客-CSDN博客_uni-app 使用echarts

 写了蛮长的点个赞走呗 !亲爱的!!!

 

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值