vue引入echarts图表根据接口请求获取数据

vue引入echarts图表根据接口请求获取数据

下载echarts

npm i echarts -S

main.js引入echarts

Vue.use(echarts);
Vue.prototype.$echarts = echarts;

1.html 命名id 在带有id的盒子上进行绘制图表

      <div class="echarts_box">
        <div id="tradingIndex" class="canvans"></div>
      </div>

2.css 设置绘制图表盒子的宽高

.echarts_box {
  width: 95%;
  height: 550px;
}
.canvans {
  width: 100%;
  height: 550px;
}

3.js

	//  
	mounted() {
		//调用接口函数
    	this.getData();
  	},
	//请求接口
    getData() {
      this.$axios({
        url: "/***/****/****", 
        method: "post",
      }).then((res) => {
        console.log(res);
        if (res.data.status == 1) {
          let arr = res.data.data;
          arr.forEach((item) => { //处理数据
            this.LineDataX.push(item.show_date);
            this.LineDataY.push(parseInt(item.total_money / item.total_num));
          });
          //再调用图标函数
          this.$nextTick(() => {
            this.getStatisticalLine();
          });
        }
      });
    },
    // 绘制折线图
    getStatisticalLine() {
      let chart = this.$echarts.init(document.getElementById("tradingIndex")); //引用带有id的标签
      let option = {  //图表的样式
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category",
          data: this.LineDataX,
        },
        yAxis: {
          type: "value",
          name: "交易指数(金额/数量)",
        },
        series: [
          {
            data: this.LineDataY,
            type: "line",
          },
        ],
      };
      chart.setOption(option); //绘制 必须要有
      window.addEventListener("resize", () => {  //根据屏幕进行实时绘制
        chart.resize();
      });
    },

4,展示样式
在这里插入图片描述
5.这个网站有很多echarts的模板基本可以实现项目中的一些图表

网址:

https://www.makeapie.com/explore.html#charts=barcomponents=gridsort=ranktimeframe=allauthor=all

希望大家多多支持

  • 13
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Cheng Lucky

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

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

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

打赏作者

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

抵扣说明:

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

余额充值