Vue中引入echarts的步骤,折线图、柱状图、饼图的常用配置项

一、安装echarts

npm i echarts

二、引入echarts

1.全局引入

在main.js文件中:

//全局引入echarts
import * as echarts from 'echarts';
//需要挂载到Vue原型上
Vue.prototype.$echarts = echarts;

2.局部引入

let echarts = require(‘echarts’);

或者

import * as echarts from "echarts";

注意:如果echarts的版本在5.0以上,只能局部引入,全局引入会出错(好像是这样,如果不对欢迎指正)

三、vue中使用echarts

1.准备好函数

准备一个函数,并在挂载时调用

<script>
import * as echarts from "echarts";
export default {
    name: 'MyData',
    data() {
        return {

        };
    },
    mounted() {
        this.echartsInit();
    },
    methods: {
        echartsInit() {

        }
    },
};
</script>

2.准备一个容器来放echarts

配置好ref属性,用于后面初始化容器获取DOM

<div ref="chart"></div>

3.初始化容器

使用init函数

methods: {
     echartsInit() {
     		//初始化容器
			const myChart = echarts.init(this.$refs.pro_chart); 
      }
  },

4.调节配置项

option配置项从这里搞:echarts官方文档示例

echartsInit() {
        const myChart = echarts.init(this.$refs.pro_chart); //初始化容器
        const option = {这里面就是图表的各种配置项,从官方文档搞下来};
  }

5.配置项放入容器,绘制图表

echartsInit() {
	     const myChart = echarts.init(this.$refs.chart); //初始化容器
	     const option = {这里面就是图表的各种配置项,从官方文档搞下来};
	     myChart.setOption(option);
 }

四、一些常用的配置项

1.折线图

option = {
    title: {
        text: '标题'
    },
    //图例的相关设置
    legend: {
    	x:'center', //水平位置
	    y:'bottom', //垂直方向位置
	    padding: [10,0,0,0], //上右下左距离
	    itemWidth: 30,  //图例宽
        itemHeight: 30, //图例高
        textStyle: {  //图例的字体样式
            fontSize: 26,  
            color: '#666',
        },
	    data: ['类目1', '类目2'], //图例名字,要和数据的name对应
	 },
    //这是鼠标移到某个数据上显示的面板配置
    tooltip: {
        trigger: 'item',
        triggerOn: 'click',
        axisPointer: {
            type: 'none'
        },
        formatter: function () {
            return '17.5KG'
        }
    },
    //这是一些工具,比如下面这个saveAsImage是保存为图片的选项
    toolbox: {
        show: false,
        feature: {
            saveAsImage: {}
        }
    },
    //图的距离
    grid: {
        left: '3%',
        right: '4%',
        bottom: '5%',
        top: '5%',
        containLabel: true
    },
    //x轴相关配置
    xAxis: {
        type: 'category',
        boundaryGap: false, //坐标值是展示在小头头上还是展示在头头之间,你懂的
        axisLabel: {
            //设置x轴的展示间隔
            interval: 0,
            //x轴坐标文字换行
            formatter: function (value, index) {
                var num = 5; //每行显示字数 
                var str = "";
                var valLength = value.length; //该项x轴字数  
                var rowNum = Math.ceil(valLength / num); // 行数  
                if (rowNum > 1) {
                    for (var i = 0; i < rowNum; i++) {
                        var temp = "";
                        var start = i * num;
                        var end = start + num;
                        temp = value.substring(start, end) + "\n";
                        str += temp;
                    }
                    return str;
                } else {
                    return value;
                }
            }
        },
        //刻度相关配置:
        axisTick: {
            show: false,//去掉刻度
        },
        //轴线相关配置:
        axisLine: {
            show: false,  //去掉y轴的线
            lineStyle: {color: '#ccc'},  //设置轴线颜色
        },
        // prettier-ignore
        data: ['1','1''1''1''1''1''1''1']
    },
    yAxis: {
        type: 'value',
        //隐藏y轴的横线
        splitLine: {
            show: false
        },
        //设置y轴的初始值和结束值
        min: '20',
        max: '24.5',
        splitNumber: 9,  //设置y轴的间隔
        axisLabel: {
            formatter: function (value) {
                //保留一位小数并且加上单位
                return value.toFixed(1) + ' °C';
            }
        },
        axisPointer: {
            snap: true
        },
        //刻度相关配置:
        axisTick: {
            show: false,//去掉刻度
        },
        //轴线相关配置:
        axisLine: {
            show: false,  //去掉y轴的线
            lineStyle: {color: '#ccc'},  //设置轴线颜色
        },
    },
    series: [
        {
            name: '类目1',
            type: 'line',
            lineStyle: {
                color: 'rgb(118, 162, 255, 1)', //改变折线颜色
                normal: {
                    opacity: 0, //透明度,0隐藏1显示
                }
            },
            showSymbol: true,  //显示隐藏小圆点
            itemStyle: {
                color: 'RGBA(118, 162, 255, 1)', //小圆点的颜色
            },
            showBackground: true, //是否展示背景
            backgroundStyle: {
                color: 'RGBA(255, 228, 218, 1)'  //背景色
            },
            smooth: true, //数据是否平滑连接
            data: [21.1, 22.1, 23.5, 23.2, 22.5, 23.2, 22.1],
        },
        {
            name: '类目2',
            type: 'line',
            lineStyle: {
                color: 'rgb(63, 207, 153, 1)', //改变折线颜色
                normal: {
                    opacity: 1, //透明度,0隐藏1显示
                }
            },
            showSymbol: true,  //显示隐藏折线上的小圆点
            itemStyle: {
                color: 'RGBA(63, 207, 153, 1)'
            },
            smooth: true,//数据是否平滑连接
            data: [21.3, 22.8, 22.5, 23.6, 21.5, 23.2, 21.1],
        },
    ]
};

2.柱状图

option = {
	//图例
    legend: {
		x: 'center',
		y: 'bottom',
		padding: [10, 0, 3, 0],
		textStyle: {  //图例的字体样式
			color: '#fff',
		},
		data: ['类目A', '类目B']
	},
	//在容器中的位置
    grid: {
        left: '2%',
        right: '3%',
        bottom: '10%',
        top: '5%',
        containLabel: true
    },
    //悬浮卡片
    tooltip: {
        trigger: 'axis',  //坐标轴触发
        triggerOn: 'click', //点击显示卡片
        axisPointer: {
            type: 'none'
        },
        formatter: function () {
            return '奥里给'  //自定义悬浮卡片显示的数值
        }
    },
    xAxis: {
        type: 'category',
        data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
        //去掉最下面的头头
        axisTick: {
            show: false
        },
        //刻度相关配置:
        axisTick: {
            show: false,//去掉刻度
        },
        //轴线相关配置:
        axisLine: {
            show: false,  //去掉y轴的线
        },
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: function (value) {
                //保留一位小数并且加上单位
                return value + 'KG';
            }
        },
        //隐藏y轴的横线
        splitLine: {
            show: false
        },
        //设置y轴的初始值和结束值
        min: '10',
        max: '19',
        splitNumber: 9,  //设置y轴的间隔
        //刻度相关配置:
        axisTick: {
            show: false,//去掉刻度
            alignWithLabel: true, //刻度和值居中对齐
        },
        //轴线相关配置:
        axisLine: {
            show: false,  //去掉y轴的线
        },
    },
    series: [
        {
            name: '类目A',
            data: [12, 15, 15, 13, 17, 14, 13, 15, 13, 17, 14, 16],
            type: 'bar',
            barGap: '10%',  //设置柱子间隔
            itemStyle: {
                //设置渐变色
                opacity: 1, //透明度0隐藏,1显示
                barBorderRadius: [25, 25, 0, 0], //顶部的圆角弧度
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: '#73A2FF' }, //渐变上面
                    { offset: 1, color: '#11AFFA' } //渐变下面
                ])
            },
            showBackground: true,//是否展示背景并设置颜色
            backgroundStyle: {
                color: 'RGBA(255, 228, 218, 1)'
            }
        },
        {
            name: '类目B',
            data: [14, 17, 14, 13, 17, 14, 15, 17, 14, 13, 17, 12],
            type: 'bar',
            itemStyle: {
                opacity: 1,
                barBorderRadius: [25, 25, 0, 0],//顶部的圆角弧度
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: '#3FCF98' },
                    { offset: 1, color: '#10CEF0' }
                ])
            },
            showBackground: false,  //是否展示背景
        },
    ]
};

3.环形饼图显示百分比

option = {
  //第一个是底色,第二个是高亮色
  color:['rgb(78,253,149)','rgb(28,39,62)'], 
  series: [
    {
      name: '环形饼图',
      type: 'pie',
      radius: ['50%', '70%'],  //内圈占比,外圈占比
      hoverAnimation: false,//禁止鼠标悬停放大
      avoidLabelOverlap: true, //防止标签重叠
      //显示数值的样式
      label: {
          show: true,
          position: 'center', //位置
          formatter : function (data){
              // console.log(data)
              let a   =  data. name
              let b   =  data.percent.toFixed (0) + "%"
              let c   =   a + ':' + b
             // return a+':'+b
              return c  //去掉小数,加单位
          },
      },
      data: [
          {
            value: 95,  //第一个数值为余量的值
            selected:true,     //默认选中第一块
            label:{
                show:true,     //默认显示第一块
                fontSize: '40',
                fontWeight: 'bold',
                color: 'rgb(53,243,253)'
            }
          },
          {
            value: 5,//第二个数值为总量-余量的值,两个值加起来是总量
            label: {
              show:false
            }
          },  
      ]
    }
  ]
};

五、有其他问题去翻文档:echarts官网

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值