forked from akhilrex/hammond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmileageChart.vue
54 lines (53 loc) · 1.36 KB
/
mileageChart.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<script>
import { Line } from 'vue-chartjs'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
extends: Line,
props: { vehicle: { type: Object, required: true }, since: { type: Date, default: '' }, user: { type: Object, required: true } },
computed: {
...mapState('utils', ['isMobile']),
},
watch: {
since(newOne, old) {
if (newOne === old) {
return
}
this.fetchMileage()
},
},
data: function() {
return {
chartData: [],
}
},
mounted() {
this.fetchMileage()
},
methods: {
showChart() {
var labels = this.chartData.map((x) => x.date.substr(0, 10))
var dataset = {
steppedLine: true,
label: `${this.$t('odometer')} (${this.$t('unit.short.' + this.user.distanceUnitDetail.key)}/${this.$t('unit.short.' + this.vehicle.fuelUnitDetail.key)})`,
fill: true,
data: this.chartData.map((x) => x.mileage),
}
this.renderChart({ labels, datasets: [dataset] }, { maintainAspectRatio: false })
},
fetchMileage() {
axios
.get(`/api/vehicles/${this.vehicle.id}/mileage`, {
params: {
since: this.since,
},
})
.then((response) => {
this.chartData = response.data
this.showChart()
})
.catch((err) => console.log(err))
},
},
}
</script>