echarts学习 vue中使用echarts

本文介绍了如何在Vue项目中使用Echarts,包括在public文件夹下引入echarts.min.js,将其挂载到Vue原型对象,安装和使用axios进行后端数据请求,以及Echarts的初始化、数据获取、主题引入、柱状图样式修改、option拆分和分辨率适配等详细步骤。
摘要由CSDN通过智能技术生成

1、在public文件夹下新建 static/lib -->放入echarts.min.js文件(echarts.min.js 文件可以在jsDelivr中下载
2、然后在 public/index.html 文件中引用入

<script src="static/lib/echarts.min.js"></script>

3、在main.js文件中,将全局的echarts 挂载到Vue的原型对象上

Vue.prototype.$echarts = window.echarts

4、 axios(该部分用于后端数据请求)
1.安装 npm install axios
2.引入

import axios from 'axios'
// 请求基准路径的配置
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/'
// 将axios挂载到Vue的原型对象上
Vue.prototype.$axios = axios

5、 使用,echart ,初始化,数据获取

该部分为数据是由后端请求

<template>
  <div class="com-contain">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
export default {
     
  data () {
     
    return {
     
      chartInstance: null,
      allData: [], // 服务器返回的数据
      currentPage: 1, // 当前显示的页数
      totalPage: 0, // 一共有多少页 
      timer: null // 定时器的标识
    }
  },
  mounted () {
     
    this.initChart()
    this.getData()
  },
  destroyed () {
     
  	clearInterval(this.timer)
  },
  methods: {
     
    // 初始化chartInstance对象
    initChart () {
     
      this.chartInstance = this.$echarts.init(this.$refs.seller_ref)
 	  // 对图表对象进行鼠标事件的监听
 	  this.chartInstance.on('mouseover', () => {
     
 	  	clearInterval(this.timer)
 	  })
 	  this.chartInstance.on('mouseout', () => {
     
 	  	this.startInterval()
 	  })
    },

    // 获取服务器的数据
    async getData () {
     
      // http://127.0.0.1:8888/api/seller
      const data = await this.$axios.get('seller')
      this.allData = data.ret
      // 对数据排序
      this.allData.sort((
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值