I have a JavaScript file which contains the data of a chart, I want that chart to be displayed in a Vue component, but how can I do that.
Javascript file named as LineChart.js
new Vue({
data: () => ({
date: [
1600934100.0,
1602009600.0,
1602747060.0,
1603050158.390939,
1603305573.992575
],
challenge: [
9.0,
9.5,
2.5,
11.52,
12.4
]
}),
mounted() {
let data = this.date.map((date, index) => ({
x: new Date(date * 1000),
y: this.challenge[index]
}))
let ctx = this.$refs.chart.getContext('2d')
let chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
data
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'month',
displayFormats: {
month: 'MMM YYYY'
}
}
}],
yAxes: [{
ticks: {
callback: function(value, index, values) {
return value + 'k';
}
}
}]
}
}
})
}
})
And my Vue component named as Test.vue
<template>
<div class="To display chart">
<vx-card>
<div class="chart-container">
<LineChart></LineChart> <-- I want my chart here
</div>
</vx-card>
</div>
</template>
<script>
import LineChart from './LineChart.js'
export default {
name: 'Test'
}
</script>
I have two questions:
Nothing is displayed in my case, so I want to know how Javascript can be imported to Vue component in order to display the chart.
In my JavaScript file, I want to use API instead of data & challenge directly and the date from API should be converted just like the logic I have used, I just want to know how API can be used and the flow of it and conversion of timestamps of API s resource.