From 3e82d438079ee41e604eea6897d863dfc7e22d85 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 27 Jun 2015 16:01:06 +0200 Subject: [PATCH] Expand some code to generate chartJS charts. --- .../Account/ChartJsAccountChartGenerator.php | 91 +++++++++++++++++++ .../Budget/ChartJsBudgetChartGenerator.php | 47 ++++++++++ .../Controllers/Chart/AccountController.php | 6 +- 3 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 app/Generator/Chart/Account/ChartJsAccountChartGenerator.php create mode 100644 app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php diff --git a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php new file mode 100644 index 0000000000..c496d2fb49 --- /dev/null +++ b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php @@ -0,0 +1,91 @@ +frontpage($accounts, $start, $end); + } + + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function frontpage(Collection $accounts, Carbon $start, Carbon $end) + { + // language: + $language = Preferences::get('language', 'en')->data; + $format = Config::get('firefly.monthAndDay.' . $language); + $data = [ + 'count' => 0, + 'labels' => [], + 'datasets' => [], + ]; + $current = clone $start; + while ($current <= $end) { + $data['labels'][] = $current->formatLocalized($format); + $current->addDay(); + } + + foreach ($accounts as $account) { + $set = [ + 'label' => $account->name, + 'fillColor' => 'rgba(220,220,220,0.2)', + 'strokeColor' => 'rgba(220,220,220,1)', + 'pointColor' => 'rgba(220,220,220,1)', + 'pointStrokeColor' => '#fff', + 'pointHighlightFill' => '#fff', + 'pointHighlightStroke' => 'rgba(220,220,220,1)', + 'data' => [], + ]; + $current = clone $start; + while ($current <= $end) { + $set['data'][] = Steam::balance($account, $current); + $current->addDay(); + } + $data['datasets'][] = $set; + $data['count']++; + } + + return $data; + } + + /** + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + public function single(Account $account, Carbon $start, Carbon $end) + { + // TODO: Implement single() method. + //throw new NotImplementedException; + } +} \ No newline at end of file diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php new file mode 100644 index 0000000000..d5cf392ba0 --- /dev/null +++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php @@ -0,0 +1,47 @@ +startOfMonth()); - $end = Session::get('end', Carbon::now()->endOfMonth()); + $start = clone Session::get('start', Carbon::now()->startOfMonth()); + $end = clone Session::get('end', Carbon::now()->endOfMonth()); $accounts = $repository->getFrontpageAccounts($frontPage); // chart properties for cache: @@ -101,7 +101,7 @@ class AccountController extends Controller $cache->addProperty('frontpage'); $cache->addProperty('accounts'); if ($cache->has()) { - return Response::json($cache->get()); // @codeCoverageIgnore + //return Response::json($cache->get()); // @codeCoverageIgnore } $data = $this->generator->frontpage($accounts, $start, $end);