New chart.

This commit is contained in:
James Cole 2015-08-01 07:04:41 +02:00
parent c04f08dfd8
commit 16b95ea78a
6 changed files with 120 additions and 27 deletions

View File

@ -32,6 +32,51 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
return $this->frontpage($accounts, $start, $end); return $this->frontpage($accounts, $start, $end);
} }
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end)
{
// language:
$data = [
'count' => 1,
'labels' => [],
'datasets' => [
[
'label' => trans('firefly.spent'),
'data' => []
]
],
];
$ids = [];
foreach ($accounts as $account) {
$ids[] = $account->id;
}
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
foreach ($accounts as $account) {
$id = $account->id;
$start = isset($startBalances[$id]) ? $startBalances[$id] : 0;
$end = isset($endBalances[$id]) ? $endBalances[$id] : 0;
$diff = $end - $start;
if ($diff > 0) {
$data['labels'][] = $account->name;
$data['datasets'][0]['data'][] = $diff;
}
}
return $data;
}
/** /**
* @param Collection $accounts * @param Collection $accounts
* @param Carbon $start * @param Carbon $start

View File

@ -79,6 +79,36 @@ class AccountController extends Controller
return Response::json($data); return Response::json($data);
} }
/**
* Shows the balances for all the user's expense accounts.
*
* @param AccountRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function expenseAccounts(AccountRepositoryInterface $repository)
{
$start = clone Session::get('start', Carbon::now()->startOfMonth());
$end = clone Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getAccounts(['Expense account', 'Beneficiary account']);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expenseAccounts');
$cache->addProperty('accounts');
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$data = $this->generator->expenseAccounts($accounts, $start, $end);
$cache->store($data);
return Response::json($data);
}
/** /**
* Shows the balances for all the user's frontpage accounts. * Shows the balances for all the user's frontpage accounts.
* *

View File

@ -186,15 +186,16 @@ class CategoryController extends Controller
$shared = $shared == 'shared' ? true : false; $shared = $shared == 'shared' ? true : false;
$allCategories = $repository->getCategories(); $allCategories = $repository->getCategories();
$entries = new Collection; $entries = new Collection;
$categories = new Collection; $categories = $allCategories->filter(
function (Category $category) use ($repository, $start, $end, $shared) {
// filter by checking the entire year first:
foreach ($allCategories as $category) {
$spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared); $spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared);
if ($spent < 0) { if ($spent < 0) {
$categories->push($category); return $category;
} }
return null;
} }
);
while ($start < $end) { while ($start < $end) {
$month = clone $start; // month is the current end of the period $month = clone $start; // month is the current end of the period
@ -204,18 +205,14 @@ class CategoryController extends Controller
foreach ($categories as $category) { // each budget, fill the row foreach ($categories as $category) { // each budget, fill the row
$spent = $repository->spentInPeriodCorrected($category, $start, $month, $shared); $spent = $repository->spentInPeriodCorrected($category, $start, $month, $shared);
if ($spent < 0) { if ($spent < 0) {
$spent = $spent * -1; $row[] = $spent * -1;
$row[] = $spent;
} else { } else {
$row[] = 0; $row[] = 0;
} }
} }
$entries->push($row); $entries->push($row);
$start->addMonth(); $start->addMonth();
} }
$data = $this->generator->spentInYear($categories, $entries); $data = $this->generator->spentInYear($categories, $entries);
$cache->store($data); $cache->store($data);
@ -248,16 +245,16 @@ class CategoryController extends Controller
$shared = $shared == 'shared' ? true : false; $shared = $shared == 'shared' ? true : false;
$allCategories = $repository->getCategories(); $allCategories = $repository->getCategories();
$allEntries = new Collection; $allEntries = new Collection;
$categories = new Collection; $categories = $allCategories->filter(
function (Category $category) use ($repository, $start, $end, $shared) {
// filter by checking the entire year first:
foreach ($allCategories as $category) {
$spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared); $spent = $repository->spentInPeriodCorrected($category, $start, $end, $shared);
if ($spent > 0) { if ($spent > 0) {
$categories->push($category); return $category;
}
} }
return null;
}
);
while ($start < $end) { while ($start < $end) {
$month = clone $start; // month is the current end of the period $month = clone $start; // month is the current end of the period
@ -271,14 +268,10 @@ class CategoryController extends Controller
} else { } else {
$row[] = 0; $row[] = 0;
} }
} }
$allEntries->push($row); $allEntries->push($row);
$start->addMonth(); $start->addMonth();
} }
$data = $this->generator->earnedInYear($categories, $allEntries); $data = $this->generator->earnedInYear($categories, $allEntries);
$cache->store($data); $cache->store($data);

View File

@ -282,6 +282,7 @@ Route::group(
*/ */
// accounts: // accounts:
Route::get('/chart/account/frontpage', ['uses' => 'Chart\AccountController@frontpage']); Route::get('/chart/account/frontpage', ['uses' => 'Chart\AccountController@frontpage']);
Route::get('/chart/account/expense', ['uses' => 'Chart\AccountController@expenseAccounts']);
Route::get('/chart/account/month/{year}/{month}/{shared?}', ['uses' => 'Chart\AccountController@all'])->where( Route::get('/chart/account/month/{year}/{month}/{shared?}', ['uses' => 'Chart\AccountController@all'])->where(
['year' => '[0-9]{4}', 'month' => '[0-9]{1,2}', 'shared' => 'shared'] ['year' => '[0-9]{4}', 'month' => '[0-9]{1,2}', 'shared' => 'shared']
); );
@ -300,8 +301,12 @@ Route::group(
// categories: // categories:
Route::get('/chart/category/frontpage', ['uses' => 'Chart\CategoryController@frontpage']); Route::get('/chart/category/frontpage', ['uses' => 'Chart\CategoryController@frontpage']);
Route::get('/chart/category/spent-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@spentInYear'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']); Route::get('/chart/category/spent-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@spentInYear'])->where(
Route::get('/chart/category/earned-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@earnedInYear'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']); ['year' => '[0-9]{4}', 'shared' => 'shared']
);
Route::get('/chart/category/earned-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@earnedInYear'])->where(
['year' => '[0-9]{4}', 'shared' => 'shared']
);
Route::get('/chart/category/{category}/month', ['uses' => 'Chart\CategoryController@month']); // should be period. Route::get('/chart/category/{category}/month', ['uses' => 'Chart\CategoryController@month']); // should be period.
Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']); Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']);

View File

@ -41,6 +41,7 @@ function drawChart() {
pieChart('chart/bill/frontpage', 'bills-chart'); pieChart('chart/bill/frontpage', 'bills-chart');
stackedColumnChart('chart/budget/frontpage', 'budgets-chart'); stackedColumnChart('chart/budget/frontpage', 'budgets-chart');
columnChart('chart/category/frontpage', 'categories-chart'); columnChart('chart/category/frontpage', 'categories-chart');
columnChart('chart/account/expense', 'expense-accounts-chart');
getBoxAmounts(); getBoxAmounts();

View File

@ -68,6 +68,25 @@
</div> </div>
</div> </div>
<!-- EXPENSE ACCOUNTS -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'expense_accounts'|_ }}</h3>
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="box-body">
{% if Config.get('firefly.chart') == 'google' %}
<div id="expense-accounts-chart"></div>
{% endif %}
{% if Config.get('firefly.chart') == 'chartjs' %}
<canvas id="expense-accounts-chart" style="width:100%;height:400px;"></canvas>
{% endif %}
</div>
</div>
<!-- SAVINGS --> <!-- SAVINGS -->
<div class="box"> <div class="box">
<div class="box-header with-border"> <div class="box-header with-border">