vue项目使用echarts总结

一   安装echarts 

npm install echarts --save 

二   全局引入echarts,在入口文件main.js

import echarts from "echarts";
Vue.prototype.$echarts = echarts;

这样就可以在项目中使用啦

项目中使用 (HTML部分)

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

JS部分

this.$axios.get('接口地址').then(res=>{
    let data = res.data.data  // 获取后台返回的数据
    let myChart = this.$echarts.init(document.getElementById('echarts'))  // 初始化
    let xData = []     // 对数据处理
    let yData = []
    for(var key in data){
        xData.push(key)   // 横坐标
        yData.push(data[key].completeCount / data[key].count))   // 纵坐标
    }
    myChart.setOption({
         grid: {      //  直角坐标系
		    show: true,
            borderColor: '#eee',  //网格边线
		    containLabel: true,
		    left: "10%", //grid 组件离容器左侧的距离。
		    right: "30",
		    bottom: "20%",
		    tooltip: {     //显示提示框组件
			    show: true
		    }    
         },
         legend: {   // 图例
			show: true,
			left: '50',
			top: '20',
			width: "12",
			height: "12",
			data: ['当前进度']
		   },
         xAxis: {
			splitLine: {   // 坐标系中的分割线
				show: true, 
				lineStyle: {
				    color: ['#eee']
				}
			},
			axisLabel:{
				interval: 0,  // 设置为0 显示全部数据(默认数据过多自动隐藏)
				// rotate: 45  // 旋转角度
			},
			axisTick: {
				alignWithLabel: true  // 刻度线与标签对齐
			},
			data: xData  // 横坐标数组
		    },
	     yAxis: {  // 纵坐标
			splitLine: {
				lineStyle: {
					color: ['#eee'] // 纵坐标分割线颜色
				}
			},
			axisLabel: {
				show: true,
				interval: 'auto',
				formatter: function(value,index){
					return value * 100 + '%'
				}
			},
	    },
        series: [{
			itemStyle: {
			    normal: {  // 设置渐变色
					color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [{
							offset: 0,
							color: this.bg.start // 0% 处的颜色
				},
				{
					offset: 1,
					color: this.bg.end // 100% 处的颜色
				}], false)
			    }
			},
			name: '当前进度', // 悬浮框提示文字
			type: 'bar',
			// barGap: .5, //柱子之间间距
			// label:{  // 柱条上是否显示文字等设置
				// 	show: true,
			    // 	distance: 5,
				// 	position: 'top',
				// 	rotate: 45,
				// },
			data: yData
		}],
	   window.addEventListener('resize', () => {   // 自适应宽度
			myChart.resize()
		})

    })						
						            
})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值