I successfully created bar charts using vue2 and vue-chart.js with data from an API call. However, I am facing issues with displaying line charts. Despite loading the data, the line chart is not showing up, and no errors are being displayed. I have reviewed my code but cannot identify the error. Any suggestions? Thanks!
https://i.sstatic.net/OqmZt.png
Versions:
"vue": "^2.6.11"
"vue-chartjs": "^4.1.1"
my base-vue:
<template>
<div id="app" class="content">
<div class="dist_30"></div>
<h3>Charts</h3>
<div class="dist_10"></div>
<div class="row ">
<div class="col-12">
<div style="margin: 10px 10px 10px 10px;">
<BarChart />
</div>
</div>
</div>
<div class="row ">
<div class="col-12">
<div style="margin: 10px 10px 10px 10px;">
<LineChart />
</div>
</div>
</div>
</div>
</template>
<script>
import BarChart from '@/components/BarChart'
import LineChart from '@/components/LineChart'
export default {
name: 'App',
components: { BarChart , LineChart}
}
</script>
my LineChart.vue:
<template>
<Line v-if="loaded"
:chart-options="chartOptions"
:chart-data="chartData"
:chart-id="chartId"
:dataset-id-key="datasetIdKey"
:plugins="plugins"
:css-classes="cssClasses"
:styles="myStyles"
:width="300"
:height="height"
/>
</template>
<script>
import { Line } from 'vue-chartjs/legacy'
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, LineElement, CategoryScale, LinearScale } from 'chart.js';
ChartJS.register(Title, Tooltip, Legend, BarElement, LineElement, CategoryScale, LinearScale);
const chartAPI = 'https://m.myapp2go.de/services/getstatistik_tables.php';
export default {
name: 'LineChart',
components: { Line },
async mounted () {
this.loaded = false
try {
const response = await fetch(chartAPI);
const chartArray = await response.json();
this.chartdata = chartArray.items;
// Get all the numbers/labels from the response
this.chartData.labels = this.chartdata.map(i => i.table).slice(1);
this.chartData.datasets[0].data = this.chartdata.map(i => i.count).slice(1);
console.log("chart Data ", JSON.stringify(this.chartdata));
console.log("chart Data numbers: ", this.newChartArray);
this.loaded = true
} catch (e) {
console.error(e)
}
},
computed: {
myStyles () {
return {
position: 'relative'
}
}
},
props: {
chartId: {
type: String,
default: 'line-chart'
},
datasetIdKey: {
type: String,
default: 'label'
},
width: {
type: Number,
default: 200
},
height: {
type: Number,
default: 0
},
cssClasses: {
default: '',
type: String
},
styles: {
type: Object,
default: () => {}
},
plugins: {
type: Array,
default: () => []
}
},
data() {
return {
chartData: {
labels: [ 'num', 'call', 'blog', 'key', 'mod,' ,'pic', 'acc', 'ifc', 'req', 'inf' ],
datasets: [ {
label: 'Database statistics',
borderWidth: 1,
data: [ ]} ]
},
chartOptions: {
responsive: true,
maintainAspectRatio: false
},
newChartArray: [],
loaded: false
}
}
}
</script>
output from chrome-dev-tools:
<Line chart-options="[object Object]" chart-data="[object Object]" chart-id="line-chart" dataset-id-key="label" plugins="" css-classes="" styles="[object Object]" width="300" height="0"></Line>