From 838330b90919a8bc47f12b1255077ecbf0440c70 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 16 Dec 2015 10:17:15 +0100 Subject: [PATCH] First attempt at multi-year budget chart. --- .../Chart/Budget/BudgetChartGenerator.php | 7 ++++ .../Budget/ChartJsBudgetChartGenerator.php | 39 +++++++++++++++++++ .../Controllers/Chart/BudgetController.php | 24 +++++++++++- public/js/reports/default/multi-year.js | 3 ++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/Generator/Chart/Budget/BudgetChartGenerator.php b/app/Generator/Chart/Budget/BudgetChartGenerator.php index 552a8b2933..c7aa64a863 100644 --- a/app/Generator/Chart/Budget/BudgetChartGenerator.php +++ b/app/Generator/Chart/Budget/BudgetChartGenerator.php @@ -18,6 +18,13 @@ interface BudgetChartGenerator */ public function budget(Collection $entries); + /** + * @param Collection $entries + * + * @return array + */ + public function multiYear(Collection $entries); + /** * @param Collection $entries * diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php index d91a293286..420501ac1a 100644 --- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php +++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php @@ -141,4 +141,43 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator return $data; } + + /** + * @param Collection $entries + * + * @return array + */ + public function multiYear(Collection $entries) + { + //var_dump($entries); + $data = [ + 'count' => 0, + 'labels' => [], + 'datasets' => [], + ]; + // labels: for each budget. + // dataset: for each year? + foreach($entries as $entry) { + $year = $entry['date']->year; + if(!in_array($year, $data['labels'])) { + $data['labels'][] = $entry['date']->year; + } + } + // can be joined? + $set = []; + foreach($entries as $entry) { + $name = $entry['budget']; + $set[$name] = isset($set[$name]) ? $set[$name] : []; + $set[$name][] = ($entry['sum'] * -1); + } + foreach($set as $name => $values) { + $data['datasets'][] = ['label' => $name, 'data' => $values]; + } + $data['count'] = count($data['datasets']); + +return $data; + //var_dump($data); + //exit; + + } } diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 2cdba30b4e..49b0f6ab2c 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -46,6 +46,19 @@ class BudgetController extends Controller */ public function multiYear(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets) { + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($report_type); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty($accounts); + $cache->addProperty($budgets); + $cache->addProperty('multiYearBudget'); + + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + $currentStart = clone $start; $collection = new Collection; @@ -53,7 +66,7 @@ class BudgetController extends Controller $currentEnd = clone $currentStart; $currentEnd->endOfYear(); - echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '
'; + //echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '
'; /** @var Budget $budget */ foreach ($budgets as $budget) { @@ -64,7 +77,8 @@ class BudgetController extends Controller $name = $budget->name; $sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts); } - echo $name.': '.$sum.'
'; + $collection->push(['budget' => $name, 'sum' => $sum,'date' => $currentStart]); + //echo $name . ': ' . $sum . '
'; } // do something for all budgets. @@ -74,6 +88,12 @@ class BudgetController extends Controller } + $data = $this->generator->multiYear($collection); + + //$cache->store($data); + + return Response::json($data); + } /** diff --git a/public/js/reports/default/multi-year.js b/public/js/reports/default/multi-year.js index 904982dd4e..9b3448b489 100644 --- a/public/js/reports/default/multi-year.js +++ b/public/js/reports/default/multi-year.js @@ -39,6 +39,9 @@ function updateBudgetChart(e) { }); var budgetIds = budgets.join(','); + // remove old chart: + $('#budgets-chart').replaceWith(''); + // draw chart. Redraw when exists? Not sure if we support that. columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');