Vue+Echarts简单折线图 Vue+Echarts实现简单折线图

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Vue+Echarts简单折线图 Vue+Echarts实现简单折线图

ZPeng_Yan   2022-03-21 我要评论
想了解Vue+Echarts实现简单折线图的相关内容吗,ZPeng_Yan在本文为您仔细讲解Vue+Echarts简单折线图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Vue,Echarts,折线图,下面大家一起来学习吧。

Vue+Echarts实现一个折线图,打开vue的项目:

1、在项目里面安装echarts

npm install echarts --save

2、在需要用图表的地方引入

import echarts from 'echarts'

3、打开my.vue

继续写代码,代码如下:

<template>
    <!--为echarts准备一个具备大小的容器dom-->
    <div id="main" style="width: 600px;height: 400px;"></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>
<style scoped>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
</style>

这个时候,可以看到,加载出的折线图了,后面可以继续进行完善。

这是最基本的折线图,里面的折线点需要替换的话,要注意一些事情

如下代码 所示

<template>
    <!--为echarts准备一个具备大小的容器dom-->
    <div id="main" style="width: 600px;height: 400px;"></div>
</template>
<script>
    import {getorder} from '../api/api.js'
    import echarts from 'echarts'
    export default {
        name: '',
        data() {
            return {
                charts: '',
                /*  opinion: ["1", "3", "3", "4", "5"],*/
                
                //opinionData: ["3", "2", "4", "4", "5"]
                opinionData: [],
                temp:[],
                id:1,
            }
        },
        methods: {
            drawLine(id) {
                // 前端向后端发送请求,获取数据(折线点)
                // 发送请求 要写在drawLine方法里面,不然的话 opinionData 赋予不上数据,做无用功
                // params 里面的是 要向后端传递的一些参数,为了获取数据库中的数据,要替换成你自己的参数
                let params = {typ:9,id:this.id}
                // 这是我个人的 axios 封装,有兴趣的话,可以看我 axios 封装的文章
                getorder(params).then((result)=>{
                this.temp = result.data.tempdic
                // console.log(this.temp)
                // 进行赋值
                for (let i = 0; i < this.temp.length; i++) {
                    var str = ''
                    str += this.temp[i].temp
                    this.opinionData.push(str)
                    // console.log(this.temp[i].temp)
                }
                
                // 折线图 自带的代码
                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: []
                    
                    },
                    yAxis: {
                        type: 'value',
                        
                    },
 
                    series: [{
                        name: '温度展示',
                        type: 'line',
                        stack: '总量',
                        data: this.opinionData
                    }]
                })            
            })    
            }
        },
        //调用
        mounted() {
            this.$nextTick(function() {
                this.drawLine('main')
            })
        }
    }
</script>
<style scoped>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
</style>

这样就能展示出,我们想展示的数据的折线图了!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们