账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    mpvue使用echarts 第二次进入页面 图表不显示
    46
    0

    展示一个图表多个地方需要使用,做成了一个组件,第一次进入页面,图表展示正常,返回之后再次进入页面,图表就不在显示。但是把下面的代码做成一个页面就不会存在这样的问题。本人是新手,感觉可能是哪里渲染出现了问题,求大佬解答一下。
    这是组件的代码。

    <template>
        <div class="echarts-wrap">
            <mpvue-echarts lazyLoad :echarts="echarts" :onInit="HandleInit" ref="echarts" />
        </div>
    </template>
    
    <script>
        import echarts from 'echarts'
        import mpvueEcharts from 'mpvue-echarts'
        import {
            man,
            woman
        } from '../utils/data.js'
        let chart = null;
        export default {
            components: {
                mpvueEcharts
            },
            data() {
                return {
                    childrenSex: null,
                    childrenAge: null,
                    echarts,
                    option: null,
                    normalHight: [123, 134, 146, 157, 167, 189, 190, 214, 223, 233, 244, 255, 266, 277, 288],
                    perfectHight: [211, 222, 233, 244, 255, 266, 277, 288, 299, 311, 322, 333, 344, 355, 366],
                    lowHight: [211, 222, 233, 244, 255, 266, 277, 288, 299, 311, 322, 333, 344, 355, 366],
                    youHight: [211, 222, 233, 244, 255, 266, 277, 288, 299, 311, 322, 333, 344, 355, 366]
                }
            },
            methods: {
                InitChart() {
                    let timeList = []
                    let yearnow = new Date().getFullYear()
                    let monthnow = new Date().getMonth() + 1
                    for (let i = monthnow; i <= 12; i++) {
                        timeList.push(`${yearnow}年/${i}月`)
                        if (i == 12) {
                            console.log(123);
                            for (let z = 0; z <= 13 - monthnow; z++) {
                                timeList.push(`${yearnow+1}年/${z+1}月`)
                            }
                        }
                    }
                    /**
                     * @msg: 修改列表数据,根据年龄和性别
                     * @param {type} 
                     * @return: 
                     */
                    if (this.childrenSex == 2) {
                        this.normalHight = [111, 111, 111, 111, 111, 111]
                    } else {
                        this.normalHight = [333, 333, 333, 333, 333, 333]
                    }
        
        
        
                    this.option = {
                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            itemWidth: 10,
                            itemHeight: 7,
                            data: ['正常身高', '完美身高', '矮小身高', '你的身高'],
                            itemGap: 5
                        },
                        dataZoom: [{
                                show: true,
                                realtime: true,
                                handleSize: 0,
                                start: 0,
                                end: 50
                            },
                            {
                                type: 'inside',
                                realtime: true,
                                start: 0,
                                end: 50
                            }
                        ],
                        calculable: true,
                        xAxis: [{
                            type: 'category',
                            boundaryGap: false,
                            data: timeList
                        }],
                        yAxis: [{
                            type: 'value',
                            max: 600,
                            min: 0
                        }],
                        series: [{
                                name: '正常身高',
                                type: 'line',
                                stack: '总量',
                                data: this.normalHight
                            },
                            {
                                name: '完美身高',
                                type: 'line',
                                stack: '总量',
                                data: this.perfectHight
                            },
                            {
                                name: '矮小身高',
                                type: 'line',
                                stack: '总量',
                                data: this.lowHight
                            },
                            {
                                name: '你的身高',
                                type: 'line',
                                stack: '总量',
                                data: this.youHight
                            }
                        ]
                    };
                    this.$refs.echarts.init()
                },
                HandleInit(canvas, width, height) {
                    chart = echarts.init(canvas, null, {
                        width: width,
                        height: height
                    })
                    canvas.setChart(chart)
                    chart.setOption(this.option, true)
                    return chart
                },
            },
            mounted(x) {
                // 不能使用created和onLoad,必须是在模版加载完成之后,才能初始化图表。
                this.InitChart()
            },
            onLoad(x) {
                this.childrenSex = x.sex
                this.childrenAge = x.age
            }
        }
    </script>
    
    <style scoped>
        .echarts-wrap {
            width: 100%;
            height: 300px;
        }
    </style>
    

    感谢各位

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 向前冲 普通会员 1楼

      在MPVue中使用ECharts,当再次进入页面时,图表不会自动显示可能有以下几种原因:

      1. 组件挂载顺序问题:在使用MPVue时,组件会被编译并打包成spa(Single Page App)应用。当用户第一次访问应用时,ECharts会被浏览器缓存或禁止,导致组件未正确加载。解决方案是确保在组件被调用或使用时,将其组件注册到Vue应用的生命周期钩子中,并在组件挂载后调用mounted钩子,以便ECharts能够正确加载。

      ```javascript // 首先在Vue的main.js文件中注册ECharts组件 import Vue from 'vue'; import App from './App.vue'; import 'echarts/lib/theme/default';

      Vue.config.productionTip = false; Vue.use(ECharts);

      new Vue({ el: '#app', components: { App, }, mounted() { // 1. 将ECharts引入到应用中 const echarts = new echarts.ECharts(); // 2. 按照正常顺序注册ECharts组件 echarts.registerElement('option', echarts.ElementOption); // 3. 在组件挂载时调用 mounted钩子 this.$mount(); }, }); ```

      1. ECharts版本不兼容:在使用ECharts时,确保你正在使用的是最新版本的ECharts。有时候,旧版本的ECharts可能无法与新版本的MPVue集成,导致图表无法正确显示。如果遇到此问题,你可以尝试更新ECharts到最新版本,或者在Vue组件中使用echarts.init方法初始化ECharts实例。

      ``javascript // 在Vue的main.js文件中,使用echarts.init`方法初始化ECharts实例 import Vue from 'vue'; import App from './App.vue'; import 'echarts/lib/theme/default';

      Vue.config.productionTip = false; Vue.use(ECharts);

      new Vue({ el: '#app', components: { App, }, mounted() { // 1. 将ECharts引入到应用中 const echarts = new echarts.ECharts(); // 2. 使用echarts.init方法初始化ECharts实例 echarts.init(); }, }); ```

      1. ECharts初始化问题:有时候,初始化ECharts可能会出现问题,例如图表未创建、数据加载不完整或显示异常等问题。请确保在使用ECharts时,遵循其官方文档提供的初始化方法,例如:

      javascript // 在ECharts的初始化方法中 new echarts.ECharts({ // 选择图表类型 option: { xAxis: { type: 'category', data: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], }, yAxis: { type: 'value', }, series: [ { name: 'Sales', type: 'line', data: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110], }, ], }, });

      如果以上方法都无法解决问题,你可能需要检查ECharts的依赖库、Vue实例、以及MPVue是否正确配置了数据和选项。例如,确保你的ECharts组件依赖于MPVue提供的<MPVue>标签,以及MPVue使用了ECharts作为图表库的<MPVueChart>元素。如果MPVue的图表配置文件(如Chart.vue)中有以下部分:

      ```html

      ```

      则需要在MPVue组件中调用MPVueChart.init方法进行初始化,如下所示:

      ```html

      ```

      请根据具体的应用情况,查找并解决上述问题。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部