vue中怎么引入echarts

1 篇文章 0 订阅

引入命令:

npm install echarts --save

全局引入:

在main.js中写入:

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

在其他页面用到echarts时,需要先有一个HTML标签:(id不可缺少)

 <div id="monthorder" style="width:100%;height:300px"></div>

接下来echart的应用方式为:

var myechart = this.$echarts.init(document.getElementById("monthorder"));

按需引入:

在需要用到的页面引入,以饼状图为例:

//引入echart
var echarts = require("echarts/lib/echarts");
//引入饼状图
require("echarts/lib/chart/pie");
//引入title等等
require("echarts/lib/component/tooltip");
require("echarts/lib/component/title");
require("echarts/lib/component/legend");
require("echarts/lib/component/legendScroll");

然后就是按照网上一般的写法写就行,

我参照了:https://echarts.baidu.com/examples/editor.html?c=pie-legend

 

 

下面贴一部分我写的:

<template>
  <div class="indexs">
          <div id="monthorder" style="width:100%;height:300px"></div>
  </div>
</template>
<script>
var echarts = require("echarts/lib/echarts");
require("echarts/lib/chart/pie");
require("echarts/lib/component/tooltip");
require("echarts/lib/component/title");
require("echarts/lib/component/legend");
export default {
  name: "indexss",
  data() {
    return {
      monthlist: {
        monthdata: [
          "未付款的订单",
          "待确认的订单",
          "待发货的订单",
          "已发货的订单",
          "已完成的订单",
          "取消的订单"
        ],
        time: "2018.01.01-2019.09.01",
        precent: [0, 10, 12, 15, 30, 1],
        total: 88
      }
    };
  },
  created() {
    this.$nextTick(function() {
      this.montheahcrt();
    });
  },
  methods: {
    montheahcrt() {
      var that = this;
      var monthseries=that.monthseries();
      var myechart = echarts.init(document.getElementById("monthorder"));
      myechart.setOption({
        title: { subtext: that.monthlist.time,x:'center' },
        tooltip: { trigger: "item", formatter: "{a} <br/>{b} : {c} ({d}%)" },
      
        legend: {
          type: "scroll",
          orient: "vertical",
          right: 0,
          top: 0,
          bottom: 20,
          data:monthseries.legendData,
          selected:monthseries.selected
        },
        series: [
          {
            name:'',
            type: "pie",
            radius: "50%",
            center: ["60%", "50%"],
            data:monthseries.seriesData
          }
        ]
      });
    },
    monthseries() {
      var data = this.monthlist;
      var legendData = [];
      var seriesData = [];
      var selected = {};
      for (var i = 0; i < data.monthdata.length; i++) {
        seriesData.push({
          name: data.monthdata[i],
          value: data.precent[i]
        });
        legendData.push(data.monthdata[i]);
        selected[data.monthdata[i]] = true;
      }
      return {
        seriesData,
        selected,
        legendData
      };
    }
  }
};
</script>
<style scoped>
.indexs {
  width: 100%;
}
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值