axios請求Echarts折線圖

在jQuery裏面,實現一個折線圖,【前端統計圖】echarts實現單條折線圖 www.jianshu.com/p/0354a4f8c…前端

1:在項目裏面安裝摺線圖 cnpm install echarts --svue

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

import echarts from 'echarts' ios

3:打開echarts.vue 繼續寫代碼,代碼以下:web

<template>
	<div>
    <!--爲echarts準備一個具有大小的容器dom-->
    <div id="main" style="width: 600px;height: 400px;"></div>
    </div>
</template>
<script>
    import echarts from 'echarts'
    export default {
        name: '',
        data() {
            return {
                charts: '',
            /*  opinion: ["1", "3", "3", "4", "5"],*/
                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
                    }]
                })
            }
        },
        //調用
        mounted() {
            this.$nextTick(function() {
                this.drawLine('main')
            })
        }
    }
</script>


複製代碼

4:這個時候,能夠看到,加載出的折線圖了,後面能夠繼續進行完善。 成功顯示折線圖 express

5:以上的數據是經過變量構造的 實際項目裏面 須要用到axios請求後端接口 那麼,咱們今天就把接口寫在mock裏面吧npm

首先在mock裏面新建一個echarts.json json

echarts.json文件axios

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

複製代碼

6:在build目錄下找到webpack.dev.conf.js文件,編寫如下代碼後端

// mock code
const express = require('express')
const app = express()
const test = require('../mock/test.json') 
const echarts = require('../mock/echarts.json') 
const routes = express.Router()
app.use('/api', routes)

// 若是是post請求,那麼將get改成post便可
devServer: {
 ...
 before(app){
 app.get('/api/test', (req, res) => {
  res.json(test)
 }),
 
 app.get('/api/echarts', (req, res) => {
  res.json(echarts)
 })
 }
複製代碼

7:瀏覽器輸入http://localhost:8080/api/echarts 成功看到模擬數據

8:echarts.vue裏面的代碼。

一開始寫代碼的時候,賦值成功,數據也能打印在控制檯上,可是不知道爲何就是繪製不出來折線圖,我表示很無奈,因而問了一下張小麗,她讓我把 this.drawLine('main')方法直接放在賦值以後,一開始我是放在 mounted()裏面進行調用的,更換了位置以後,再次打開瀏覽器,折線圖已經繪製在瀏覽器裏面了,哈哈,有大神閨蜜帶飛的感受可真好。

<template>
	<div>
    <!--爲echarts準備一個具有大小的容器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('http://localhost:8080/api/echarts').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
                    }]
                })
            },
                                
        },
        //調用
        mounted() {
        	this.getData();         
        }
    }
</script>
複製代碼

9:再次查看,從json裏面請求過來的數據就這樣展現在界面了。


原文做者:祈澈姑娘

相關文章
相關標籤/搜索