mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Second attempt.
This commit is contained in:
parent
838330b909
commit
56715556ed
@ -149,35 +149,29 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator
|
||||
*/
|
||||
public function multiYear(Collection $entries)
|
||||
{
|
||||
//var_dump($entries);
|
||||
// dataset:
|
||||
$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;
|
||||
}
|
||||
// get labels from one of the budgets (assuming there's at least one):
|
||||
$first = $entries->first();
|
||||
foreach ($first['budgeted'] as $year => $noInterest) {
|
||||
$data['labels'][] = strval($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];
|
||||
|
||||
// then, loop all entries and create datasets:
|
||||
foreach ($entries as $entry) {
|
||||
$name = $entry['name'];
|
||||
$spent = $entry['spent'];
|
||||
$budgeted = $entry['budgeted'];
|
||||
$data['datasets'][] = ['label' => 'Spent on ' . $name, 'data' => $spent];
|
||||
$data['datasets'][] = ['label' => 'Budgeted for ' . $name, 'data' => $budgeted];
|
||||
}
|
||||
$data['count'] = count($data['datasets']);
|
||||
|
||||
return $data;
|
||||
//var_dump($data);
|
||||
//exit;
|
||||
return $data;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -56,41 +56,54 @@ class BudgetController extends Controller
|
||||
$cache->addProperty('multiYearBudget');
|
||||
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||
// return Response::json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$currentStart = clone $start;
|
||||
$collection = new Collection;
|
||||
/**
|
||||
* budget
|
||||
* year:
|
||||
* spent: x
|
||||
* budgeted: x
|
||||
* year
|
||||
* spent: x
|
||||
* budgeted: x
|
||||
*/
|
||||
$entries = new Collection;
|
||||
// go by budget, not by year.
|
||||
foreach ($budgets as $budget) {
|
||||
$entry = ['name' => '', 'spent' => [], 'budgeted' => []];
|
||||
|
||||
while ($currentStart < $end) {
|
||||
$currentEnd = clone $currentStart;
|
||||
$currentEnd->endOfYear();
|
||||
$currentStart = clone $start;
|
||||
while ($currentStart < $end) {
|
||||
// fix the date:
|
||||
$currentEnd = clone $currentStart;
|
||||
$currentEnd->endOfYear();
|
||||
|
||||
//echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '<br>';
|
||||
|
||||
/** @var Budget $budget */
|
||||
foreach ($budgets as $budget) {
|
||||
// get data:
|
||||
if (is_null($budget->id)) {
|
||||
$name = trans('firefly.noBudget');
|
||||
$sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);
|
||||
$name = trans('firefly.noBudget');
|
||||
$sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);
|
||||
$budgeted = 0;
|
||||
} else {
|
||||
$name = $budget->name;
|
||||
$sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts);
|
||||
$name = $budget->name;
|
||||
$sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts);
|
||||
$budgeted = $repository->getBudgetLimitRepetitions($budget, $currentStart, $currentEnd)->sum('amount');
|
||||
}
|
||||
$collection->push(['budget' => $name, 'sum' => $sum,'date' => $currentStart]);
|
||||
//echo $name . ': ' . $sum . '<br>';
|
||||
|
||||
// save to array:
|
||||
$year = $currentStart->year;
|
||||
$entry['name'] = $name;
|
||||
$entry['spent'][$year] = ($sum * -1);
|
||||
$entry['budgeted'][$year] = $budgeted;
|
||||
|
||||
// jump to next year.
|
||||
$currentStart = clone $currentEnd;
|
||||
$currentStart->addDay();
|
||||
}
|
||||
// do something for all budgets.
|
||||
|
||||
// jump to next year.
|
||||
$currentStart = clone $currentEnd;
|
||||
$currentStart->addDay();
|
||||
|
||||
$entries->push($entry);
|
||||
}
|
||||
|
||||
$data = $this->generator->multiYear($collection);
|
||||
|
||||
//$cache->store($data);
|
||||
// generate chart with data:
|
||||
$data = $this->generator->multiYear($entries);
|
||||
|
||||
return Response::json($data);
|
||||
|
||||
|
@ -23,6 +23,7 @@ function drawChart() {
|
||||
|
||||
// draw budget chart based on selected budgets:
|
||||
$('.budget-checkbox').on('change', updateBudgetChart);
|
||||
updateBudgetChart();
|
||||
|
||||
|
||||
}
|
||||
@ -37,12 +38,24 @@ function updateBudgetChart(e) {
|
||||
budgets.push(current.val());
|
||||
}
|
||||
});
|
||||
var budgetIds = budgets.join(',');
|
||||
|
||||
// remove old chart:
|
||||
$('#budgets-chart').replaceWith('<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');
|
||||
if(budgets.length > 0) {
|
||||
|
||||
// draw chart. Redraw when exists? Not sure if we support that.
|
||||
columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');
|
||||
var budgetIds = budgets.join(',');
|
||||
|
||||
// remove old chart:
|
||||
$('#budgets-chart').replaceWith('<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');
|
||||
|
||||
// hide message:
|
||||
$('#budgets-chart-message').hide();
|
||||
|
||||
// draw chart. Redraw when exists? Not sure if we support that.
|
||||
columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');
|
||||
} else {
|
||||
// hide canvas, show message:
|
||||
$('#budgets-chart-message').show();
|
||||
$('#budgets-chart').hide();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -30,7 +30,7 @@
|
||||
</div>
|
||||
|
||||
{% for account in accounts %}
|
||||
<div class="row">
|
||||
<div class="row" style="display:none;">
|
||||
<div class="col-lg-12 col-md-12 col-sm-12">
|
||||
<div class="box">
|
||||
<div class="box-header with-border">
|
||||
@ -53,6 +53,9 @@
|
||||
<div class="box-body">
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
<p class="well" id="budgets-chart-message" style="display:none;">
|
||||
Select one or more budgets to generate this chart. Insofar possible, your selection will be saved.
|
||||
</p>
|
||||
<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user