Outsourced a chart to a specialised Google chart thing.

This commit is contained in:
James Cole 2015-06-27 08:18:47 +02:00
parent 9e5484937e
commit 63ff01e78d
4 changed files with 101 additions and 30 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace FireflyIII\Generator\Chart\Account;
use Illuminate\Support\Collection;
use Carbon\Carbon;
/**
* Interface AccountChartGenerator
*
* @package FireflyIII\Generator\Chart\Account
*/
interface AccountChartGenerator
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function all(Collection $accounts, Carbon $start, Carbon $end);
}

View File

@ -0,0 +1,56 @@
<?php
namespace FireflyIII\Generator\Chart\Account;
use Carbon\Carbon;
use Grumpydictator\Gchart\GChart;
use Illuminate\Support\Collection;
use Steam;
/**
* Class GoogleAccountChartGenerator
*
* @package FireflyIII\Generator\Chart\Account
*/
class GoogleAccountChartGenerator implements AccountChartGenerator
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function all(Collection $accounts, Carbon $start, Carbon $end)
{
// make chart (original code):
$chart = new GChart;
$chart->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();
}
}

View File

@ -2,6 +2,7 @@
namespace FireflyIII\Http\Controllers\Chart; namespace FireflyIII\Http\Controllers\Chart;
use App;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
@ -21,10 +22,24 @@ use Steam;
*/ */
class AccountController extends Controller 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. * Shows the balances for all the user's accounts.
* *
* @param GChart $chart
* @param AccountRepositoryInterface $repository * @param AccountRepositoryInterface $repository
* *
* @param $year * @param $year
@ -33,7 +48,7 @@ class AccountController extends Controller
* *
* @return \Symfony\Component\HttpFoundation\Response * @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'); $start = new Carbon($year . '-' . $month . '-01');
$end = clone $start; $end = clone $start;
@ -49,9 +64,6 @@ class AccountController extends Controller
return Response::json($cache->get()); // @codeCoverageIgnore return Response::json($cache->get()); // @codeCoverageIgnore
} }
$chart->addColumn(trans('firefly.dayOfMonth'), 'date');
/** @var Collection $accounts */ /** @var Collection $accounts */
$accounts = $repository->getAccounts(['Default account', 'Asset account']); $accounts = $repository->getAccounts(['Default account', 'Asset account']);
if ($shared === false) { if ($shared === false) {
@ -63,31 +75,8 @@ class AccountController extends Controller
} }
} }
// // make chart:
$data = $this->generator->all($accounts, $start, $end);
$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();
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);

View File

@ -91,6 +91,9 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository'); $this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository');
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); $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\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help');
$this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper'); $this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper');