vue+axios+echarts實(shí)現(xiàn)一個(gè)折線圖

前端可視化是一個(gè)前端最基本的技能,要想做的好看,還是得借助一下百度家的echarts,那要怎么在Vue中使用echarts?這個(gè)官網(wǎng)沒有給出實(shí)例,實(shí)例基本都是在jquery里面使用,引入的例子。

Echarts官網(wǎng):https://echarts.apache.org/zh/index.html

1:在項(xiàng)目里面安裝echarts

cnpm install echarts --s

2:在需要用圖表的地方引入

import echarts from "echarts";
圖片.png

3:打開vue組件
繼續(xù)寫代碼,代碼如下:

<template>
  <div id="app">
    <!--為echarts準(zhǔn)備一個(gè)具備大小的容器dom-->
    <div id="main" style="width: 600px; height: 400px"></div>
  </div>
</template>
<script>
import echarts from "echarts";
export default {
  name: "",
  data() {
    return {
      charts: "",
      opinionData: ["3", "2", "4", "4", "5"],
    };
  },
  methods: {
    drawLine(id) {
      this.charts = echarts.init(document.getElementById(id));
      this.charts.setOption({
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["近七日收益"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },

        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: ["1", "2", "3", "4", "5"],
        },
        yAxis: {
          type: "value",
        },

        series: [
          {
            name: "近七日收益",
            type: "line",
            stack: "總量",
            data: this.opinionData,
          },
        ],
      });
    },
  },
  //調(diào)用
  mounted() {
    this.$nextTick(function () {
      this.drawLine("main");
    });
  },
};
</script>
<style scoped>
</style>

這個(gè)時(shí)候,可以看到,加載出的折線圖了,后面可以繼續(xù)進(jìn)行完善。


以上完成的,只是一個(gè)靜態(tài)頁面,下面就開始完成動(dòng)態(tài)數(shù)據(jù)渲染部分的折線圖部分啦~~


1:進(jìn)入項(xiàng)目,npm安裝

npm install axios --save

2.在main.js下引用axios

import axios from 'axios'

image

3:準(zhǔn)備json數(shù)據(jù)

{
    "categories": [
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12"
    ],
    "data": [
        3,
        2,
        4,
        4,
        5
    ]
}

自己寫了一個(gè)json數(shù)據(jù),放在服務(wù)器上,現(xiàn)在要通過vue項(xiàng)目調(diào)用數(shù)據(jù)
http://xxx.com/test.json

4:跨域問題,
一般后端小伙伴給到我們這邊的接口里面,應(yīng)該對(duì)跨域問題已經(jīng)處理好了,所以不需要我們處理了。

如果需要我們處理,我們可以設(shè)置代理,利用proxyTable屬性實(shí)現(xiàn)跨域請(qǐng)求
在config/index.js 里面找到proxyTable :{} ,然后在里面加入以下代碼
(這里處于安全考慮,我隱藏了自己的而服務(wù)器域名,如果需要測(cè)試,改成你自己的即可)

   proxyTable: {
  '/api': {
     target: 'http://inxxxe.com',//設(shè)置你調(diào)用的接口域名和端口號(hào) 
    changeOrigin: true,//允許跨域
    pathRewrite: {
      '^/api': '' //這個(gè)是定義要訪問的路徑,名字隨便寫 
    }
  }
},

5:打開一個(gè)界面test.vue,開始寫請(qǐng)求數(shù)據(jù)的方法

 methods: {
            getData() {
                axios.get('/api/test.json').then(response => {
                    console.log(response.data);
                       this.opinionData =response.data.data;
                        this.drawLine('main')
                }, response => {
                    console.log("error");
                });
            },
        }

6:再次運(yùn)行

npm run dev

運(yùn)行成功之后,打開f12,查看network的請(qǐng)求
這個(gè)時(shí)候,我們可以看見,本地的localhost替代 了我之前放在服務(wù)器上的鏈接的域名,這也是設(shè)置代理成功,就解決了跨域的問題了。

請(qǐng)求成功

response里面也有返回值,ok,下一步就要開始將這些數(shù)據(jù)渲染在前端界面上面了。


test.vue參考代碼:

<template>
    <div>
    <!--為echarts準(zhǔn)備一個(gè)具備大小的容器dom-->
    <div id="main" style="width: 600px;height: 400px;"></div>
    </div>
</template>
<script>
    import echarts from 'echarts'
      import axios from "axios";
    export default {
        name: '',
        data() {
            return {
                charts: '',
          
               /* opinionData: ["3", "2", "4", "4", "5"]*/
               opinionData: []
            }
        },
        methods: {
             getData() {
                axios.get('/api/test.json').then(response => {
                    console.log(response.data);
                       this.opinionData =response.data.data;
                        this.drawLine('main')
                }, response => {
                    console.log("error");
                });
            },
            drawLine(id) {
                this.charts = echarts.init(document.getElementById(id))
                this.charts.setOption({
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['近七日收益']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },

                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                    data: ["1","2","3","4","5"]
                    
                    },
                    yAxis: {
                        type: 'value'
                    },

                    series: [{
                        name: '近七日收益',
                        type: 'line',
                        stack: '總量',
                        data: this.opinionData
                    }]
                })
            },
                                
        },
        //調(diào)用
        mounted() {
            this.getData();         
        }
    }
</script>

實(shí)現(xiàn)效果


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容