Files
firefly-iii/resources/assets/v2/pages/dashboard/budgets.js

192 lines
6.5 KiB
JavaScript
Raw Normal View History

2023-08-04 19:10:49 +02:00
/*
* budgets.js
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {getVariable} from "../../store/get-variable.js";
import Dashboard from "../../api/v2/chart/budget/dashboard.js";
2023-08-07 19:05:23 +02:00
import {getDefaultChartSettings} from "../../support/default-chart-settings.js";
2023-08-08 14:11:04 +02:00
import formatMoney from "../../util/format-money.js";
2023-08-12 07:53:11 +02:00
import {Chart} from 'chart.js';
import {I18n} from "i18n-js";
2023-08-08 14:11:04 +02:00
let currencies = [];
let chart = null;
let chartData = null;
2023-08-12 07:53:11 +02:00
let afterPromises = false;
let language;
let i18n; // for translating items in the chart.
async function loadTranslations(i18n, locale) {
const response = await fetch(`./v2/i18n/${locale}.json`);
const translations = await response.json();
i18n.store(translations);
}
2023-08-04 19:10:49 +02:00
export default () => ({
2023-08-06 18:33:29 +02:00
loading: false,
2023-08-04 19:10:49 +02:00
autoConversion: false,
loadChart() {
2023-08-06 18:33:29 +02:00
if (true === this.loading) {
2023-08-04 19:10:49 +02:00
return;
}
2023-08-06 18:33:29 +02:00
this.loading = true;
2023-08-08 14:11:04 +02:00
if (null !== chartData) {
this.drawChart(this.generateOptions(chartData));
2023-08-07 19:05:23 +02:00
this.loading = false;
2023-08-08 14:11:04 +02:00
return;
2023-08-06 18:33:29 +02:00
}
2023-08-08 14:11:04 +02:00
this.getFreshData();
2023-08-06 18:33:29 +02:00
},
2023-08-08 14:11:04 +02:00
drawChart(options) {
if (null !== chart) {
chart.data.datasets = options.data.datasets;
chart.update();
return;
2023-08-06 18:33:29 +02:00
}
2023-08-08 14:11:04 +02:00
chart = new Chart(document.querySelector("#budget-chart"), options);
2023-08-06 18:33:29 +02:00
},
getFreshData() {
2023-08-04 19:10:49 +02:00
const dashboard = new Dashboard();
dashboard.dashboard(new Date(window.store.get('start')), new Date(window.store.get('end')), null).then((response) => {
2023-08-08 14:11:04 +02:00
chartData = response.data; // save chart data for later.
this.drawChart(this.generateOptions(response.data));
2023-08-07 19:05:23 +02:00
this.loading = false;
2023-08-06 18:33:29 +02:00
});
},
generateOptions(data) {
2023-08-08 14:11:04 +02:00
currencies = [];
2023-08-07 19:05:23 +02:00
let options = getDefaultChartSettings('column');
options.options.locale = window.store.get('locale').replace('_', '-');
options.options.plugins = {
tooltip: {
callbacks: {
title: function (context) {
return context.label;
},
label: function (context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
2023-08-06 07:02:55 +02:00
}
2023-08-08 14:11:04 +02:00
return label + ' ' + formatMoney(context.parsed.y, currencies[context.parsed.x] ?? 'EUR');
2023-08-06 18:33:29 +02:00
}
2023-08-04 19:10:49 +02:00
}
2023-08-06 18:33:29 +02:00
}
};
2023-08-04 19:10:49 +02:00
2023-08-07 19:05:23 +02:00
options.data = {
labels: [],
datasets: [
{
label: 'TODO spent',
data: [],
borderWidth: 1,
stack: 1
},
{
label: 'TODO left',
data: [],
borderWidth: 1,
stack: 1
},
{
label: 'TODO overspent',
data: [],
borderWidth: 1,
stack: 1
}
]
};
2023-08-06 18:33:29 +02:00
for (const i in data) {
if (data.hasOwnProperty(i)) {
let current = data[i];
2023-08-07 19:05:23 +02:00
// // convert to EUR yes no?
2023-08-06 18:33:29 +02:00
let label = current.label + ' (' + current.currency_code + ')';
2023-08-07 19:05:23 +02:00
options.data.labels.push(label);
2023-08-06 18:33:29 +02:00
if (this.autoConversion) {
2023-08-08 14:11:04 +02:00
currencies.push(current.native_code);
2023-08-06 18:33:29 +02:00
// series 0: spent
2023-08-07 19:05:23 +02:00
options.data.datasets[0].data.push(parseFloat(current.native_entries.spent) * -1);
2023-08-06 18:33:29 +02:00
// series 1: left
2023-08-07 19:05:23 +02:00
options.data.datasets[1].data.push(parseFloat(current.native_entries.left));
2023-08-06 18:33:29 +02:00
// series 2: overspent
2023-08-07 19:05:23 +02:00
options.data.datasets[2].data.push(parseFloat(current.native_entries.overspent));
2023-08-06 18:33:29 +02:00
}
if (!this.autoConversion) {
2023-08-08 14:11:04 +02:00
currencies.push(current.currency_code);
2023-08-06 18:33:29 +02:00
// series 0: spent
2023-08-07 19:05:23 +02:00
options.data.datasets[0].data.push(parseFloat(current.entries.spent) * -1);
2023-08-06 18:33:29 +02:00
// series 1: left
2023-08-07 19:05:23 +02:00
options.data.datasets[1].data.push(parseFloat(current.entries.left));
2023-08-06 18:33:29 +02:00
// series 2: overspent
2023-08-07 19:05:23 +02:00
options.data.datasets[2].data.push(parseFloat(current.entries.overspent));
2023-08-04 19:10:49 +02:00
}
}
2023-08-06 18:33:29 +02:00
}
2023-08-08 14:11:04 +02:00
return options;
2023-08-06 18:33:29 +02:00
},
2023-08-04 19:10:49 +02:00
init() {
2023-08-12 07:53:11 +02:00
// console.log('budgets init');
Promise.all([getVariable('autoConversion', false), getVariable('language', 'en-US')]).then((values) => {
i18n = new I18n();
loadTranslations(i18n, values[1]);
// load translations.
//i18n = require('../../lang/' + values[1] + '.js').default;
//import lang from '../../lang/' + values[1] + '.js';
//language = values[1];
2023-08-04 19:10:49 +02:00
this.autoConversion = values[0];
2023-08-12 07:53:11 +02:00
afterPromises = true;
2023-08-08 14:11:04 +02:00
if (false === this.loading) {
this.loadChart();
}
2023-08-04 19:10:49 +02:00
});
window.store.observe('end', () => {
2023-08-12 07:53:11 +02:00
if (!afterPromises) {
return;
}
// console.log('boxes observe end');
2023-08-08 14:11:04 +02:00
if (false === this.loading) {
this.chartData = null;
this.loadChart();
}
2023-08-04 19:10:49 +02:00
});
2023-08-06 07:02:55 +02:00
window.store.observe('autoConversion', (newValue) => {
2023-08-12 07:53:11 +02:00
if (!afterPromises) {
return;
}
// console.log('boxes observe autoConversion');
2023-08-06 07:02:55 +02:00
this.autoConversion = newValue;
2023-08-08 14:11:04 +02:00
if (false === this.loading) {
this.loadChart();
}
2023-08-06 07:02:55 +02:00
});
2023-08-04 19:10:49 +02:00
},
});