NUXT-中使用-Echarts

10次阅读

共计 1301 个字符,预计需要花费 4 分钟才能阅读完成。

一、安装 echarts 依赖包

npm install echarts --save

二、在 plugins 目录下创建 echarts.js 文件并在里面引入 echarts 依赖包

import Vue from 'vue'
import echarts from 'echarts' // 引入 echarts
Vue.prototype.$echarts = echarts // 引入组件(将 echarts 注册为全局)

三、在 nuxt.config.js 配置文件中引入我们刚刚创建的 echart.js

  // 配置插件
  plugins: ['~plugins/iview', '~plugins/echarts'],

四、使用

<template>
 <div id="echarts">
   <div id="myChart"></div>
   <div id="myChart2"></div>
 </div>
</template>
<script type="text/javascript">
export default {
  name: 'Echarts',
  data () {return {}
  },
  methods: {echartsInit () {
      // 找到容器
      let myChart = this.$echarts.init(document.getElementById('myChart'))
      // 开始渲染
      myChart.setOption({title: {text: '在 Vue 中使用 echarts'},
        tooltip: {},
        xAxis: {data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [{
          name: '销量',
          type: 'bar',
          data: [5, 20, 36, 10, 10, 20]
        }]
      })
    },
    echartsInit2 () {let myChart = this.$echarts.init(document.getElementById('myChart2'))
      myChart.setOption({title: {text: 'echarts 饼图'},
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '55%',
            data: [{value: 235, name: '视频广告'},
              {value: 274, name: '联盟广告'},
              {value: 310, name: '邮件营销'},
              {value: 335, name: '直接访问'},
              {value: 400, name: '搜索引擎'}
            ]
          }
        ]
      })
    }
  },
  mounted () {this.echartsInit()
    this.echartsInit2()}
}
</script>
<style scoped lang="less">
  #myChart{
    width: 600px;
    height: 300px;
    margin-left: auto;
    margin-right: auto;
    float: left;
  }
  #myChart2{
    width: 600px;
    height: 300px;
    margin-left: auto;
    margin-right: auto;
    float: right;
  }
</style>

END


觉得有帮助的小伙伴点个赞支持下~

正文完
 0