vue动态统计图的绘画

效果图:

实现:

一、导入依赖

 import echarts from 'echarts'

二、vue的代码实现

1.在main.js导入文件

// 引入 echarts 插件
import echarts from 'echarts'
// 配置成全局组件
Vue.prototype.$echarts = echarts
2.代码实现
<template>
    <!--为echarts准备一个具备大小的容器dom-->
    <div id="main" style="width: 50%;height: 100%">运营统计</div>
</template>
<script>
    export default {
        name: 'zhexiantu',
        data() {
            return {
                charts: '',
                //数据1
                opinionData1: [],
                //x轴
                age:[0,10,24,40,60,80,100],
                
            }
        },
        created() {
            //页面加载进行获取数据
            this.getUserData();
            
        },
        methods: {
            getUserData(){
                this.$axios.post(
                    //请求路径
                    "/patient/selectAgeCount",
                    {
                        ages:this.age 
                    }
                ).then((res) => {
                    //赋值给y轴
                    this.opinionData1 = res.data;
                    //调用函数
                    this.$nextTick(function() {
                        //画div为main的数据
                        this.drawLine('main')
                    })
                }).catch(function(error) {
                    console.log(error);
                });

            },
            drawLine(id) {
                //初始化数据
                this.charts = this.$echarts.init(document.getElementById(id))
                this.charts.setOption({
                    title: {
                        text: '用户注册年龄统计图'
                    },
                    //触发类型;轴触发,axis则鼠标hover到一条柱状图显示全部数据,item则鼠标hover到折线点显示相应数据
                    tooltip: {
                        trigger: 'axis'
                    },
                    //所选项
                    legend: {
                        data: ['用户']
                    },
                    //布局样式
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    //下载图片按钮
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    //x轴
                    xAxis: {
                        name: '(年龄)',
                        type: 'category',
                        boundaryGap: false,
                        data: this.age

                    },
                    //y轴
                    yAxis: {
                        name: '人数',
                        type: 'value',
                    },
                    series: [
                        {
                            name: '用户',
                            type: 'line',
                            stack: '总人数',
                            data: this.opinionData1
                        }
                    ]
                })
            }
        },

    }
</script>
<style scoped>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
</style>

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值