From 0f260da8e62d85f4b37d6412a5571ff468b1e30c Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 Dec 2016 16:25:25 +0100 Subject: [PATCH] More code for issue #452 --- .../Chart/Basic/ChartJsGenerator.php | 27 +++++++++++++ .../Chart/Basic/GeneratorInterface.php | 15 ++++++- .../Controllers/Chart/AccountController.php | 39 ++++++++----------- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/app/Generator/Chart/Basic/ChartJsGenerator.php b/app/Generator/Chart/Basic/ChartJsGenerator.php index a4ba5bc7ac..9821ab06ad 100644 --- a/app/Generator/Chart/Basic/ChartJsGenerator.php +++ b/app/Generator/Chart/Basic/ChartJsGenerator.php @@ -61,4 +61,31 @@ class ChartJsGenerator implements GeneratorInterface return $chartData; } + + /** + * Will generate a (ChartJS) compatible array from the given input. Expects this format: + * + * 'label-of-entry' => value + * 'label-of-entry' => value + * + * @param string $setLabel + * @param array $data + * + * @return array + */ + public function singleSet(string $setLabel, array $data): array + { + $chartData = [ + 'count' => 1, + 'labels' => array_keys($data), // take ALL labels from the first set. + 'datasets' => [ + [ + 'label' => $setLabel, + 'data' => array_values($data), + ], + ], + ]; + + return $chartData; + } } \ No newline at end of file diff --git a/app/Generator/Chart/Basic/GeneratorInterface.php b/app/Generator/Chart/Basic/GeneratorInterface.php index 0a18521d72..ba579080db 100644 --- a/app/Generator/Chart/Basic/GeneratorInterface.php +++ b/app/Generator/Chart/Basic/GeneratorInterface.php @@ -21,7 +21,7 @@ namespace FireflyIII\Generator\Chart\Basic; interface GeneratorInterface { /** - * Will generate a Chart JS compatible array from the given input. Expects this format: + * Will generate a (ChartJS) compatible array from the given input. Expects this format: * * 0: [ * 'label' => 'label of set', @@ -45,4 +45,17 @@ interface GeneratorInterface */ public function multiSet(array $data): array; + /** + * Will generate a (ChartJS) compatible array from the given input. Expects this format: + * + * 'label-of-entry' => value + * 'label-of-entry' => value + * + * @param string $setLabel + * @param array $data + * + * @return array + */ + public function singleSet(string $setLabel, 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 72174240da..5955f333a6 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -58,10 +58,8 @@ class AccountController extends Controller /** * @param Account $account - * @param string $date * * @return \Illuminate\Http\JsonResponse - * @throws FireflyException */ public function all(Account $account) { @@ -69,7 +67,7 @@ class AccountController extends Controller $cache->addProperty('account-all-chart'); $cache->addProperty($account->id); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); } /** @var AccountRepositoryInterface $repository */ @@ -81,21 +79,20 @@ class AccountController extends Controller $range = Steam::balanceInRange($account, $start, $end); $current = clone $start; $previous = array_values($range)[0]; - $labels = []; $chartData = []; while ($end >= $current) { - $theDate = $current->format('Y-m-d'); - $balance = $range[$theDate] ?? $previous; - - $labels[] = $current->formatLocalized($format); - $chartData[] = $balance; - $previous = $balance; + $theDate = $current->format('Y-m-d'); + $balance = $range[$theDate] ?? $previous; + $label = $current->formatLocalized($format); + $chartData[$label] = $balance; + $previous = $balance; $current->addDay(); } - - $data = $this->generator->single($account, $labels, $chartData); + /** @var GeneratorInterface $generator */ + $generator = app(GeneratorInterface::class); + $data = $generator->singleSet($account->name, $chartData); $cache->store($data); return Response::json($data); @@ -470,31 +467,27 @@ class AccountController extends Controller return $cache->get(); } - $chartData = [ - - ]; + $chartData = []; foreach ($accounts as $account) { $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; + $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; $currentStart->addDay(); $currentSet['entries'][$label] = $balance; } - $account->balances = $balances; - $chartData[] = $currentSet; + $chartData[] = $currentSet; } + /** @var GeneratorInterface $generator */ $generator = app(GeneratorInterface::class); $data = $generator->multiSet($chartData); $cache->store($data);