Skip to content

Firefly III: a personal finances manager

Add icons, build first chart.

James Coleae269c38e8a5
diff --git a/app/Http/Controllers/Chart/PiggyBankController.php b/app/Http/Controllers/Chart/PiggyBankController.php
index ea894a56f8..a738d9f18e 100644
--- a/app/Http/Controllers/Chart/PiggyBankController.php
+++ b/app/Http/Controllers/Chart/PiggyBankController.php
@@ -33,6 +33,8 @@ use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Facades\Navigation;
use FireflyIII\Support\Facades\Steam;
use FireflyIII\Support\Http\Controllers\DateCalculation;
+use FireflyIII\Support\JsonApi\Enrichments\PiggyBankEnrichment;
+use FireflyIII\User;
use Illuminate\Http\JsonResponse;
/**
@@ -67,8 +69,16 @@ final class PiggyBankController extends Controller
$cache->addProperty('chart.piggy-bank.history');
$cache->addProperty($piggyBank->id);
if ($cache->has()) {
- return response()->json($cache->get());
+ // return response()->json($cache->get());
}
+
+ // enrich
+ /** @var User $admin */
+ $admin = auth()->user();
+ $enrichment = new PiggyBankEnrichment();
+ $enrichment->setUser($admin);
+ /** @var PiggyBank $piggyBank */
+ $piggyBank = $enrichment->enrichSingle($piggyBank);
$set = $repository->getEvents($piggyBank);
$set = $set->reverse();
$locale = Steam::getLocale();
@@ -80,7 +90,7 @@ final class PiggyBankController extends Controller
$firstEvent = $set->first();
$firstDate = null === $firstEvent ? new Carbon() : $firstEvent->date;
- // which ever is older:
+ // whichever is older:
$oldest = $startDate->lt($firstDate) ? $startDate : $firstDate;
$today = today(config('app.timezone'));
// depending on diff, do something with range of chart.
@@ -100,8 +110,12 @@ final class PiggyBankController extends Controller
$chartData[$finalLabel] = $finalSum;
$data = $this->generator->singleSet($piggyBank->name, $chartData);
- $cache->store($data);
+ $response = [
+ 'data' => $data,
+ 'currency' => $piggyBank->meta['currency']->toArray(),
+ ];
+ $cache->store($response);
- return response()->json($data);
+ return response()->json($response);
}
}
diff --git a/config/translations.php b/config/translations.php
index 45495a9491..3c2fa4dfe7 100644
--- a/config/translations.php
+++ b/config/translations.php
@@ -68,8 +68,10 @@ return [
'withdrawal',
'i_owe_amount',
'i_am_owed_amount',
+ 'no_data_for_chart',
'deposit',
'transfer',
+ 'could_not_load_chart',
'inactive',
'liability_direction_debit_short',
'liability_direction_credit_short',
diff --git a/public/images/empty.png b/public/images/empty.png
new file mode 100644
index 0000000000..ebdbd50704
Binary files /dev/null and b/public/images/empty.png differ
diff --git a/public/images/warning-sign.png b/public/images/warning-sign.png
new file mode 100644
index 0000000000..1ef0b50508
Binary files /dev/null and b/public/images/warning-sign.png differ
diff --git a/resources/assets/v3/js/boot/bootstrap.js b/resources/assets/v3/js/boot/bootstrap.js
index 69cae5ea74..d0991d6b24 100644
--- a/resources/assets/v3/js/boot/bootstrap.js
+++ b/resources/assets/v3/js/boot/bootstrap.js
@@ -46,8 +46,8 @@ getFreshVariable('lastActivity').then((serverValue) => {
throw new Error('401 in getFreshVariable.');
}
const localValue = store.get('lastActivity');
- // store.set('cacheValid', localValue === serverValue);
- // store.set('lastActivity', serverValue);
+ store.set('cacheValid', localValue === serverValue);
+ store.set('lastActivity', serverValue);
// console.log('Server value: ' + serverValue);
// console.log('Local value: ' + localValue);
// console.log('Cache valid: ' + (localValue === serverValue));
diff --git a/resources/assets/v3/js/pages/piggy-banks/show.js b/resources/assets/v3/js/pages/piggy-banks/show.js
index 06619ff2f3..9350007706 100644
--- a/resources/assets/v3/js/pages/piggy-banks/show.js
+++ b/resources/assets/v3/js/pages/piggy-banks/show.js
@@ -22,7 +22,7 @@ import '../../boot/bootstrap.js';
import sidebar from '../../pages/shared/sidebar.js';
import dates from '../shared/dates.js';
import i18next from "i18next";
-import {drawChart} from '../../shared/draw-chart.js';
+import {drawSingleCurrencyChart} from '../../shared/draw-chart.js';
import {getVariable} from "../../store/get-variable.js";
let show = function () {
@@ -31,12 +31,11 @@ let show = function () {
// console.log('Generic JS for page with few features.');
//const ctx = document.getElementById('piggy-bank-history');
getVariable('anonymous').then((value) => {
- drawChart('line', 'some-url', 'piggy-bank-history', value);
+ const page = window.location.href.split('/');
+ const piggyBankID = parseInt(page[page.length - 1]);
+ drawSingleCurrencyChart('line', 'chart/piggy-bank/' + piggyBankID, 'piggy-bank-history', value);
});
-
-
-
},
confirmHistoryRemoval(e) {
if(confirm(i18next.t('firefly.reset_history_confirm'))) {
diff --git a/resources/assets/v3/js/shared/draw-chart.js b/resources/assets/v3/js/shared/draw-chart.js
index 39bf4eb6f2..1996650db2 100644
--- a/resources/assets/v3/js/shared/draw-chart.js
+++ b/resources/assets/v3/js/shared/draw-chart.js
@@ -19,6 +19,8 @@
*/
import Chart from 'chart.js/auto';
+import formatMoney from "../util/format-money.js";
+import i18next from "i18next";
let defaultChartOptions = {
@@ -27,6 +29,15 @@ let defaultChartOptions = {
cubicInterpolationMode: 'monotone'
}
},
+ plugins: {
+ legend: {
+ display: false,
+ },
+ tooltip: {
+ callbacks: {
+ }
+ }
+ },
responsive: true,
maintainAspectRatio: false,
scales: {
@@ -39,68 +50,78 @@ let defaultChartOptions = {
y: {
display: true,
beginAtZero: true,
- ticks: {
- // break ticks when too long.
- callback: function (value, index, ticks) {
- if(anonymous) {
- return '0';
- }
- return formatLabel(value, 20);
- }
- }
+ ticks: {}
},
- // ],
- // yAxes: [{
- // ticks: {
- // callback: function (tickValue) {
- // "use strict";
- // if (anonymous) {
- // return accounting.formatMoney(0);
- // }
- // // use first symbol or null:
- // return accounting.formatMoney(tickValue);
- // },
- // beginAtZero: true
- // }
- //
- // }]
- },
- // tooltips: {
- // mode: 'label',
- // callbacks: {
- // label: function (tooltipItem, data) {
- // "use strict";
- // var string = accounting.formatMoney(tooltipItem.yLabel, data.datasets[tooltipItem.datasetIndex].currency_symbol);
- // if (anonymous) {
- // string = accounting.formatMoney(0);
- // }
- // return data.datasets[tooltipItem.datasetIndex].label + ': ' + string;
- // }
- // }
- // }
+ }
};
-export function drawChart(type, url, holder, anonymous) {
+export function drawSingleCurrencyChart(type, url, holder, anonymous) {
if ('line' === type) {
- drawLineChart(url, holder, anonymous);
+ drawSingleCurrencyLineChart(url, holder, anonymous);
}
}
-function drawLineChart(url, holder) {
- // lineChart
- console.log('here we are');
- const ctx = document.getElementById(holder).getContext('2d');
-
- new Chart(ctx, {
- type: 'line',
- data: {
- labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
- datasets: [{
- label: '# of Votes',
- data: [12, 19, 3, 5, 2, 3],
- borderWidth: 1
- }]
- },
- options: defaultChartOptions
+function drawSingleCurrencyLineChart(url, holder, anonymous) {
+
+ document.getElementById(holder).classList.remove('general-chart-error');
+ window.axios.get(url).then((response) => {
+ let all = response.data;
+ let data = all.data;
+ let currency = all.currency;
+
+
+ let yAxisCallback = function (value, index, ticks) {
+ if (anonymous) {
+ value = '0';
+ }
+ return formatMoney(value, currency.code);
+ }
+ let labelCallback = function (tooltipItem) {
+ "use strict";
+ let index = tooltipItem.dataIndex;
+ let amount = tooltipItem.dataset.data[index];
+ let label = tooltipItem.label;
+ let string = formatMoney(amount, currency.code);
+ if (anonymous) {
+ string = formatMoney('0', currency.code);
+ }
+ return label + ': ' + string;
+ }
+
+
+ let options = {...defaultChartOptions};
+ options.scales.y.ticks.callback = yAxisCallback;
+ 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;
+ }
+
+ // TODO colorize data?
+ // if (colorData) {
+ // data = colorizeData(data);
+ // }
+
+ // 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 = '';
+ el.classList.add('general-chart-error');
+ el.innerText = i18next.t('firefly.could_not_load_chart') + ' ' + error;
});
+
+
}
diff --git a/resources/assets/v3/js/store/get-variable.js b/resources/assets/v3/js/store/get-variable.js
index 1462d1ce0b..5451c70463 100644
--- a/resources/assets/v3/js/store/get-variable.js
+++ b/resources/assets/v3/js/store/get-variable.js
@@ -23,6 +23,7 @@ import Post from "../api/preferences/post.js";
export function getVariable(name, defaultValue = null) {
const validCache = window.store.get('cacheValid');
+ console.log('Cache valid: ', validCache);
// currently unused, window.X can be used by the blade template
// to make things available quicker than if the store has to grab it through the API.
// then again, it's not that slow.
diff --git a/resources/assets/v3/sass/app.scss b/resources/assets/v3/sass/app.scss
index f347bd8812..067719bfa3 100644
--- a/resources/assets/v3/sass/app.scss
+++ b/resources/assets/v3/sass/app.scss
@@ -72,9 +72,20 @@ html[data-bs-theme="dark"] .money-transfer {
// custom Firefly III CSS
.general-chart-error {
- height: 30px;
- // TODO move picutre
- background: url('/v1/images/error.png') no-repeat center center;
+ min-height: 250px;
+ text-align:center;
+ background-size: 32px 32px;
+ background-image: url('/images/warning-sign.png');
+ background-repeat: no-repeat;
+ background-position: center center;
+}
+.empty-chart-error {
+ min-height: 250px;
+ text-align:center;
+ background-size: 32px 32px;
+ background-image: url('/images/empty.png');
+ background-repeat: no-repeat;
+ background-position: center center;
}
// hide search.
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 4863f80b9c..90ea22ef3a 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -44,6 +44,7 @@ return [
'month_to_date' => 'Month to date',
'year_to_date' => 'Year to date',
'previous_year' => 'Previous year (:year)',
+ 'could_not_load_chart' => 'Could not load chart. Sorry about that. This is the error:',
'YTD' => 'YTD',
'welcome_back' => 'What\'s playing?',
'main_dashboard_page_title' => 'Home',