首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在vue中将值检索到echart条形图时出错

在vue中将值检索到echart条形图时出错
EN

Stack Overflow用户
提问于 2019-11-10 17:09:28
回答 1查看 864关注 0票数 1

我正尝试在vue图表中使用我的firestore数据库数据,但它显示错误且未定义

代码语言:javascript
复制
    <vx-card title="PLACEMENT ANALYSIS" class="mb-base" >

        <div class="mt-5">
            <e-charts :options="bar" ref="bar" theme="ovilia-green" auto-resize />
        </div>


    </vx-card>
</template>

<script>
import ECharts from 'vue-echarts/components/ECharts'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/legend'
import 'echarts/lib/chart/bar'
import theme from './theme.json'
import firebase from 'firebase/app'
import 'firebase/auth'
import "firebase/firestore"



ECharts.registerTheme('ovilia-green', theme)

export default {
    data() {
        return {
            arr:[],
            l:50,
            r:30,
            bar: {
                legend: {},
                tooltip: {},
                dataset: {
                    // Provide data.
                    source: [
                        ['Product', 'Eligible Students', 'Placed Students', ],
                        ['B.Tech',  ],
                        ['MBA', this.random2(),this.random3()],
                        ['B.com', this.random2(),this.random3()],
                        ['MSc.', this.random2(),this.random3()],
                        ['Others', this.random2(),this.random3()]
                    ]
                },
                // Declare X axis, which is a category axis, mapping
                // to the first column by default.
                xAxis: { type: 'category' },
                // Declare Y axis, which is a value axis.
                yAxis: {},
                // Declare several series, each of them mapped to a
                // column of the dataset by default.
                series: [{ type: 'bar' }, { type: 'bar' }]

            },
        }
    },
    beforeCreate(){
        let u = firebase.auth().currentUser
        firebase.firestore().collection('Colleges').doc(u.uid).get().then(doc =>{
            this.arr = doc.data()
            console.log(this.arr)

        })

    },

    methods: {
        random2() {


            return[ this.arr.eligible]


        },
        random3(){
            return[this.arr.placed]
        }
    },
    components: {
        ECharts
    },
    computed:{
        chart(){
            console.log(this.arr)
            return this.arr
        }
    }
}
</script>

在这里,合格和放置是arr中的字段,在beforeCreate()中可见,但图表显示合格是未定义的,图表不可见。我尝试在数据返回字段中使用独立变量,例如l和r,但仍然显示未定义。我正在导入不同页面上的echart作为

代码语言:javascript
复制
<echarts-bar-chart></echarts-bar-chart>
<script>
import EchartsBarChart from '../charts-and-maps/charts/echarts/EchartsBarChart.vue'
                <echarts-bar-chart></echarts-bar-chart>
Components:{
EchartsBarChart
}
</script>
EN

回答 1

Stack Overflow用户

发布于 2019-11-10 17:19:24

arr是一个数组,而您正在尝试使用数组上不存在的属性arr.eligible。是否需要项目属性,如arr[0].eligible

您可能还想确认异步回调中的this确实是您的组件。有时需要一个闭包来捕获它。

您需要确保this引用您的组件。试着在一个闭包中捕获它。通常情况下,lambda可以工作,但有时也会出现问题。

代码语言:javascript
复制
const that = this;

firebase.firestore().collection('Colleges').doc(u.uid).get().then(doc =>{
            that.arr = doc.data()
            console.log(that.arr)
            console.log(that)

        })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58787173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档