From 63ff01e78d9c24afd867452b24334bbfbb23de1a Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 27 Jun 2015 08:18:47 +0200 Subject: [PATCH] Outsourced a chart to a specialised Google chart thing. --- .../Chart/Account/AccountChartGenerator.php | 23 ++++++++ .../Account/GoogleAccountChartGenerator.php | 56 +++++++++++++++++++ .../Controllers/Chart/AccountController.php | 49 +++++++--------- app/Providers/FireflyServiceProvider.php | 3 + 4 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 app/Generator/Chart/Account/AccountChartGenerator.php create mode 100644 app/Generator/Chart/Account/GoogleAccountChartGenerator.php diff --git a/app/Generator/Chart/Account/AccountChartGenerator.php b/app/Generator/Chart/Account/AccountChartGenerator.php new file mode 100644 index 0000000000..6d72af2407 --- /dev/null +++ b/app/Generator/Chart/Account/AccountChartGenerator.php @@ -0,0 +1,23 @@ +addColumn(trans('firefly.dayOfMonth'), 'date'); + $index = 1; + /** @var Account $account */ + foreach ($accounts as $account) { + $chart->addColumn(trans('firefly.balanceFor', ['name' => $account->name]), 'number'); + $chart->addCertainty($index); + $index++; + } + $current = clone $start; + $current->subDay(); + $today = Carbon::now(); + while ($end >= $current) { + $row = [clone $current]; + $certain = $current < $today; + foreach ($accounts as $account) { + $row[] = Steam::balance($account, $current); + $row[] = $certain; + } + $chart->addRowArray($row); + $current->addDay(); + } + $chart->generate(); + + return $chart->getData(); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index 2c5d4f7a6e..2fec3f2619 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -2,6 +2,7 @@ namespace FireflyIII\Http\Controllers\Chart; +use App; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Account; @@ -21,10 +22,24 @@ use Steam; */ class AccountController extends Controller { + + /** @var \FireflyIII\Generator\Chart\Account\AccountChartGenerator */ + protected $generator; + + /** + * + */ + public function __construct() + { + parent::__construct(); + // create chart generator: + $generator = App::make('FireflyIII\Generator\Chart\Account\AccountChartGenerator'); + } + + /** * Shows the balances for all the user's accounts. * - * @param GChart $chart * @param AccountRepositoryInterface $repository * * @param $year @@ -33,7 +48,7 @@ class AccountController extends Controller * * @return \Symfony\Component\HttpFoundation\Response */ - public function all(GChart $chart, AccountRepositoryInterface $repository, $year, $month, $shared = false) + public function all(AccountRepositoryInterface $repository, $year, $month, $shared = false) { $start = new Carbon($year . '-' . $month . '-01'); $end = clone $start; @@ -49,9 +64,6 @@ class AccountController extends Controller return Response::json($cache->get()); // @codeCoverageIgnore } - - $chart->addColumn(trans('firefly.dayOfMonth'), 'date'); - /** @var Collection $accounts */ $accounts = $repository->getAccounts(['Default account', 'Asset account']); if ($shared === false) { @@ -63,31 +75,8 @@ class AccountController extends Controller } } - // - - $index = 1; - /** @var Account $account */ - foreach ($accounts as $account) { - $chart->addColumn(trans('firefly.balanceFor', ['name' => $account->name]), 'number'); - $chart->addCertainty($index); - $index++; - } - $current = clone $start; - $current->subDay(); - $today = Carbon::now(); - while ($end >= $current) { - $row = [clone $current]; - $certain = $current < $today; - foreach ($accounts as $account) { - $row[] = Steam::balance($account, $current); - $row[] = $certain; - } - $chart->addRowArray($row); - $current->addDay(); - } - $chart->generate(); - - $data = $chart->getData(); + // make chart: + $data = $this->generator->all($accounts, $start, $end); $cache->store($data); return Response::json($data); diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 7429939d67..1f8c4d0a3a 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -91,6 +91,9 @@ class FireflyServiceProvider extends ServiceProvider $this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository'); $this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); + // make Google charts: + $this->app->bind('FireflyIII\Generator\Chart\Account\AccountChartGenerator', 'FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator'); + $this->app->bind('FireflyIII\Helpers\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help'); $this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper');