我已经尝试了一段时间来显示带有来自axios API的数据的图形。 API上的axios获取效果很好,但是数据未显示在图形上。但是,我按照本教程进行操作,并在chart.js上加载了v-if =。
有人可以帮我,因为我真的不懂吗?
谢谢
这是我的代码:
`<template>
<div class="charts">
<div class="row">
<div class="flex md6 xs12">
<va-card
v-if ="loaded"
class="chart-widget"
:title="$t('charts.verticalBarChart')"
>
<va-chart :data="verticalBarChartData" type="vertical-bar"/>
</va-card>
</div>
<div class="flex md6 xs12">
<va-card
class="chart-widget"
:title="$t('charts.horizontalBarChart')"
>
<va-chart :data="horizontalBarChartData" type="horizontal-bar"/>
</va-card>
</div>
</div>
<div class="row">
<div class="flex md12 xs12">
<va-card
class="chart-widget"
:title="$t('charts.lineChart')"
>
<va-chart :data="lineChartData" type="line"/>
</va-card>
</div>
</div>
<div class="row">
<div class="flex md6 xs12">
<va-card
class="chart-widget"
:title="$t('charts.pieChart')"
>
<va-chart :data="pieChartData" type="pie"/>
</va-card>
</div>
<div class="flex md6 xs12">
<va-card
class="chart-widget"
:title="$t('charts.donutChart')"
>
<va-chart :data="donutChartData" type="donut"/>
</va-card>
</div>
</div>
<div class="row">
<div class="flex md12 xs12">
<va-card
class="chart-widget"
:title="$t('charts.bubbleChart')"
>
<va-chart :data="bubbleChartData" type="bubble"/>
</va-card>
</div>
</div>
</div>
</template>
```
<script>
var dataget
import { getLineChartData } from '../../../data/charts/LineChartData'
import { getBubbleChartData } from '../../../data/charts/BubbleChartData'
import { getPieChartData } from '../../../data/charts/PieChartData'
import { getDonutChartData } from '../../../data/charts/DonutChartData'
// import { getVerticalBarChartData } from '../../../data/charts/VerticalBarChartData'
import { getHorizontalBarChartData } from '../../../data/charts/HorizontalBarChartData'
import axios from 'axios'
```
```
export default {
name: 'charts',
data () {
return {
loaded : false,
bubbleChartData: getBubbleChartData(this.$themes),
lineChartData: getLineChartData(this.$themes),
pieChartData: getPieChartData(this.$themes),
donutChartData: getDonutChartData(this.$themes),
verticalBarChartData: getVerticalBarChartData(this.$themes),
horizontalBarChartData: getHorizontalBarChartData(this.$themes),
}
},
methods: {
refreshData () {
this.lineChartData = getLineChartData(this.$themes)
},
},
// Ajout
async mounted () {
this.loaded = false
try {
axios.get('http://localhost:8080/reading/_size?filter={"device":"Random-Integer-Device"}', {
auth: {
username: 'admin',
password: 'secret'
}
})
.then(function (response) {
//console.log(response.data._size);
dataget = response.data._size;
console.log("SIZE : "+ dataget)
})
this.loaded = true
} catch (e) {
console.error(e)
}
},
// Ajout
}
// this is a test
const getVerticalBarChartData = (themes) => ({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [
{
label: 'USA',
backgroundColor: themes.primary,
borderColor: 'transparent',
data: null,
},
{
label: 'USSR',
backgroundColor: themes.info,
borderColor: 'transparent',
// It doesn't work here for dataget value
data: [dataget, 10, 22, 39, 15, 20, 85, 32, 60, 50, 20, 30],
},
],
})
// This is a test
</script>
<style lang="scss">
.chart-widget {
.va-card__body {
height: 550px;
}
}
</style>
```
问题是,axios.get
发出异步HTTP请求,并仅在收到响应后才为变量dataget
分配值。但是,当变量dataget
仍未定义时,将创建图表。
在下面的代码行之后移动位于两个注释之间的整个代码块//这是一个测试,它应该可以工作。
dataget = response.data._size;