Vue中使用Echarts

基本用法

  • 打开终端输入:npm install echarts --save
  • 在main.js里面全局引入的方式
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 初始样式
import './assets/css/base.css'

import echarts from 'echarts'

Vue.config.productionTip = false
Vue.prototype.$echarts = echarts

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

  • 在需要使用的组件页面中
    1、template中的代码
<div id="sCharts"></div>

2、script中的代码

export default {
  methods: {
    //   图表
    initEcharts () {
      let myChart = this.$echarts.init(document.getElementById('sCharts'))
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        xAxis: [
          {
            type: 'category',
            data: [
              '1月',
              '2月',
              '3月',
              '4月',
              '5月',
              '6月',
              '7月',
              '8月',
              '9月',
              '10月',
              '11月',
              '12月'
            ],
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '总体积',
            min: 0,
            max: 10,
            interval: 1,
            axisLabel: {
              formatter: '{value} m³'
            }
          },
          {
            type: 'value',
            name: '使用负荷',
            min: 0,
            max: 100,
            interval: 10,
            axisLabel: {
              formatter: '{value} %'
            }
          }
        ],
        series: [
          {
            name: '总体积',
            type: 'bar',
            data: [
              2.0,
              4.9,
              5.5,
              2.2,
              4.6,
              6.7,
              5.6,
              6.2,
              3.6,
              2.0,
              6.4,
              3.3
            ],
            itemStyle: {
              normal: {
                color: '#d0e17d'
              }
            }
            // label: {
            //   show: true
            // }
          },
          {
            name: '使用负荷',
            type: 'line',
            yAxisIndex: 1,
            data: [
              100,
              22,
              33,
              45,
              63,
              80,
              20,
              23,
              23,
              65,
              12,
              6.2
            ],
            itemStyle: {
              normal: {
                color: '#898989'
              }
            }
          }
        ]
      }
      myChart.setOption(option)
    }
  },
  mounted () {
    this.initEcharts()
  }
}

3、css中的代码

#sCharts {
     height: 500px;
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值