vue中使用echarts

本文详细介绍在Vue项目中如何使用ECharts图表库,包括安装依赖、全局引入及按需引入的方法,通过实例演示如何创建并自定义图表样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、安装echarts依赖

安装echarts依赖

cnpm install echarts -S
或者
npm install echarts -S

2、创建图表

2.1、全局引入

全局引入会将所有的echarts图表打包,导致体积过大,因此项目中用的按需引入,但是这里还是记录用法。

// main.js中引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts 

使用echarts

export default {
  data () {
    return {
    }
  },
  mounted(){
    this.drawEcharts();
  },
  methods: {
    drawEcharts(){
        // 基于准备好的dom,初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('myChart'))
        // 绘制图表
        myChart.setOption(
            tooltip: {},
            xAxis: {
                data: [], // 实际数据为周一至周日组成的数组
            },
            yAxis: {},
            series: [{
                name: '电量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        });
    }
  }
}

2.1、按需引入与效果

按需引入在需要的时候引入echarts,比全局引入小很多,下面的echarts很多具体样式很多都省略掉了,没有写上去,因此实际效果与代码会有出入。

export default {
  data () {
    return {
    }
  },
  mounted(){
    this.drawEcharts();
  },
  methods: {
    drawEcharts(){
        // 基于准备好的dom,初始化echarts实例
        let myChart = echarts.init(document.getElementById('myChart'))
        // 绘制图表
        myChart.setOption(
        	color: ['#596a75'],
	        title: {
	          top: 70,
	          text: '', // 图表标题
	          textStyle: {
	            color: '#6997b3',
	            fontSize: 14,
	            fontWeight: 'normal'
	          }
	        },
            tooltip: {},
            xAxis: {
                data: [], // 实际数据为周一至周日组成的数组
            },
            yAxis: {},
            series: [{
                name: '电量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20],
                itemStyle: {
	            normal: {
	                color: function(params) {
	                  if (params.value < 40) {
	                    return '#f13834';
	                  }
	                  return '#3695d3';
	                }
	              }
	            }
            }]
        });
    }
  }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值