Skip to content

Firefly III: a personal finances manager

Expand options.

James Cole4f81a68bfedb
diff --git a/config/translations.php b/config/translations.php
index 411866d9fa..5b5a599068 100644
--- a/config/translations.php
+++ b/config/translations.php
@@ -68,6 +68,7 @@ return [
'withdrawal',
'i_owe_amount',
'intro_next_label',
+ 'today',
'intro_prev_label',
'intro_done_label',
'i_am_owed_amount',
diff --git a/package-lock.json b/package-lock.json
index bfa6d95554..528a464353 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1249,6 +1249,15 @@
"pnpm": ">=8"
}
},
+ "node_modules/chartjs-plugin-annotation": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/chartjs-plugin-annotation/-/chartjs-plugin-annotation-3.1.0.tgz",
+ "integrity": "sha512-EkAed6/ycXD/7n0ShrlT1T2Hm3acnbFhgkIEJLa0X+M6S16x0zwj1Fv4suv/2bwayCT3jGPdAtI9uLcAMToaQQ==",
+ "license": "MIT",
+ "peerDependencies": {
+ "chart.js": ">=4.0.0"
+ }
+ },
"node_modules/chokidar": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-5.0.0.tgz",
@@ -3635,6 +3644,7 @@
"bootstrap5-tags": "^1.7.17",
"cally": "^0.9.2",
"chart.js": "^4.5.1",
+ "chartjs-plugin-annotation": "^3.1.0",
"date-fns": "^4.4.0",
"fullcalendar": "^7.0.1",
"i18next": "^26.0.3",
diff --git a/resources/assets/v3/js/pages/dashboard/dashboard.js b/resources/assets/v3/js/pages/dashboard/dashboard.js
index 1c1f5cd210..3d9b4e80e1 100644
--- a/resources/assets/v3/js/pages/dashboard/dashboard.js
+++ b/resources/assets/v3/js/pages/dashboard/dashboard.js
@@ -44,6 +44,7 @@ let index = function () {
"api/v1/chart/account/overview?period=1D&start=" + format(start,"yyyy-LL-dd") + "&end=" + format(end,"yyyy-LL-dd"),
"accounts-chart",
value,
+ true
);
});
},
diff --git a/resources/assets/v3/js/shared/draw-chart.js b/resources/assets/v3/js/shared/draw-chart.js
index 83686c27c4..9b4be10123 100644
--- a/resources/assets/v3/js/shared/draw-chart.js
+++ b/resources/assets/v3/js/shared/draw-chart.js
@@ -22,6 +22,10 @@ import Chart from "chart.js/auto";
import formatMoney from "../util/format-money.js";
import i18next from "i18next";
import format from '../util/format.js';
+import annotationPlugin from 'chartjs-plugin-annotation';
+
+Chart.register(annotationPlugin);
+
let defaultChartOptions = {
elements: {
@@ -54,39 +58,48 @@ let defaultChartOptions = {
},
};
-export function drawMultiCurrencyChart(type, url, holder, anonymous) {
+export function drawMultiCurrencyChart(type, url, holder, anonymous, drawTodayMarker) {
if ("line" === type) {
- drawMultiCurrencyLineChart(url, holder, anonymous);
+ drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker);
}
}
+
export function drawSingleCurrencyChart(type, url, holder, anonymous) {
if ("line" === type) {
drawSingleCurrencyLineChart(url, holder, anonymous);
}
}
-function drawMultiCurrencyLineChart(url, holder, anonymous) {
+function drawMultiCurrencyLineChart(url, holder, anonymous, drawTodayMarker) {
document.getElementById(holder).classList.remove("general-chart-error");
window.axios.get(url).then((response) => {
- let all = response.data;
- let options = { ...defaultChartOptions };
+ let all = response.data;
+ let options = {...defaultChartOptions};
+ let today = new Date();
+ let firstScale = ''; // used for today marker.
+ let drawTodayIndex = '';
let data = {
datasets: [],
labels: [],
};
let axes = {};
- // collect all Y axes from the data.
+ // collect all Y axes from the data.
for (let i = 0; i < all.length; i++) {
- if(Object.hasOwn(all, i)) {
+ if (Object.hasOwn(all, i)) {
let current = all[i];
// take the labels from index 0.
- if(0 === i) {
- for(let j in current.entries) {
- if(Object.hasOwn(current.entries, j)) {
+ if (0 === i) {
+ for (let j in current.entries) {
+ if (Object.hasOwn(current.entries, j)) {
let date = new Date(j);
- data.labels.push(format(date,i18next.t('config.month_and_day_fns')));
+ if (drawTodayMarker && isSameDay(date, today)) {
+ console.log('Today is ', j, date);
+ drawTodayIndex = j;
+ }
+ data.labels.push(format(date, i18next.t('config.month_and_day_fns')));
}
}
+ firstScale = 'y' + current.currency_code;
}
// add the dataset.
let dataset = {
@@ -95,8 +108,8 @@ function drawMultiCurrencyLineChart(url, holder, anonymous) {
data: [],
yAxisID: 'y' + current.currency_code,
};
- for(let j in current.entries) {
- if(Object.hasOwn(current.entries, j)) {
+ for (let j in current.entries) {
+ if (Object.hasOwn(current.entries, j)) {
dataset.data.push(current.entries[j]);
}
}
@@ -104,11 +117,11 @@ function drawMultiCurrencyLineChart(url, holder, anonymous) {
let currencyCode = current.currency_code;
let axisId = 'y' + currencyCode;
- if(!Object.hasOwn(axes, axisId)) {
+ if (!Object.hasOwn(axes, axisId)) {
axes[axisId] = {
id: axisId,
type: "linear",
- position: Object.keys(axes).length < 1 ? "left": 'right',
+ position: Object.keys(axes).length < 1 ? "left" : 'right',
ticks: {
callback: function (value) {
if (anonymous) {
@@ -121,51 +134,74 @@ function drawMultiCurrencyLineChart(url, holder, anonymous) {
}
}
}
- console.log(data);
delete options.scales.y;
- options.scales = { ...options.scales, ...axes };
-
- let labelCallback = function (tooltipItem) {
- "use strict";
- let index = tooltipItem.dataIndex;
- let amount = tooltipItem.dataset.data[index];
+ options.scales = {...options.scales, ...axes};
- let string = formatMoney(amount, tooltipItem.dataset.currency_code);
- if (anonymous) {
- string = formatMoney("0", tooltipItem.dataset.currency_code);
- }
- return tooltipItem.dataset.label + ": " + string;
- };
+ let labelCallback = function (tooltipItem) {
+ "use strict";
+ let index = tooltipItem.dataIndex;
+ let amount = tooltipItem.dataset.data[index];
- options.plugins.tooltip.callbacks.label = labelCallback;
-
- 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;
+ let string = formatMoney(amount, tooltipItem.dataset.currency_code);
+ if (anonymous) {
+ string = formatMoney("0", tooltipItem.dataset.currency_code);
}
+ return tooltipItem.dataset.label + ": " + string;
+ };
- // TODO colorize data?
- // if (colorData) {
- // data = colorizeData(data);
- // }
+ options.plugins.tooltip.callbacks.label = labelCallback;
- // lineChart
- const ctx = document.getElementById(holder).getContext("2d");
- new Chart(ctx, {
- type: "line",
- data: data,
- options: options,
- });
- })
+ 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);
+ // }
+ if (drawTodayMarker && '' !== drawTodayIndex) {
+ let markDate = format(new Date(drawTodayIndex), i18next.t('config.month_and_day_fns'));
+ // draw line using annotation plugin.
+ options.plugins.annotation = {
+ annotations: {
+ line1: {
+ type: 'line',
+ xScaleID: 'x',
+ yScaleID: firstScale,
+ xMin: markDate,
+ xMax: markDate,
+ display: true,
+ borderColor: 'rgb(255, 0, 0)',
+ borderWidth: 1,
+ label: {
+ content: i18next.t('firefly.today'),
+ enabled: true,
+ display: true,
+ position: 'center'
+ }
+ }
+ }
+ };
+ }
+ console.log(options);
+ // lineChart
+ const ctx = document.getElementById(holder).getContext("2d");
+ new Chart(ctx, {
+ type: "line",
+ data: data,
+ options: options,
+ });
+ })
.catch((error) => {
let el = document.getElementById(holder).parentElement;
el.innerHTML = "";
@@ -202,7 +238,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;
@@ -241,3 +277,11 @@ function drawSingleCurrencyLineChart(url, holder, anonymous) {
i18next.t("firefly.could_not_load_chart") + " " + error;
});
}
+
+
+// https://stackoverflow.com/questions/43855166/how-to-tell-if-two-dates-are-in-the-same-day-or-in-the-same-hour
+function isSameDay(d1, d2) {
+ return d1.getFullYear() === d2.getFullYear() &&
+ d1.getMonth() === d2.getMonth() &&
+ d1.getDate() === d2.getDate();
+}
diff --git a/resources/assets/v3/package.json b/resources/assets/v3/package.json
index 32440fc601..e6772956ea 100644
--- a/resources/assets/v3/package.json
+++ b/resources/assets/v3/package.json
@@ -13,6 +13,7 @@
"bootstrap5-tags": "^1.7.17",
"cally": "^0.9.2",
"chart.js": "^4.5.1",
+ "chartjs-plugin-annotation": "^3.1.0",
"date-fns": "^4.4.0",
"fullcalendar": "^7.0.1",
"i18next": "^26.0.3",