Vue中使用echarts图表

一、安装引入echarts插件

安装

npm install echarts -S 

在main.js中配置引入

// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
Vue.use(echarts)

二、使用

<template>
  <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
  <div id="main" style="width: 800px;height:600px;font-size: 20px;margin-left: 50px;padding: 10px"></div>
</template>

<script>
  export default {
    name: "echart-sales",
    data() {
      return {
        getSalesUrl: '/manage/category/getSales',
        data1: [],
        dataName: []
      }
    },
    methods: {
      getEchars() {
        // 基于准备好的dom,初始化echarts实例
        var myChart = this.$echarts.init(document.getElementById('main'));
        console.log("获取echarts表");

        // 指定图表的配置项和数据
        var option = {
          title: {
            text: '图书销量图例',
            subtext: '按类别进行统计',
            x: 'center',
            textStyle: {
              fontSize: 30
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          legend: {
            orient: 'vertical',
            left: 'left',
            data: this.dataName,
            textStyle: {
              fontSize: 20 // 用 legend.textStyle.fontSize 更改示例大小
            }
          },
          series: [
            {
              name: '访问来源',
              type: 'pie',
              radius: '55%',
              center: ['50%', '60%'],
              data: this.data1,
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                },
                normal: {
                  label: {
                    textStyle: {
                      // 用 itemStyle.normal.label.textStyle.fontSize 來更改餅圖上項目字體大小
                      fontSize: 20
                    }
                  }
                }
              }
            }
          ]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
      }
    },
    mounted:function() {
//挂载前请求数据
      this.$http.get(this.getSalesUrl)
        .then((res) => {
          console.log(res);
          this.data1 = res.data.data;
          console.log(this.data1);
          for (var i = 0; i < this.data1.length; i++) {
            this.dataName[i] = this.data1[i].name;
          }
//渲染数据
          this.getEchars();

        })

    }
  }
</script>

<style scoped>

</style>

三、运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟林洁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值