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