在vue中使用echarts

在Vue中使用ECharts可以按照以下步骤进行:

1. 安装ECharts:

在Vue项目的根目录下,执行以下命令安装ECharts依赖:

npm install echarts --save

2.引入echart

import * as echarts from 'echarts'

引入echart有全部引入和按需引入,这里我使用的是全部引入,且在自己需要的页面下,而不是main.js

按需引入:https://echarts.apache.org/handbook/zh/basics/import

3.页面上定义一个有宽高容器(如果没有宽高,图表不会显示)

<div id="map" style="width: 100%;height:400px;"></div>

4.在js里初始化(一定要先获取后台数据,再渲染echarts图表)

methods: {
	//1获取数据列表
	getList() {
		userBPRecordList(this.listQuery, this.pageIndex, this.pageSize).then(res => {
			this.total = res.TotalCount
			this.list = res.DataList
			console.log('打印列表', res)
		
			if (res.TotalCount == 0) { //没有数据时
				//在data里定义好的空数组
				this.xdata = []  
				this.y1data = []
				this.y2data = []
				this.getMap() //渲染图表
			} else {
				res.DataList.forEach(element => {
					this.xdata.push(element.Date + ' ' + element.Time) //x轴时间
					this.y1data.push(element.SBP) //SBP收缩压
					this.y2data.push(element.DBP) //DBP舒张压
				});
				// console.log('打印x轴时间', this.xdata)
				// console.log('打印收缩压', this.y1data)
				// console.log('打印舒张压', this.y2data)
				this.getMap() //渲染图表
			}
		})
	},
	
	//2初始化echarts
	getMap() {
		var myChart = echarts.init(document.getElementById('map'))
		let option = {
			title: {
				text: '血压趋势图',
				x: 'center',
				y: 'top',
				textAlign: 'left'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				data: ['收缩压', '舒张压'],
					bottom: 0,
					padding: [30, 30, 0, 0],
				},
				grid: {
					top: '15%',
					left: '6%',
					right: '6%',
					bottom: '10%',
					// containLabel: true
				},
				xAxis: {
					type: 'category',
					boundaryGap: false,
					data: this.xdata
				},
				yAxis: {
					// min: 0, //取0为最小刻度
					// max: 200, //取100为最大刻度
				},
				series: [{
					name: '收缩压',
					type: 'line',
					data: this.y1data
				},
				{
					name: '舒张压',
					type: 'line',
					data: this.y2data
				}]
		}
		myChart.setOption(option);
	},
}

5.展示效果:

在这里插入图片描述

以上步骤是一个基本的示例,可以根据自己的需求进行配置和数据的设置。
ECharts支持的图表类型和配置项非常丰富,可以参考ECharts的官方文档来了解更多用法和配置选项。
ECharts官网地址:https://echarts.apache.org/zh/index.html

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值