【vue】echarts圖表使用方法


首先在cmd中輸入指令,ECharts 和 zrender 安裝完成會放在node_modules目錄下可以直接在項目代碼中 require('echarts') 得到 ECharts

npm install echarts --save

之后直接頁面使用Echarts

HTML代碼

<template>
    <div class="chart" ref="myEchart"></div>
</template>

script代碼

<script>
  export default {
    name: 'chart',
    data() {
      return {

      }
    },
    mounted() {
      this.getPillar();
    },
    methods: {

      // 渲染圖表
      getPillar() {
        var echarts = require('echarts');
        // 基於准備好的dom(myEchartPillar),初始化echarts實例
        let myChart = echarts.init(this.$refs.myEchart);
        //指定圖表的配置項和數據,繪制圖表
        myChart.setOption({
          title: {
            text: '折線圖堆疊'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['郵件營銷', '聯盟廣告', '視頻廣告', '直接訪問', '搜索引擎']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          toolbox: {
            feature: {
              saveAsImage: {}
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
          },
          yAxis: {
            type: 'value'
          },
          series: [{
              name: '郵件營銷',
              type: 'line',
              stack: '總量',
              data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
              name: '聯盟廣告',
              type: 'line',
              stack: '總量',
              data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
              name: '視頻廣告',
              type: 'line',
              stack: '總量',
              data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
              name: '直接訪問',
              type: 'line',
              stack: '總量',
              data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
              name: '搜索引擎',
              type: 'line',
              stack: '總量',
              data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
          ]
        });
        //解決自適應
        setTimeout(function() {
          window.addEventListener("resize", () => {
            myChart.resize();
          });
        }, 500);
      }
    }
  }
</script>

style代碼

<style>
.chart{
  width: 100%;
  height: 100%;
}
</style>

案例展示


免責聲明!

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



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