Clean up chart code.

This commit is contained in:
James Cole
2016-12-11 17:05:48 +01:00
parent 553e9270e5
commit 08c4542847
5 changed files with 123 additions and 326 deletions

View File

@@ -1,71 +0,0 @@
<?php
/**
* AccountChartGeneratorInterface.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Account;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use Illuminate\Support\Collection;
/**
* Interface AccountChartGeneratorInterface
*
* @package FireflyIII\Generator\Chart\Account
*/
interface AccountChartGeneratorInterface
{
/**
* @param array $values
* @param array $names
*
* @return array
*/
public function pieChart(array $values, array $names): array;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function frontpage(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function revenueAccounts(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Account $account
* @param array $labels
* @param array $dataSet
*
* @return array
*/
public function single(Account $account, array $labels, array $dataSet): array;
}

View File

@@ -1,164 +0,0 @@
<?php
/**
* ChartJsAccountChartGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Account;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Support\ChartColour;
use Illuminate\Support\Collection;
/**
* Class ChartJsAccountChartGenerator
*
* @package FireflyIII\Generator\Chart\Account
*/
class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end): array
{
$data = [
'count' => 1,
'labels' => [], 'datasets' => [[
'label' => trans('firefly.spent'),
'data' => []]]];
foreach ($accounts as $account) {
if ($account->difference > 0) {
$data['labels'][] = $account->name;
$data['datasets'][0]['data'][] = $account->difference;
}
}
return $data;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function frontpage(Collection $accounts, Carbon $start, Carbon $end): array
{
// language:
$format = (string)trans('config.month_and_day');
$data = ['count' => 0, 'labels' => [], 'datasets' => [],];
$current = clone $start;
while ($current <= $end) {
$data['labels'][] = $current->formatLocalized($format);
$current->addDay();
}
foreach ($accounts as $account) {
$data['datasets'][] = [
'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' => $account->balances,
];
}
$data['count'] = count($data['datasets']);
return $data;
}
/**
* @param array $values
* @param array $names
*
* @return array
*/
public function pieChart(array $values, array $names): array
{
$data = [
'datasets' => [
0 => [],
],
'labels' => [],
];
$index = 0;
foreach ($values as $categoryId => $value) {
// make larger than 0
if (bccomp($value, '0') === -1) {
$value = bcmul($value, '-1');
}
$data['datasets'][0]['data'][] = round($value, 2);
$data['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
$data['labels'][] = $names[$categoryId];
$index++;
}
return $data;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function revenueAccounts(Collection $accounts, Carbon $start, Carbon $end): array
{
$data = [
'count' => 1,
'labels' => [], 'datasets' => [[
'label' => trans('firefly.earned'),
'data' => []]]];
foreach ($accounts as $account) {
if ($account->difference > 0) {
$data['labels'][] = $account->name;
$data['datasets'][0]['data'][] = $account->difference;
}
}
return $data;
}
/**
* @param Account $account
* @param array $labels
* @param array $dataSet
*
* @return array
*/
public function single(Account $account, array $labels, array $dataSet): array
{
$data = [
'count' => 1,
'labels' => $labels,
'datasets' => [
[
'label' => $account->name,
'data' => $dataSet,
],
],
];
return $data;
}
}

View File

@@ -13,6 +13,8 @@ declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Basic;
use FireflyIII\Support\ChartColour;
/**
* Class ChartJsGenerator
*
@@ -88,4 +90,38 @@ class ChartJsGenerator implements GeneratorInterface
return $chartData;
}
/**
* Expects data as:
*
* key => value
*
* @param array $data
*
* @return array
*/
public function pieChart(array $data): array
{
$chartData = [
'datasets' => [
0 => [],
],
'labels' => [],
];
$index = 0;
foreach ($data as $key => $value) {
// make larger than 0
if (bccomp($value, '0') === -1) {
$value = bcmul($value, '-1');
}
$chartData['datasets'][0]['data'][] = round($value, 2);
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
$chartData['labels'][] = $key;
$index++;
}
return $chartData;
}
}

View File

@@ -20,6 +20,18 @@ namespace FireflyIII\Generator\Chart\Basic;
*/
interface GeneratorInterface
{
/**
* Expects data as:
*
* key => value
*
* @param array $data
*
* @return array
*/
public function pieChart(array $data): array;
/**
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
*

View File

@@ -16,7 +16,6 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
@@ -43,7 +42,7 @@ use Steam;
class AccountController extends Controller
{
/** @var \FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface */
/** @var GeneratorInterface */
protected $generator;
/**
@@ -52,8 +51,7 @@ class AccountController extends Controller
public function __construct()
{
parent::__construct();
// create chart generator:
$this->generator = app(AccountChartGeneratorInterface::class);
$this->generator = app(GeneratorInterface::class);
}
/**
@@ -64,7 +62,7 @@ class AccountController extends Controller
public function all(Account $account)
{
$cache = new CacheProperties();
$cache->addProperty('account-all-chart');
$cache->addProperty('chart.account.all');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
@@ -74,7 +72,6 @@ class AccountController extends Controller
$repository = app(AccountRepositoryInterface::class);
$start = $repository->oldestJournalDate($account);
$end = new Carbon;
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
@@ -90,9 +87,7 @@ class AccountController extends Controller
$current->addDay();
}
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->singleSet($account->name, $chartData);
$data = $this->generator->singleSet($account->name, $chartData);
$cache->store($data);
return Response::json($data);
@@ -112,14 +107,13 @@ class AccountController extends Controller
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expenseAccounts');
$cache->addProperty('accounts');
$cache->addProperty('chart.account.expense-accounts');
if ($cache->has()) {
return Response::json($cache->get());
}
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
$start->subDay();
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
@@ -132,13 +126,10 @@ class AccountController extends Controller
$diff = bcsub($endBalance, $startBalance);
if (bccomp($diff, '0') !== 0) {
$chartData[$account->name] = round($diff, 2);
$account->difference = round($diff, 2);
}
}
arsort($chartData);
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->singleSet(trans('firefly.spent'), $chartData);
$data = $this->generator->singleSet(trans('firefly.spent'), $chartData);
$cache->store($data);
return Response::json($data);
@@ -158,28 +149,33 @@ class AccountController extends Controller
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expenseByBudget');
$cache->addProperty('chart.account.expense-budget');
if ($cache->has()) {
return Response::json($cache->get());
}
// grab all journals:
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withBudgetInformation()->setTypes([TransactionType::WITHDRAWAL]);
$collector->setAccounts(new Collection([$account]))
->setRange($start, $end)
->withBudgetInformation()
->setTypes([TransactionType::WITHDRAWAL]);
$transactions = $collector->getJournals();
$chartData = [];
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlBudgetId = intval($transaction->transaction_journal_budget_id);
$transBudgetId = intval($transaction->transaction_budget_id);
$budgetId = max($jrnlBudgetId, $transBudgetId);
$result[$budgetId] = $result[$budgetId] ?? '0';
$result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);
}
$names = $this->getBudgetNames(array_keys($result));
$data = $this->generator->pieChart($result, $names);
foreach ($result as $budgetId => $amount) {
$chartData[$names[$budgetId]] = $amount;
}
$data = $this->generator->pieChart($chartData);
$cache->store($data);
return Response::json($data);
@@ -199,26 +195,30 @@ class AccountController extends Controller
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expenseByCategory');
$cache->addProperty('chart.account.expense-category');
if ($cache->has()) {
return Response::json($cache->get());
}
// grab all journals:
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::WITHDRAWAL]);
$transactions = $collector->getJournals();
$result = [];
$chartData = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
$names = $this->getCategoryNames(array_keys($result));
$data = $this->generator->pieChart($result, $names);
foreach ($result as $categoryId => $amount) {
$chartData[$names[$categoryId]] = $amount;
}
$data = $this->generator->pieChart($chartData);
$cache->store($data);
return Response::json($data);
@@ -256,7 +256,7 @@ class AccountController extends Controller
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('incomeByCategory');
$cache->addProperty('chart.account.income-category');
if ($cache->has()) {
return Response::json($cache->get());
}
@@ -265,17 +265,21 @@ class AccountController extends Controller
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::DEPOSIT]);
$transactions = $collector->getJournals();
$result = [];
$chartData = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
$names = $this->getCategoryNames(array_keys($result));
$data = $this->generator->pieChart($result, $names);
foreach ($result as $categoryId => $amount) {
$chartData[$names[$categoryId]] = $amount;
}
$data = $this->generator->pieChart($chartData);
$cache->store($data);
return Response::json($data);
@@ -299,12 +303,10 @@ class AccountController extends Controller
}
$range = Preferences::get('viewRange', '1M')->data;
$end = Navigation::endOfPeriod($start, $range);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('frontpage');
$cache->addProperty('specificPeriod');
$cache->addProperty('chart.account.period');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
@@ -314,21 +316,18 @@ 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;
$label = $current->formatLocalized($format);
$chartData[$label] = $balance;
$previous = $balance;
$current->addDay();
}
$data = $this->generator->single($account, $labels, $chartData);
$data = $this->generator->singleSet($account->name, $chartData);
$cache->store($data);
return Response::json($data);
@@ -363,8 +362,7 @@ class AccountController extends Controller
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('revenueAccounts');
$cache->addProperty('accounts');
$cache->addProperty('chart.account.revenue-accounts');
if ($cache->has()) {
return Response::json($cache->get());
}
@@ -381,20 +379,13 @@ class AccountController extends Controller
$endBalance = $endBalances[$id] ?? '0';
$diff = bcsub($endBalance, $startBalance);
$diff = bcmul($diff, '-1');
$account->difference = round($diff, 2);
if (bccomp($diff, '0') !== 0) {
$chartData[$account->name] = round($diff, 2);
$account->difference = round($diff, 2);
}
}
asort($chartData);
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->singleSet(trans('firefly.spent'), $chartData);
$cache->store($data);
$data = $this->generator->revenueAccounts($accounts, $start, $end);
$data = $this->generator->singleSet(trans('firefly.spent'), $chartData);
$cache->store($data);
return Response::json($data);
@@ -416,8 +407,7 @@ class AccountController extends Controller
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('frontpage');
$cache->addProperty('single');
$cache->addProperty('chart.account.single');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
@@ -427,21 +417,18 @@ 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;
$label = $current->formatLocalized($format);
$chartData[$label] = $balance;
$previous = $balance;
$current->addDay();
}
$data = $this->generator->single($account, $labels, $chartData);
$data = $this->generator->singleSet($account->name, $chartData);
$cache->store($data);
return Response::json($data);
@@ -460,14 +447,13 @@ class AccountController extends Controller
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('account-balance-chart');
$cache->addProperty('chart.account.account-balance-chart');
$cache->addProperty($accounts);
if ($cache->has()) {
return $cache->get();
}
$chartData = [];
foreach ($accounts as $account) {
$currentSet = [
'label' => $account->name,
@@ -486,9 +472,7 @@ class AccountController extends Controller
}
$chartData[] = $currentSet;
}
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->multiSet($chartData);
$data = $this->generator->multiSet($chartData);
$cache->store($data);
return $data;