vue中使用echars,渲染不上后端请求的数据

16 篇文章 0 订阅
11 篇文章 0 订阅

最近用vue和echars做了两个小页面,遇到一个问题是,在mounted发送请求,和初始化echars图,发现数据出不来,现在记录一下解决方法。

解决:在数据请求的时候,生命周期是不会等待你完成请求在继续走mounted,所有你在mounted初始化echarts的时候,请求可能没完成,所以说拿不到数据显示,你可以直接再mounted请求数据,在请求成功回调里面进行echarts初始化

this.$post('/adminOutAccess/',this.department,this.months).then(response=>{
                console.log(response.name)
                console.log(response.coverage)
                console.log(response.duration)
                this.staff = response.name
                this.coverage = response.coverage
                this.duration = response.duration
                this.getData()
            })
getData(){
                const that = this
                const option = {
                    title:{
                        text:this.department+" "+this.month+" 月走访信息"
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        }
                    },
                    toolbox: {
                        feature: {
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    legend: {
                        data: ['走访时长', '走访覆盖率']
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: that.staff,
                            axisPointer: {
                                type: 'shadow'
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '走访时长',
                            min: 0,
                            max: 100,
                            interval: 10,
                            axisLabel: {
                                formatter: '{value} h'
                            }
                        },
                        {
                            type: 'value',
                            name: '走访覆盖率',
                            min: 0,
                            max: 100,
                            interval: 10,
                            axisLabel: {
                                formatter: '{value} %'
                            }
                        }
                    ],
                    series: [
                        {
                            name: '走访时长',
                            type: 'bar',
                            data: [41.16, 42.20, 9.03, 5.72, 59.63, 31.62, 25.04, 21.83, 49.45, 30.18, 13.59, 23.32, 79.81, 93.15, 38.19, 50.25, 55.52, 0.00, 46.25,
                            ]
                            // data: this.duration
                        },
                        {
                            name: '走访覆盖率',
                            type: 'line',
                            yAxisIndex: 1,
                            // data: this.coverage,
                            data: [100.00,
                            100.00,
                            100.00,
                            38.89,
                            98.63,
                            98.33,
                            67.42,
                            65.26,
                            100.00,
                            80.77,
                            100.00,
                            100.00,
                            99.16,
                            87.39,
                            84.81,
                            100.00,
                            100.00,
                            0.00,
                            100.00,
                            ]
                        }
                    ]
                };
                this.chart1 = echarts.init(document.getElementById('chart1'))
                this.chart1.setOption(option)
                // this.chart1.showLoading();
            }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Vue 使用 ECharts,需要先安装 ECharts 和 vue-echarts。 1. 安装 ECharts 和 vue-echarts ``` npm install echarts --save-dev npm install vue-echarts --save-dev ``` 2. 引入 ECharts 和 vue-echarts 在 main.js 引入 ECharts 和 vue-echarts: ``` import Vue from 'vue' import ECharts from 'echarts' import EChartsVue from 'vue-echarts' Vue.component('v-chart', EChartsVue.component) Vue.prototype.$echarts = ECharts ``` 3. 在 Vue 组件使用 vue-echarts 在组件模板引入 vue-echarts,并传入数据和配置: ``` <template> <div> <v-chart :options="chartOptions"></v-chart> </div> </template> <script> export default { data() { return { chartOptions: { title: { text: 'ECharts 入门示例' }, tooltip: {}, xAxis: { data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20, 15] }] } } } } </script> ``` 4. 获取后端数据 使用 Vue 的生命周期钩子函数 mounted,在组件挂载后获取后端数据,然后将数据传给 ECharts 进行渲染: ``` <template> <div> <v-chart ref="myChart" :options="chartOptions"></v-chart> </div> </template> <script> import axios from 'axios' export default { data() { return { chartOptions: { title: { text: 'ECharts 示例' }, tooltip: {}, xAxis: { data: [] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [] }] } } }, mounted() { axios.get('/api/data').then(response => { const data = response.data const xAxisData = [] const seriesData = [] data.forEach(item => { xAxisData.push(item.name) seriesData.push(item.value) }) this.chartOptions.xAxis.data = xAxisData this.chartOptions.series[0].data = seriesData this.$refs.myChart.setOption(this.chartOptions) }) } } </script> ``` 以上代码使用 axios 发送 GET 请求获取后端数据,然后将数据处理成 ECharts 需要的格式,最后在组件挂载完成后调用 setOption 方法渲染图表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值