在Vue 項目中使用echarts

第一種方法,直接引入echartsvue

1. 安裝echarts項目依賴
    npm install echarts --save 2. 在main.js中全局引入 import echarts from "echarts"; Vue.prototype.$echarts = echarts;
<template>
  <div id="app">
    <div id="main" style="width: 600px;height:400px;"></div>
  </div>
</template>

js部分 script 代碼npm

<script>
export default {
  name: "app",
  methods: {
    drawChart() {
      // 基於準備好的dom,初始化echarts實例
      let myChart = this.$echarts.init(document.getElementById("main"));
      // 指定圖表的配置項和數據
      let option = {
        title: {
          text: "ECharts 入門示例"
        },
        tooltip: {},
        legend: {
          data: ["銷量"]
        },
        xAxis: {
          data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
        },
        yAxis: {},
        series: [
          {
            name: "銷量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      };
      // 使用剛指定的配置項和數據顯示圖表。
      myChart.setOption(option);
    }
  },
  mounted() {
    this.drawChart();
  }
};
</script>

 

第二種方法,使用 Vue-ECharts 組件app

步驟一 : 安裝組件echarts

npm install vue-echarts -S

步驟二 : 使用組件dom

<template>
  <div id="app">
    <v-chart class="my-chart" :options="bar"/>
  </div>
</template>
<script>
import ECharts from "vue-echarts/components/ECharts";
import "echarts/lib/chart/bar";
export default {
  name: "App",
  components: {
    "v-chart": ECharts
  },
  data: function() {
    return {
      bar: {
        title: {
          text: "ECharts 入門示例"
        },
        tooltip: {},
        legend: {
          data: ["銷量"]
        },
        xAxis: {
          data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
        },
        yAxis: {},
        series: [
          {
            name: "銷量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      }
    };
  }
};
</script>
<style>
.my-chart {
  width: 800px;
  height: 500px;
}
</style>
相關文章
相關標籤/搜索