refactor line-chart

This commit is contained in:
Jay Makwana
2020-04-17 19:53:08 +05:30
parent 25114009e3
commit 7ca725ac37

View File

@ -1,8 +1,6 @@
<template> <template>
<div class="graph-container"> <div class="graph-container">
<canvas <canvas id="graph" ref="graph" />
id="graph"
ref="graph" />
</div> </div>
</template> </template>
@ -16,58 +14,56 @@ export default {
labels: { labels: {
type: Array, type: Array,
require: true, require: true,
default: Array default: Array,
}, },
values: { values: {
type: Array, type: Array,
require: true, require: true,
default: Array default: Array,
}, },
invoices: { invoices: {
type: Array, type: Array,
require: true, require: true,
default: Array default: Array,
}, },
expenses: { expenses: {
type: Array, type: Array,
require: true, require: true,
default: Array default: Array,
}, },
receipts: { receipts: {
type: Array, type: Array,
require: true, require: true,
default: Array default: Array,
}, },
income: { income: {
type: Array, type: Array,
require: true, require: true,
default: Array default: Array,
}, },
formatMoney: { formatMoney: {
type: Function, type: Function,
require: false, require: false,
default: Function default: Function,
}, },
FormatGraphMoney: { FormatGraphMoney: {
type: Function, type: Function,
require: false, require: false,
default: Function default: Function,
} },
}, },
computed: { computed: {
...mapGetters('currency', [ ...mapGetters('currency', ['defaultCurrency']),
'defaultCurrency'
])
}, },
watch: { watch: {
labels (val) { labels(val) {
this.update() this.update()
} },
}, },
mounted () { mounted() {
let self = this let self = this
let context = this.$refs.graph.getContext('2d') let context = this.$refs.graph.getContext('2d')
let options = { let options = {
@ -77,13 +73,16 @@ export default {
enabled: true, enabled: true,
callbacks: { callbacks: {
label: function (tooltipItem, data) { label: function (tooltipItem, data) {
return self.FormatGraphMoney(tooltipItem.value, self.defaultCurrency) return self.FormatGraphMoney(
} tooltipItem.value * 100,
} self.defaultCurrency
)
},
},
}, },
legend: { legend: {
display: false display: false,
} },
} }
let data = { let data = {
labels: this.labels, labels: this.labels,
@ -107,7 +106,7 @@ export default {
pointHoverBorderWidth: 2, pointHoverBorderWidth: 2,
pointRadius: 4, pointRadius: 4,
pointHitRadius: 10, pointHitRadius: 10,
data: this.invoices data: this.invoices.map((invoice) => invoice / 100),
}, },
{ {
label: 'Receipts', label: 'Receipts',
@ -128,7 +127,7 @@ export default {
pointHoverBorderWidth: 2, pointHoverBorderWidth: 2,
pointRadius: 4, pointRadius: 4,
pointHitRadius: 10, pointHitRadius: 10,
data: this.receipts data: this.receipts.map((receipt) => receipt / 100),
}, },
{ {
label: 'Expenses', label: 'Expenses',
@ -149,7 +148,7 @@ export default {
pointHoverBorderWidth: 2, pointHoverBorderWidth: 2,
pointRadius: 4, pointRadius: 4,
pointHitRadius: 10, pointHitRadius: 10,
data: this.expenses data: this.expenses.map((expense) => expense / 100),
}, },
{ {
label: 'Net Income', label: 'Net Income',
@ -170,34 +169,40 @@ export default {
pointHoverBorderWidth: 2, pointHoverBorderWidth: 2,
pointRadius: 4, pointRadius: 4,
pointHitRadius: 10, pointHitRadius: 10,
data: this.income data: this.income.map((_i) => _i / 100),
} },
] ],
} }
this.myLineChart = new Chart(context, { this.myLineChart = new Chart(context, {
type: 'line', type: 'line',
data: data, data: data,
options: options options: options,
}) })
}, },
methods: { methods: {
update () { update() {
this.myLineChart.data.labels = this.labels this.myLineChart.data.labels = this.labels
this.myLineChart.data.datasets[0].data = this.invoices this.myLineChart.data.datasets[0].data = this.invoices.map(
this.myLineChart.data.datasets[1].data = this.receipts (invoice) => invoice / 100
this.myLineChart.data.datasets[2].data = this.expenses )
this.myLineChart.data.datasets[3].data = this.income this.myLineChart.data.datasets[1].data = this.receipts.map(
(receipt) => receipt / 100
)
this.myLineChart.data.datasets[2].data = this.expenses.map(
(expense) => expense / 100
)
this.myLineChart.data.datasets[3].data = this.income.map((_i) => _i / 100)
this.myLineChart.update({ this.myLineChart.update({
lazy: true lazy: true,
}) })
}, },
beforeDestroy () { beforeDestroy() {
this.myLineChart.destroy() this.myLineChart.destroy()
} },
} },
} }
</script> </script>