Chart is compatible with Vue.js, although when I am fetching the api data from remote server via Fetch Api, Chart is recieving the data but not displaying it on window.
I have a template for fetching the data with default
<template>
<Bar id="my-chart-id" :options="chartOptions" :data="chartData" />
</template>
<script>
import { Bar } from 'vue-chartjs'
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js'
import myToken from '../server/token'
import apiDataFetch from '../src/function/fetch'
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)
export default {
name: 'BarChart',
components: { Bar },
data() {
return {
items: [],
query: 'year',
type: 'closed_category',
chartData: {
labels: [],
datasets: [{ data: [] }]
},
chartOptions: {
responsive: true
}
}
},
methods: {
async getData() {
const options = {
method: 'GET',
headers: {
Authorization: `Bearer ${myToken}`,
'Content-Language': 'en',
'Content-Type': 'application/json'
}
}
await apiDataFetch(`/admin/statistics/subscription?type=${this.query}`, options)
.then(res => res.json())
.then(res => {
let data = res.data.details
let dataset = this.chartData.datasets[0]
const chData = []
data.forEach(item => {
this.chartData.labels.push(item.dates.start)
this.items.push(item.items)
})
this.items.forEach(item => {
item.forEach(e => {
if (e.type === this.type) {
chData.push(e.count)
}
})
})
dataset.data = [...chData]
console.log(dataset.data)
})
}
},
mounted() {
this.getData()
},
}
</script>
I have seen the chart components in dev tools and here is the data is shown, but not displayed in window.