Skip to content

Firefly III: a personal finances manager

Experimental code for issue #452

James Colee3b2f2d9a877
diff --git a/app/Generator/Chart/Basic/ChartJsGenerator.php b/app/Generator/Chart/Basic/ChartJsGenerator.php
new file mode 100644
index 0000000000..a4ba5bc7ac
--- /dev/null
+++ b/app/Generator/Chart/Basic/ChartJsGenerator.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * ChartJsGenerator.php
+ * Copyright (C) 2016 [email protected]
+ *
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types = 1);
+
+namespace FireflyIII\Generator\Chart\Basic;
+
+/**
+ * Class ChartJsGenerator
+ *
+ * @package FireflyIII\Generator\Chart\Basic
+ */
+class ChartJsGenerator implements GeneratorInterface
+{
+
+ /**
+ * Will generate a Chart JS compatible array from the given input. Expects this format:
+ *
+ * 0: [
+ * 'label' => 'label of set',
+ * 'entries' =>
+ * [
+ * 'label-of-entry' => 'value'
+ * ]
+ * ]
+ * 1: [
+ * 'label' => 'label of another set',
+ * 'entries' =>
+ * [
+ * 'label-of-entry' => 'value'
+ * ]
+ * ]
+ *
+ *
+ * @param array $data
+ *
+ * @return array
+ */
+ public function multiSet(array $data): array
+ {
+ $chartData = [
+ 'count' => count($data),
+ 'labels' => array_keys($data[0]['entries']), // take ALL labels from the first set.
+ 'datasets' => [],
+ ];
+
+ foreach ($data as $set) {
+ $chartData['datasets'][] = [
+ 'label' => $set['label'],
+ 'data' => array_values($set['entries']),
+ ];
+ }
+
+ return $chartData;
+ }
+}
\ No newline at end of file
diff --git a/app/Generator/Chart/Basic/GeneratorInterface.php b/app/Generator/Chart/Basic/GeneratorInterface.php
new file mode 100644
index 0000000000..0a18521d72
--- /dev/null
+++ b/app/Generator/Chart/Basic/GeneratorInterface.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * GeneratorInterface.php
+ * Copyright (C) 2016 [email protected]
+ *
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types = 1);
+
+namespace FireflyIII\Generator\Chart\Basic;
+
+/**
+ * Interface GeneratorInterface
+ *
+ * @package FireflyIII\Generator\Chart\Basic
+ */
+interface GeneratorInterface
+{
+ /**
+ * Will generate a Chart JS compatible array from the given input. Expects this format:
+ *
+ * 0: [
+ * 'label' => 'label of set',
+ * 'entries' =>
+ * [
+ * 'label-of-entry' => 'value'
+ * ]
+ * ]
+ * 1: [
+ * 'label' => 'label of another set',
+ * 'entries' =>
+ * [
+ * 'label-of-entry' => 'value'
+ * ]
+ * ]
+ *
+ *
+ * @param array $data
+ *
+ * @return array
+ */
+ public function multiSet(array $data): array;
+
+}
\ No newline at end of file
diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php
index b8f1ad5a7d..72174240da 100644
--- a/app/Http/Controllers/Chart/AccountController.php
+++ b/app/Http/Controllers/Chart/AccountController.php
@@ -17,6 +17,7 @@ use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
+use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
@@ -469,21 +470,33 @@ class AccountController extends Controller
return $cache->get();
}
+ $chartData = [
+
+ ];
+
foreach ($accounts as $account) {
- $balances = [];
- $current = clone $start;
- $range = Steam::balanceInRange($account, $start, clone $end);
- $previous = round(array_values($range)[0], 2);
- while ($current <= $end) {
- $format = $current->format('Y-m-d');
+ $currentSet = [
+ 'label' => $account->name,
+ 'entries' => [],
+ ];
+ $balances = [];
+ $currentStart = clone $start;
+ $range = Steam::balanceInRange($account, $start, clone $end);
+ $previous = round(array_values($range)[0], 2);
+ while ($currentStart <= $end) {
+ $format = $currentStart->format('Y-m-d');
+ $label = $currentStart->formatLocalized(strval(trans('config.month_and_day')));
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
$previous = $balance;
$balances[] = $balance;
- $current->addDay();
+ $currentStart->addDay();
+ $currentSet['entries'][$label] = $balance;
}
$account->balances = $balances;
+ $chartData[] = $currentSet;
}
- $data = $this->generator->frontpage($accounts, $start, $end);
+ $generator = app(GeneratorInterface::class);
+ $data = $generator->multiSet($chartData);
$cache->store($data);
return $data;
diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php
index 91122f0d5f..6b832907a6 100644
--- a/app/Providers/FireflyServiceProvider.php
+++ b/app/Providers/FireflyServiceProvider.php
@@ -93,6 +93,9 @@ class FireflyServiceProvider extends ServiceProvider
}
);
+ // chart generator:
+ $this->app->bind('FireflyIII\Generator\Chart\Basic\GeneratorInterface', 'FireflyIII\Generator\Chart\Basic\ChartJsGenerator');
+
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
$this->app->bind('FireflyIII\Repositories\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
$this->app->bind('FireflyIII\Helpers\Attachments\AttachmentHelperInterface', 'FireflyIII\Helpers\Attachments\AttachmentHelper');