From e3b2f2d9a8774fb2da3495e9b2b2c5410643f38e Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 Dec 2016 16:02:04 +0100 Subject: [PATCH] Experimental code for issue #452 --- .../Chart/Basic/ChartJsGenerator.php | 64 +++++++++++++++++++ .../Chart/Basic/GeneratorInterface.php | 48 ++++++++++++++ .../Controllers/Chart/AccountController.php | 29 ++++++--- app/Providers/FireflyServiceProvider.php | 3 + 4 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 app/Generator/Chart/Basic/ChartJsGenerator.php create mode 100644 app/Generator/Chart/Basic/GeneratorInterface.php 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 @@ + '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 @@ + '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');