Skip to content

Firefly III: a personal finances manager

Skip drawing the chart.

James Cole9470935a7222
diff --git a/public/v1/js/ff/index.js b/public/v1/js/ff/index.js
index fbffbaa7e8..7b00a4bb1b 100644
--- a/public/v1/js/ff/index.js
+++ b/public/v1/js/ff/index.js
@@ -34,7 +34,7 @@ function drawChart() {
if (billCount > 0) {
multiCurrencyPieChart('chart/bill/frontpage', 'bills-chart');
}
- stackedColumnChart('chart/budget/frontpage', 'budgets-chart');
+ stackedColumnChart('chart/budget/frontpage', 'budgets-two-chart');
columnChart('chart/category/frontpage', 'categories-chart');
columnChart(accountExpenseUrl, 'expense-accounts-chart');
columnChart(accountRevenueUrl, 'revenue-accounts-chart');
diff --git a/resources/assets/v3/js/pages/dashboard/dashboard.js b/resources/assets/v3/js/pages/dashboard/dashboard.js
index fedf18da91..9389e385d3 100644
--- a/resources/assets/v3/js/pages/dashboard/dashboard.js
+++ b/resources/assets/v3/js/pages/dashboard/dashboard.js
@@ -49,6 +49,16 @@ let index = function () {
value,
true,
);
+
+ // drawMultiCurrencyChart(
+ // "stacked-column",
+ // "api/v1/chart/budget/overview?start=" +
+ // format(start, "yyyy-LL-dd") +
+ // "&end=" +
+ // format(end, "yyyy-LL-dd"),
+ // "budgets-chart",
+ // value, false
+ // );
});
},
loadPiggyBanks() {
diff --git a/resources/assets/v3/js/shared/draw-chart.js b/resources/assets/v3/js/shared/draw-chart.js
index 7fee98bded..5f6bb2efd6 100644
--- a/resources/assets/v3/js/shared/draw-chart.js
+++ b/resources/assets/v3/js/shared/draw-chart.js
@@ -57,76 +57,275 @@ let defaultChartOptions = {
},
};
-export function drawMultiCurrencyChart(
- type,
- url,
- holder,
- anonymous,
- drawTodayMarker,
-) {
+export function drawMultiCurrencyChart(type, url, holder, anonymous, drawTodayMarker,) {
if ("line" === type) {
drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker);
+ return;
}
+ if ("stacked-column" === type) {
+ drawMultiCurrencyStackedColumnChart(url, holder, anonymous);
+ return;
+ }
+
+ console.error('Cannot draw a "' + type + '" chart yet :(');
}
export function drawSingleCurrencyChart(type, url, holder, anonymous) {
if ("line" === type) {
drawSingleCurrencyLineChart(url, holder, anonymous);
+ return;
}
+ console.error('Cannot draw a "' + type + '" chart yet :(');
}
+function drawMultiCurrencyStackedColumnChart(url, holder, anonymous) {
+ document.getElementById(holder).classList.remove("general-chart-error");
+ window.axios
+ .get(url)
+ .then((response) => {
+ // prep some chart variables first.
+ let all = response.data;
+ let axes = {};
+ let data = {
+ datasets: [
+ ],
+ labels: [],
+ };
+
+ // make custom options set.
+ let options = structuredClone(defaultChartOptions);
+ let datasets = {};
+
+ // loop all collected data.
+ for (let i = 0; i < all.length; i++) {
+ if (Object.hasOwn(all, i)) {
+ let current = all[i];
+ // add the name as a label
+ data.labels.push(current.label);
+
+ // there are four possible data sets for this chart
+ // TODO this is very much hard coded.
+ let keys = ['budgeted','spent','left','overspent'];
+ for(let i in keys) {
+ let key = keys[i] + current.currency_code;
+ if (!Object.hasOwn(datasets, key)) {
+ datasets[key] = {
+ label: i18next.t('firefly.' + keys[i]) + ' (' + current.currency_code + ')',
+ currency_code: current.currency_code,
+ data: [],
+ yAxisID: "y" + current.currency_code,
+ }
+ }
+ }
+
+ // for the first and all other datasets, create a new dataset object.
+ // add the data to the dataset.
+
+ // add spent and left to the dataset, if they exist.
+ console.log('current', current);
+ if(parseFloat(current.entries.spent) * -1 < parseFloat(current.entries.budgeted)) {
+ let key = 'budgeted' + current.currency_code;
+ datasets[key].data.push(0);
+
+ // user has not overspent.
+ key = 'spent' + current.currency_code;
+ let value = parseFloat(current.entries.spent) * -1;
+ datasets[key].data.push(value);
+
+ key = 'left' + current.currency_code;
+ value = parseFloat(current.entries.left);
+ datasets[key].data.push(value);
+
+ key = 'overspent' + current.currency_code;
+ datasets[key].data.push(0);
+ }
+ if(parseFloat(current.entries.spent) * -1 >= parseFloat(current.entries.budgeted)) {
+ let key = 'budgeted' + current.currency_code;
+ datasets[key].data.push(0);
+
+ // user has overspent.
+ key = 'spent' + current.currency_code;
+ let value = parseFloat(current.entries.spent) * -1;
+ datasets[key].data.push(value);
+
+ key = 'left' + current.currency_code;
+ datasets[key].data.push(0);
+
+ key = 'overspent' + current.currency_code;
+ value = parseFloat(current.entries.overspent);
+ datasets[key].data.push(value);
+ }
+ // console.log('Current entries:')
+ // console.log(current.entries);
+ //
+ //
+ // for (let j in current.entries) {
+ // // j = "spent" or earned or whatever.
+ // let key = j + current.currency_code;
+ // if (Object.hasOwn(datasets, key)) {
+ // let value = parseFloat(current.entries[j]);
+ // if(value < 0) {
+ // value = value * -1;
+ // }
+ // datasets[key].data.push(value);
+ // }
+ // }
+ console.log('All generatred datasets.')
+ console.log(datasets);
+ // add it to the dataset collection.
+ //data.datasets.push(dataset);
+ data.datasets = Object.values(datasets);
+
+ // if there is no axis yet for this currency, create one.
+ let currencyCode = current.currency_code;
+ let axisId = "y" + currencyCode;
+ if (!Object.hasOwn(axes, axisId)) {
+ axes[axisId] = {
+ id: axisId,
+ type: "linear",
+ stacked: true,
+ position: 0 === Object.keys(axes).length % 2 ? "left" : "right",
+ ticks: {
+ callback: function (value) {
+ if (anonymous) {
+ value = "0";
+ }
+ return formatMoney(value, currencyCode);
+ },
+ },
+ };
+ }
+ }
+ }
+ // remove the standard y-axis, do not need it.
+ delete options.scales.y;
+ options.scales.x.stacked = true;
+ // options.scales.y.stacked = true;
+ // add the new axes.
+ options.scales = {...options.scales, ...axes};
+ console.log(options);
+ console.log(data);
+
+ // add a callback for the label.
+ options.plugins.tooltip.callbacks.label = function (tooltipItem) {
+ "use strict";
+ let index = tooltipItem.dataIndex;
+ let amount = tooltipItem.dataset.data[index];
+
+ let string = formatMoney(
+ amount,
+ tooltipItem.dataset.currency_code,
+ );
+ if (anonymous) {
+ string = formatMoney(
+ "0",
+ tooltipItem.dataset.currency_code,
+ );
+ }
+ return tooltipItem.dataset.label + ": " + string;
+ };
+
+ // safety catch in case there is no data.
+ if (typeof data === "undefined" || 0 === data.length || (typeof data === "object" && typeof data.labels === "object" && 0 === data.labels.length)) {
+ let el = document.getElementById(holder).parentElement;
+ el.innerHTML = "";
+ el.classList.add("general-chart-error");
+ el.innerText = i18next.t("firefly.no_data_for_chart");
+ return;
+ }
+
+ // TODO colorize data?
+ // if (colorData) {
+ // data = colorizeData(data);
+ // }
+
+ // add a marker to the chart if defined.
+
+ // lineChart
+ const ctx = document.getElementById(holder).getContext("2d");
+ new Chart(ctx, {
+ type: "bar",
+ data: data,
+ options: options,
+ });
+ })
+ .catch((error) => {
+ console.error(error);
+ let el = document.getElementById(holder).parentElement;
+ el.innerHTML = "";
+ el.classList.add("general-chart-error");
+ el.innerText =
+ i18next.t("firefly.could_not_load_chart") + " " + error;
+ });
+}
+
+
function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
document.getElementById(holder).classList.remove("general-chart-error");
window.axios
.get(url)
.then((response) => {
+
+ // prep some chart variables first.
let all = response.data;
- let options = { ...defaultChartOptions };
- let today = new Date();
- let firstScale = ""; // used for today marker.
- let drawTodayIndex = "";
+ let axes = {};
let data = {
datasets: [],
labels: [],
};
- let axes = {};
- // collect all Y axes from the data.
+
+ // make custom options set.
+ let options = structuredClone(defaultChartOptions);
+
+ // prep "today" marker.
+ let today = new Date();
+ let firstScale = ""; // used for today marker.
+ let drawTodayLabel = "";
+ let drawTodayIndex = 0;
+ let labelCount = 0;
+
+ // loop all collected data.
for (let i = 0; i < all.length; i++) {
if (Object.hasOwn(all, i)) {
let current = all[i];
- // take the labels from index 0.
+ // first dataset, use the labels from that one
+ // find the place to set the "today" marker, and get FIRST y-axis ID.
+ firstScale = "y" + current.currency_code;
+ labelCount = 0;
if (0 === i) {
for (let j in current.entries) {
if (Object.hasOwn(current.entries, j)) {
+ labelCount++;
+
+ // is the marker to be set on this date?
let date = new Date(j);
if (drawTodayMarker && isSameDay(date, today)) {
- console.log("Today is ", j, date);
- drawTodayIndex = j;
+ drawTodayLabel = j;
+ drawTodayIndex = labelCount;
}
- data.labels.push(
- format(
- date,
- i18next.t("config.month_and_day_fns"),
- ),
- );
+ // add the label to the array
+ data.labels.push(format(date, i18next.t("config.month_and_day_fns"),),);
}
}
- firstScale = "y" + current.currency_code;
}
- // add the dataset.
+
+ // for the first and all other datasets, create a new dataset object.
let dataset = {
label: current.label,
currency_code: current.currency_code,
data: [],
yAxisID: "y" + current.currency_code,
};
+ // add the data to the dataset.
for (let j in current.entries) {
if (Object.hasOwn(current.entries, j)) {
dataset.data.push(current.entries[j]);
}
}
+ // add it to the dataset collection.
data.datasets.push(dataset);
+ // if there is no axis yet for this currency, create one.
let currencyCode = current.currency_code;
let axisId = "y" + currencyCode;
if (!Object.hasOwn(axes, axisId)) {
@@ -134,7 +333,7 @@ function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
id: axisId,
type: "linear",
position:
- Object.keys(axes).length < 1 ? "left" : "right",
+ 0 === Object.keys(axes).length % 2 ? "left" : "right",
ticks: {
callback: function (value) {
if (anonymous) {
@@ -144,26 +343,17 @@ function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
},
},
};
- // add the dataset.
- let dataset = {
- label: current.label,
- currency_code: current.currency_code,
- data: [],
- yAxisID: "y" + current.currency_code,
- };
- for (let j in current.entries) {
- if (Object.hasOwn(current.entries, j)) {
- dataset.data.push(current.entries[j]);
- }
- }
- data.datasets.push(dataset);
}
}
}
+ // remove the standard y-axis, do not need it.
delete options.scales.y;
- options.scales = { ...options.scales, ...axes };
+ // options.scales.y.stacked = true;
+ // add the new axes.
+ options.scales = {...options.scales, ...axes};
- let labelCallback = function (tooltipItem) {
+ // add a callback for the label.
+ options.plugins.tooltip.callbacks.label = function (tooltipItem) {
"use strict";
let index = tooltipItem.dataIndex;
let amount = tooltipItem.dataset.data[index];
@@ -181,15 +371,8 @@ function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
return tooltipItem.dataset.label + ": " + string;
};
- options.plugins.tooltip.callbacks.label = labelCallback;
-
- if (
- typeof data === "undefined" ||
- 0 === data.length ||
- (typeof data === "object" &&
- typeof data.labels === "object" &&
- 0 === data.labels.length)
- ) {
+ // safety catch in case there is no data.
+ if (typeof data === "undefined" || 0 === data.length || (typeof data === "object" && typeof data.labels === "object" && 0 === data.labels.length)) {
let el = document.getElementById(holder).parentElement;
el.innerHTML = "";
el.classList.add("general-chart-error");
@@ -201,11 +384,21 @@ function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
// if (colorData) {
// data = colorizeData(data);
// }
- if (drawTodayMarker && "" !== drawTodayIndex) {
+
+ // add a marker to the chart if defined.
+ if (drawTodayMarker && "" !== drawTodayLabel) {
let markDate = format(
- new Date(drawTodayIndex),
+ new Date(drawTodayLabel),
i18next.t("config.month_and_day_fns"),
);
+ let today = i18next.t("firefly.today");
+ let xAdjust = today.length * 4;
+ if (drawTodayIndex < 3) {
+ xAdjust = today.length * 4;
+ }
+ if (drawTodayIndex > 26) {
+ xAdjust = today.length * -4;
+ }
// draw line using annotation plugin.
options.plugins.annotation = {
annotations: {
@@ -219,7 +412,8 @@ function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
borderColor: "rgb(255, 0, 0)",
borderWidth: 1,
label: {
- content: i18next.t("firefly.today"),
+ xAdjust: xAdjust,
+ content: today,
enabled: true,
display: true,
position: "center",
@@ -228,7 +422,6 @@ function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
},
};
}
- console.log(options);
// lineChart
const ctx = document.getElementById(holder).getContext("2d");
new Chart(ctx, {
@@ -274,7 +467,7 @@ function drawSingleCurrencyLineChart(url, holder, anonymous) {
return label + ": " + string;
};
- let options = { ...defaultChartOptions };
+ let options = {...defaultChartOptions};
options.scales.y.ticks.callback = yAxisCallback;
options.plugins.tooltip.callbacks.label = labelCallback;
diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php
index 1377725d1a..ddb70b13a1 100644
--- a/resources/views/index.blade.php
+++ b/resources/views/index.blade.php
@@ -32,7 +32,12 @@
</div>
</div>
<div class="card-body">
- <canvas id="budgets-chart" class="wide-chart" height="400" width="100%"></canvas>
+ <div>
+ <canvas id="budgets-chart" class="wide-chart" height="400" width="100%"></canvas>
+ </div>
+ <div>
+ <canvas id="budgets-two-chart" class="wide-chart" height="400" width="100%"></canvas>
+ </div>
</div>
<div class="card-footer text-end">
<a href="{{ route('budgets.index') }}" class="btn btn-primary btn-sm">
@@ -176,13 +181,13 @@
<template x-for="piggyBank in piggyBanks" :key="piggyBank.id">
<div>
<a :href="'./piggy-banks/show/' + piggyBank.id" :title="piggyBank.name"><strong
- x-text="piggyBank.name"></strong></a><br>
+ x-text="piggyBank.name"></strong></a><br>
<div class="progress">
<div class="progress-bar progress-bar-striped" role="progressbar"
:class="'w-'+piggyBank.percentage" :aria-valuenow="piggyBank.percentage"
aria-valuemin="0" aria-valuemax="100">
<template x-if="piggyBank.percentage > 50">
- <span x-text="piggyBank.amount"></span>
+ <span x-text="piggyBank.amount"></span>
</template>
</div>
<template x-if="piggyBank.percentage <= 50">
@@ -253,14 +258,8 @@
lineTextColor = '#bec5cb';
}
var billCount = {{ $billCount }};
- var accountFrontpageUrl = '{{ route('chart.account.frontpage') }}';
var accountRevenueUrl = '{{ route('chart.account.revenue') }}';
var accountExpenseUrl = '{{ route('chart.account.expense') }}';
- var drawVerticalLine = '';
- {{-- render vertical line with text "today" --}}
- @if($start->lte($today) && $end->gte($today))
- drawVerticalLine = '{{ $today->isoFormat($monthAndDayFormat) }}';
- @endif
</script>
<script type="text/javascript" src="v1/js/lib/Chart.bundle.min.js?v={{ $FF_BUILD_TIME }}"
diff --git a/resources/views/partials/form/transaction/amount.blade.php b/resources/views/partials/form/transaction/amount.blade.php
index 3e9d99482e..394cdad01b 100644
--- a/resources/views/partials/form/transaction/amount.blade.php
+++ b/resources/views/partials/form/transaction/amount.blade.php
@@ -10,7 +10,7 @@
:data-index="index"
:class="{'is-invalid': transaction.errors.amount.length > 0, 'input-mask' : true, 'form-control': true}"
x-model="transaction.amount"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
@change="changedAmount"
placeholder="0.00">
<template x-if="transaction.errors.amount.length > 0">
diff --git a/resources/views/partials/form/transaction/budget.blade.php b/resources/views/partials/form/transaction/budget.blade.php
index 37fbbd8a9f..abccba1100 100644
--- a/resources/views/partials/form/transaction/budget.blade.php
+++ b/resources/views/partials/form/transaction/budget.blade.php
@@ -16,7 +16,7 @@
<select class="form-select"
:id="'budget_id_' + index"
x-model="transaction.budget_id"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
>
<template x-for="budget in formData.budgets">
<option :label="budget.name" :value="budget.id" :selected="budget.id == transaction.budget_id"
diff --git a/resources/views/partials/form/transaction/date-time.blade.php b/resources/views/partials/form/transaction/date-time.blade.php
index 948316496d..9c78ca7783 100644
--- a/resources/views/partials/form/transaction/date-time.blade.php
+++ b/resources/views/partials/form/transaction/date-time.blade.php
@@ -5,7 +5,7 @@
<div class="col-sm-10">
<input type="datetime-local" class="form-control" :id="'date_' + index"
@change="changedDateTime"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
x-model="transaction.date"
>
</div>
diff --git a/resources/views/partials/form/transaction/description.blade.php b/resources/views/partials/form/transaction/description.blade.php
index dff9260bfd..7685de1d3a 100644
--- a/resources/views/partials/form/transaction/description.blade.php
+++ b/resources/views/partials/form/transaction/description.blade.php
@@ -7,7 +7,7 @@
<input type="text" class="form-control ac-description"
:id="'description_' + index"
@change="changedDescription"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
x-model="transaction.description"
:class="{'is-invalid': transaction.errors.description.length > 0, 'form-control': true}"
:data-index="index"
diff --git a/resources/views/partials/form/transaction/group-title.blade.php b/resources/views/partials/form/transaction/group-title.blade.php
index 084f6eab99..98dd44b259 100644
--- a/resources/views/partials/form/transaction/group-title.blade.php
+++ b/resources/views/partials/form/transaction/group-title.blade.php
@@ -8,7 +8,7 @@
<input type="text" class="form-control ac-group-title"
id="group-title"
@change="changedGroupTitle"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
x-model="groupProperties.title"
:disabled="index > 0"
:class="{'is-invalid': groupProperties.titleErrors.length > 0, 'form-control': true}"
diff --git a/resources/views/partials/form/transaction/piggy-bank.blade.php b/resources/views/partials/form/transaction/piggy-bank.blade.php
index 0a90a47225..d44b33d7c5 100644
--- a/resources/views/partials/form/transaction/piggy-bank.blade.php
+++ b/resources/views/partials/form/transaction/piggy-bank.blade.php
@@ -15,7 +15,7 @@
<template x-if="!formStates.loadingPiggyBanks">
<select class="form-select"
:id="'piggy_bank_id_' + index"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
x-model="transaction.piggy_bank_id">
<template x-for="group in formData.piggyBanks">
<optgroup :label="group.name">
diff --git a/resources/views/partials/form/transaction/subscription.blade.php b/resources/views/partials/form/transaction/subscription.blade.php
index 520ab14d02..8e646e48bc 100644
--- a/resources/views/partials/form/transaction/subscription.blade.php
+++ b/resources/views/partials/form/transaction/subscription.blade.php
@@ -14,7 +14,7 @@
<template x-if="!formStates.loadingSubscriptions">
<select class="form-select"
:id="'bill_id_' + index"
- @keyup.enter="submitTransaction()"
+ @keyup.enter="save()"
x-model="transaction.bill_id">
<template x-for="group in formData.subscriptions">
<optgroup :label="group.name">