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);
}
/**
* @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 Carbon $start

View File

@ -79,6 +79,36 @@ class AccountController extends Controller
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.
*

View File

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

View File

@ -282,6 +282,7 @@ Route::group(
*/
// accounts:
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(
['year' => '[0-9]{4}', 'month' => '[0-9]{1,2}', 'shared' => 'shared']
);
@ -300,8 +301,12 @@ Route::group(
// categories:
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/earned-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@earnedInYear'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']);
Route::get('/chart/category/spent-in-year/{year}/{shared?}', ['uses' => 'Chart\CategoryController@spentInYear'])->where(
['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}/all', ['uses' => 'Chart\CategoryController@all']);

View File

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

View File

@ -68,6 +68,25 @@
</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 -->
<div class="box">
<div class="box-header with-border">