vue使用echarts,封裝復用組件


echarts是我們項目中常用的插件,在這要封裝一個echarts組件,在項目中能夠復用

我們先安裝依賴

npm install echarts

 安裝成功以后在main.js中引入 

import echart from 'echarts'
Vue.prototype.$echarts = echart 

  在components文件夾下創建c-bar文件夾,c-bar文件夾創建c-bar.vue以及index.js文件

 

 c-bar.vue文件寫入

<template>
    <div class="bar">
      <div :id="id" class="bar_canvas"></div>
    </div>
</template>
<script>
export default {
  name: "c-bar",
  props: ["propData", "id"],
  data() {
    return {
      myChart: null
    };
  },
  mounted() {
    if (this.id) {
      let myChart = this.$echarts.init(document.getElementById(this.id));
      this.myChart = myChart;
      this.myChart.setOption(this.propData);
    }
  }
};
</script>
<style scoped lang="scss">
.bar {
  width: 100%;
  height: 100%;
  position:absolute;
}
.bar_canvas {
  width: 100%;
  height: 100%;
}
</style>

  index.js文件寫入

import cBar from './c-bar.vue';

cBar.install = function (Vue) {
    Vue.component(cBar.name, cBar);
};
export default cBar;

  然后在main.js中引入組件

import cBar from '@/components/c-bar';
cBar.install(Vue)

  這樣我們就可以全局使用組件了

 我們在使用時

 

<template>
<div>
    <div class="bar">
    <c-bar :propData="dataArr" id="canvans" class="bar_canvas"></c-bar>
  </div>
  <div class="bar1">
    <c-bar :propData="option" id="can_view" class="bar_canvas1"></c-bar>
  </div>
</div>

</template>

<script>
export default {
  data() {
    return {
      myChart: null,
      option: {
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line'
    }]
    },
      dataArr: {
        tooltip: {
          trigger: "item",
          formatter: "{b}: {c} ({d}%)"
        },
        series: [
          {
            name: "",
            type: "pie",
            radius: ["20%", "30%"],
            label: {
              formatter: " {per|{d}%} {b|{b}} ",
              rich: {
                b: {
                  color:"#fff",
                  fontSize: 16,
                  lineHeight: 33
                },
                per: {
                  padding: [2, 4],
                  borderRadius: 2
                }
              }
            },
            data: [
        {
          value: 20,
          name: "win8",
          itemStyle: { normal: { color: "#02B672" } } //設置圓環的顏色
        },
        {
          value: 20,
          name: "win10",
          itemStyle: { normal: { color: "#0FC2E7" } }
        },
        {
          value: 20,
          name: "winXP",
          itemStyle: { normal: { color: "#0C6CE8" } }
        },
        {
          value: 20,
          name: "macOS",
          itemStyle: { normal: { color: "#DF4546" } }
        },
        {
          value: 20,
          name: "win7",
          itemStyle: { normal: { color: "#F7BB2D" } }
        }
      ]
          }
        ]
      }
    };
  }
};
</script>

<style lang="scss">
.bar {
  width: 500px;
  height: 500px;
  position: absolute;
  top: 0;
  right: 0;
}
.bar1 {
  width: 500px;
  height: 500px;
  position: absolute;
  top: 0;
  left: 0;
}
.bar_canvas {
  width: 100%;
  height: 100%;
}
</style>

  

 

 這樣我們就可以復用了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM