Vue 使用Echarts

在vue中使用echarts有两种方法
一、安装并全局引入
1、通过npm获取echarts
npm install echarts --save

2、在 main.js 中添加下面两行代码

import * as echarts from 'echarts'

Vue.prototype.$echarts = echarts //挂载到Vue实例上面

二、使用  https://echarts.apache.org/examples/zh/index.html#chart-type-pie

<template>
  <div>
    <div class="container">
        <div id="echart"></div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  // 页面初始化挂载dom
  mounted() {
    this.getLoadEcharts();
  },
  methods: {
    getLoadEcharts() {
      var myChart = this.$echarts.init(
        document.getElementById("echart")
      );
      var colorList = ["#4FE894", "#EF0C27", "#FEE7EA"];
      var listData = [
        { name: "不明", value: 20 },
        { name: "已判明", value: 20 },
        { name: "未发现", value: 20 }
      ];
      var option = {
        color: colorList,
        title: {
          x: "center",
          y: "center",
          textStyle: {
            color: "#000",
            fontSize: 20
          }
        },
        legend: {
          orient: "vertical",
          bottom: 5,
          align: "right",
          right: 15,
          textStyle: {
            color: "#000",
            fontSize: 20
          },
          selectedMode: false,
          data: ["元素1", "元素2", "元素3"]
        },
        tooltip: {
          trigger: "item"
        },
        series: [
          {
            type: "pie",
            center: ['50%', '50%'],
            radius: ['55%', '35%'],
            itemStyle: {
              normal: {
                color: function(params) {
                  return colorList[params.dataIndex];
                }
              }
            },
            label: {
              show: true,
              fontSize: 10,
              color: "#000",
              formatter: function(data) {
                return data.name + ":" + data.percent.toFixed(0) + "%";
              }
            },
            labelLine: {
              normal: {
                length: 15,
                length2: 15,
                lineStyle: {
                  width: 2
                }
              }
            },
            data: listData
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>
<style scoped>
.container{
    width: 300px;
    height: 300px;
    margin-left: 30px;
}
#echart{
    width: 100%;
    height: 100%;
}
</style>
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值