vue-cli中使用echarts

使用npm安装echarts

npm install echarts --save

运行完语句出现以下的内容表示echarts安装成功,4.3.0表示安装的echarts版本号在这里插入图片描述

使用echarts

<template>
<div id="bar_dv" style="width: 500px; height: 500px"> </div>   
</template>
<script>
  var echarts = require('echarts'); //引入echarts所有的方法。没有按需引入
  export default {
    name: "HelloWorld",
    data(){
      return{
        myChart: null,
      }
    },
    mounted(){
      this.showChart()
    },
    beforeDestroy() { //销毁实例
      if(!this.myChart){  //解决报错:cannot read property 'dispose' of null
        return;
      }
      this.myChart.dispose()
      this.myChart = null
    },
    methods:{
      showChart(){  
        this.myChart = echarts.init(document.getElementById("bar_dv")); //初始化echarts实例,id名称对应template中的id名称
        var option = {
          tooltip: {    //弹框
            trigger: 'item',
            position:'inside', //防止弹框跑出div,内容显示不全
            formatter: "{b}: {c}<br/>({d}%)" //b为data中的name值 c为value值 d为选中区域占比值
          },
          series: [
            { //内部环形图
              color: ['#3196fa', '#ef4864','#facc14' ],
              type:'pie',
              radius: [0, '45%'],
              label: {
                normal: {
                  position: 'inner',
                  textStyle:{
                    fontSize:'12',
                  },
                  formatter:'{b}\n{d}%',
                  position:'inside',
                },
              },
              labelLine: {
                normal: {
                  show: false,
                }
              },
              data:[ //内部环形图数据
                {value:'150', name:'大一' },
                {value:'170', name:'大二'},
                {value:'190', name:'大三'}
              ],
            },
            {//外面的环形图
              type:'pie',
              radius: ['50%', '60%'],
              label: {
                normal: {
                  show:false,
                }
              },
              data:[//外部环形图数据
                {value:'100', name:'男',itemStyle:{
                    normal:{
                      color:'#65b2ff'
                    }
                  }
                },
                {value:'50', name:'女',itemStyle:{
                    normal:{
                      color:'#c0d8f1'
                    }
                  }},
                {value:'110', name:'男',itemStyle:{
                    normal:{
                      color:'#ed7085'
                    }
                  }},
                {value:'60', name:'女',itemStyle:{
                    normal:{
                      color:'#f59cab'
                    }
                  }},
                {value:'120', name:'男',itemStyle:{
                    normal:{
                      color:'#f8d751'
                    }
                  }},
                {value:'70', name:'女',itemStyle:{
                    normal:{
                      color:'#f9e596'
                    }
                  }}
              ]
            }
          ]
        };
        this.myChart.setOption(option);
      }
    }
  }
</script>

在这j里插入图片描述
以上是一个嵌套环形图,在vue 中使用echarts的步骤主要为:
1:npm 安装echarts npm install echarts --save
2页面中引入echarts var echarts = require(‘echarts’)
3:在div中添加id名称,并给宽高 id=“bar_dv” style=“width: 500px; height: 500px”
4:初始化echarts实例 this.myChart = echarts.init(document.getElementById(“bar_dv”))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值