Echarts图表在vue中的基本使用

安装Echarts依赖

npm install echarts --save

在main.js中引入
import * as echarts from 'echarts';

Vue.prototype.$echarts = echarts
在需要使用图表的组件中编写
 // 初始化图表
    initChart() {
      this.chartInstace = this.$echarts.init(this.$refs.seller);
      this.chartInstace.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.chartInstace.on("mouseout", () => {
        this.startSetinterval();
      });
    },
    // 获取图表数据
    async getData() {
      // 获取retail的所有数据
      var { data: res } = await getretail();
      // 然后将数据赋值给ydata,xdata
      this.tableData = res;
      this.number = res;
      this.totalpage =
        this.tableData.length % 5 === 0
          ? this.tableData.length / 5
          : this.tableData.length / 5 + 1;
      this.updateChart();
      this.startSetinterval();
    },
    // 挂载
    updateChart() {
      const start = (this.currentpage - 1) * 5;
      const end = this.currentpage * 5;
      const showdata = this.tableData.slice(start, end);
      const sellername = showdata.map((item) => {
        return item.pname;
      });
      const sellernum = showdata.map((item) => {
        return item.outnumber;
      });
      var option = {
        title: {
          textStyle: { color: "#fff" },
          text: "零售商品数据",
        },
        tooltip: {},
        legend: {
          data: ["销量"],
          textStyle: { color: "#fff" },
        },
        xAxis: {
          axisLabel: {
            color: "#fff",
          },
          type: "category",
          data: sellername,
        },
        yAxis: {
          min: 0,
          max: function (value) {
            return value.max+20;
          },
          axisLabel: {
            color: "#fff",
          },
          type: "value",
        },
        // color:["#ccc","#fff"],
        series: [
          {
            name: "销量",
            type: "bar",
            data: sellernum,
            barWidth: 50,
            itemStyle:{
              normal:{
                label:{
                  formatter:"{c}",
                  show:true,
                  position:"top",
                  textStyle:{
                    fontWeight:'bolder',
                    fontSize:"12",
                    color:"#fff"
                  }
                }
              }
            }
          },
        ],
      };
      this.chartInstace.setOption(option);
    },
    // 柱状图轮播展示
    startSetinterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.currentpage++;
        if (this.currentpage > this.totalpage) {
          this.currentpage = 1;
        }
        this.updateChart();
      }, 3000);
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LBY_XK

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

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

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

打赏作者

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

抵扣说明:

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

余额充值