First attempt at multi-year budget chart.

This commit is contained in:
James Cole 2015-12-16 10:17:15 +01:00
parent 69553b138b
commit 838330b909
4 changed files with 71 additions and 2 deletions

View File

@ -18,6 +18,13 @@ interface BudgetChartGenerator
*/
public function budget(Collection $entries);
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries);
/**
* @param Collection $entries
*

View File

@ -141,4 +141,43 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries)
{
//var_dump($entries);
$data = [
'count' => 0,
'labels' => [],
'datasets' => [],
];
// labels: for each budget.
// dataset: for each year?
foreach($entries as $entry) {
$year = $entry['date']->year;
if(!in_array($year, $data['labels'])) {
$data['labels'][] = $entry['date']->year;
}
}
// can be joined?
$set = [];
foreach($entries as $entry) {
$name = $entry['budget'];
$set[$name] = isset($set[$name]) ? $set[$name] : [];
$set[$name][] = ($entry['sum'] * -1);
}
foreach($set as $name => $values) {
$data['datasets'][] = ['label' => $name, 'data' => $values];
}
$data['count'] = count($data['datasets']);
return $data;
//var_dump($data);
//exit;
}
}

View File

@ -46,6 +46,19 @@ class BudgetController extends Controller
*/
public function multiYear(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets)
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($report_type);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
$cache->addProperty($budgets);
$cache->addProperty('multiYearBudget');
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$currentStart = clone $start;
$collection = new Collection;
@ -53,7 +66,7 @@ class BudgetController extends Controller
$currentEnd = clone $currentStart;
$currentEnd->endOfYear();
echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '<br>';
//echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '<br>';
/** @var Budget $budget */
foreach ($budgets as $budget) {
@ -64,7 +77,8 @@ class BudgetController extends Controller
$name = $budget->name;
$sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts);
}
echo $name.': '.$sum.'<br>';
$collection->push(['budget' => $name, 'sum' => $sum,'date' => $currentStart]);
//echo $name . ': ' . $sum . '<br>';
}
// do something for all budgets.
@ -74,6 +88,12 @@ class BudgetController extends Controller
}
$data = $this->generator->multiYear($collection);
//$cache->store($data);
return Response::json($data);
}
/**

View File

@ -39,6 +39,9 @@ function updateBudgetChart(e) {
});
var budgetIds = budgets.join(',');
// remove old chart:
$('#budgets-chart').replaceWith('<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');
// draw chart. Redraw when exists? Not sure if we support that.
columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');