ECharts小练习

164 阅读1分钟

在vue项目中使用ECharts

ECharts官网

echarts社区

1.使用npm 安装 ECharts

npm install echarts --save

2. 在main.js中引入,并挂载到vue的prototype上

import Vue from 'vue'
import App from './App.vue'
import echarts from 'echarts'
// var echarts = require('echarts');

Vue.prototype.$echarts=echarts
Vue.config.productionTip = false // 设置为 false 以阻止 vue 在启动时生成生产提示。

new Vue({
  render: h => h(App),
}).$mount('#app')

3. 在.vue文件中使用:

3.1 使用柱状图:

<template>
  <div id="app">
    <div id='main' class="mainBox"></div> 
    <!-- 一定要给这个div设置宽高 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
    };
  },
  methods: {
    init() {
      // console.log('init',this.$echarts,document.getElementById("main"));
      // 基于准备好的dom,初始化echarts实例

      var myChart = this.$echarts.init(document.getElementById("main"));
      // 绘制图表
      myChart.setOption({
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      });
    },
  },
  mounted(){
    this.init()
  }
};
</script>

<style>
.mainBox{
  width: 400px;
  height: 400px;
}
</style>


4. 效果: