【uniapp】微信小程序使用echarts图表记录

1、插件引入

在Dcloud插件市场下载echarts插件:插件地址

或去相关代码库下载js:gitee地址

将static文件夹下中的echarts.min.js和ecStat.min.js复制到自己项目的static文件夹内或到echarts官方定制自己需要的图表类型下载js文件并放入相关目录。echarts定制地址

将如下相关目录文件复制到自己目录文件:

最后目录如下:

2、页面中使用:

view中:

bash 复制代码
		<LEchart style="position: relative;" ref="chartRef" @finished="init"></LEchart>
		<view class="text">设备总数
						<view style="font-size: 80rpx;font-weight: 600;line-height: 90rpx;">
							80
						</view>
		</view>
		<view style="width: 100%; height: 360rpx;position: relative;">
				<LEchart ref="chartLine" @finished="initLine"></LEchart>
			</view>

js:

bash 复制代码
import LEchart from "@/components/l-echart/l-echart.vue"
import * as echarts from "@/static/echarts.min.js"
export default {
		components: {
			LEchart
		},
		data() {
			return {
			option: {
					color: ['#71b544', "#ff6315", "#f7e920", "#d9d9d9", "#9dff86"],
					tooltip: {
						trigger: 'item',
						show: true
					},
					legend: {
						show: false,
						type: "scroll",
						top: "bottom",
						bottom: 10,
						itemWidth: 15,
						itemHeight: 15,
						icon: "circle", // 图例图形
						itemGap: 20,
					},
					dataset: {
						source: [{
								name: '在线-运行',
								value: 65
							},
							{
								name: '在线-故障',
								value: 5
							},
							{
								name: '离线-正常',
								value: 7
							},
							{
								name: '离线-故障',
								value: 3
							}
						]
					},
					series: [{
						type: 'pie',
						radius: ['70%', '95%'],
						center: ['50%', '48%'],
						avoidLabelOverlap: false,
						clockwise: false, // 设置为 false 表示逆时针
						label: {
							show: false,
							position: 'outside',
							formatter: '{d}%',
						},
					}]
				},
				lineOption:{
					legend: {
					        show: false,
					    },
					    tooltip: {
					        show: true,
					        trigger: 'axis',
					        confine: true,
					        axisPointer: {
					            type: 'line',
					            snap: true
					        },
					        textStyle: {
					            textShadowColor: "transparent",
					            textShadowBlur: 5,
					        }
					    },
					    grid: {
					        left: '3%',
					        right: '5%',
					        top: '20%',
					        bottom: '5%',
					        containLabel: true
					    },
					    xAxis: {
					        type: 'category',
					        boundaryGap: false,
					        axisTick: {
					            show: true,
					            inside: true
					        },
					        axisLine: {
					            show: true,
					            lineStyle: {
					                color: '#707070'
					            }
					        },
					        axisLabel: {
					            textStyle: {
					                color: "#A8ADCF"
					            }
					        }
					    },
					    yAxis: {
					        type: 'value',
					        name: "",
					        nameTextStyle: {
					            color: "#A8ADCF"
					        },
					        axisTick: {
					            show: false
					        },
					        axisLine: {
					            show: true,
					            lineStyle: {
					                color: '#707070'
					            }
					        },
					        axisLabel: {
					            textStyle: {
					                color: "#A8ADCF"
					            }
					        },
					        splitLine: {
					            show: true,
					            lineStyle: {
					                type: 'dashed',
					                color: 'rgba(112, 112, 112, 0.2)'
					            }
					        }
					    },
					    dataset: {
					        source: [
								["8:00",21],
								["9:00",21.6],
								["10:00",22],
								["11:00",26],
								["12:00",28],
								["13:00",28],
								["14:00",27],
								["15:00",25],
								["16:00",23],
								["17:00",23],
							],
					    },
					    series: [
					        {
					            type: "line",
					            smooth: true,
					            symbol: 'none',
					            lineStyle: {
					                color: "#6ffd90",
					                width: 1.5,
					            },
					            areaStyle: {
					                show: true,
					                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
					                    {
					                        offset: 0,
					                        color: 'rgba(30, 236, 157, 0.1)'
					                    },
					                    {
					                        offset: 0.5,
					                        color: 'rgba(96, 255, 154, 0.1)'
					                    },
					                    {
					                        offset: 1,
					                        color: 'rgba(211, 255, 235, 0.1)'
					                    }
					                ])
					            },
					        }
					    ],
					    color: ['#83bff6']
				}
			
				
			}
		},
		methods: {
			async init() {
				const chart = await this.$refs.chartRef.init(echarts);
				chart.setOption(this.option)
				// console.log(11);
			},
			async initLine() {
				const chart = await this.$refs.chartLine.init(echarts);
				chart.setOption(this.lineOption)
				// console.log(21);
			}
	}

}

css:

bash 复制代码
	.chart_content_item {
		width: 45%;
		height: 260rpx;
		position: relative;

		.text {
			position: absolute;
			top: 20%;
			width: 100%;
			text-align: center;
		}
	}

效果图:

参考文章:uniapp引入echarts图表(兼容H5端和微信小程序)

相关推荐
bug总结17 小时前
uniapp 微信小程序分包规范
微信小程序·小程序·uni-app
天府云创18 小时前
uni-app X 蒸汽模式发布!性能比肩原生~
前端框架·uni-app·移动开发·原生应用·蒸汽模式·app研发·多端适配
堕落年代21 小时前
uni-app x 蒸汽模式:实时流式语音识别(ASR)三大疑难杂症全记录
人工智能·uni-app·语音识别
凉红茶1 天前
用Cursor开发微信小程序的第一天
微信小程序·小程序·ai编程
小马过河R1 天前
微信小程序自定义登录态维护:从入门到生产级落地
后端·微信小程序·小程序·架构·登录态
RisunJan1 天前
uni-app 高频面试题与解答
uni-app
andrsted1 天前
用练题簿小程序 - 让错题不再白错
学习·微信小程序·小程序·学习方法
梦曦i1 天前
@meng-xi/create-uni-app vs unibest:轻量脚手架与重型框架的定位之争
前端·uni-app
wxwx_bscxy3222 天前
基于Python新冠疫情可视化分析系统的设计与实现
java·数据库·spring boot·python·微信小程序·sqlite
小小尚@2 天前
WebGIS/ECharts 地图开发|MapLand 在线行政区划边界一键下载 GeoJSON 工具
前端·javascript·echarts