71 lines
1.4 KiB
JavaScript
71 lines
1.4 KiB
JavaScript
import {isInRange} from '../lib/dateFilter'
|
|
|
|
const getDate = (value) => {
|
|
const d = new Date(value)
|
|
|
|
let month = d.getUTCMonth() + 1
|
|
const year = d.getUTCFullYear()
|
|
|
|
if (month < 10) {
|
|
month = `0${month}`
|
|
}
|
|
|
|
return `${month}/${year}`
|
|
}
|
|
|
|
const compute = (transactions, dateFrom, dateTo) => {
|
|
const indexes = {}
|
|
const values = {}
|
|
const labels = []
|
|
const up = {
|
|
label: 'Argent épargné',
|
|
borderColor: 'rgb(105, 167, 86)',
|
|
backgroundColor: 'rgb(121, 199, 83)',
|
|
fill: true,
|
|
data: [],
|
|
}
|
|
const down = {
|
|
label: 'Argent non épargné',
|
|
borderColor: 'rgb(217, 139, 102)',
|
|
backgroundColor: 'rgb(255, 209, 130)',
|
|
fill: true,
|
|
data: [],
|
|
}
|
|
|
|
transactions.forEach((transaction) => {
|
|
if (!isInRange(transaction.date, dateFrom, dateTo)) {
|
|
return
|
|
}
|
|
|
|
const date = getDate(transaction.date)
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(indexes, date)) {
|
|
indexes[date] = labels.length
|
|
values[date] = 0
|
|
labels.push(date)
|
|
}
|
|
|
|
values[date] += transaction.credit - transaction.debit
|
|
})
|
|
|
|
labels.forEach((label) => {
|
|
const value = values[label]
|
|
|
|
if (value >= 0) {
|
|
up.data.push(value)
|
|
down.data.push(0)
|
|
} else {
|
|
down.data.push(value)
|
|
up.data.push(0)
|
|
}
|
|
})
|
|
|
|
const config = {
|
|
labels: labels,
|
|
datasets: [up, down],
|
|
}
|
|
|
|
return config
|
|
}
|
|
|
|
export {compute}
|