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)
|
public function multiYear(Collection $entries)
|
||||||
{
|
{
|
||||||
//var_dump($entries);
|
// dataset:
|
||||||
$data = [
|
$data = [
|
||||||
'count' => 0,
|
'count' => 0,
|
||||||
'labels' => [],
|
'labels' => [],
|
||||||
'datasets' => [],
|
'datasets' => [],
|
||||||
];
|
];
|
||||||
// labels: for each budget.
|
// get labels from one of the budgets (assuming there's at least one):
|
||||||
// dataset: for each year?
|
$first = $entries->first();
|
||||||
foreach($entries as $entry) {
|
foreach ($first['budgeted'] as $year => $noInterest) {
|
||||||
$year = $entry['date']->year;
|
$data['labels'][] = strval($year);
|
||||||
if(!in_array($year, $data['labels'])) {
|
|
||||||
$data['labels'][] = $entry['date']->year;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// can be joined?
|
|
||||||
$set = [];
|
// then, loop all entries and create datasets:
|
||||||
foreach($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$name = $entry['budget'];
|
$name = $entry['name'];
|
||||||
$set[$name] = isset($set[$name]) ? $set[$name] : [];
|
$spent = $entry['spent'];
|
||||||
$set[$name][] = ($entry['sum'] * -1);
|
$budgeted = $entry['budgeted'];
|
||||||
}
|
$data['datasets'][] = ['label' => 'Spent on ' . $name, 'data' => $spent];
|
||||||
foreach($set as $name => $values) {
|
$data['datasets'][] = ['label' => 'Budgeted for ' . $name, 'data' => $budgeted];
|
||||||
$data['datasets'][] = ['label' => $name, 'data' => $values];
|
|
||||||
}
|
}
|
||||||
$data['count'] = count($data['datasets']);
|
$data['count'] = count($data['datasets']);
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
//var_dump($data);
|
|
||||||
//exit;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,41 +56,54 @@ class BudgetController extends Controller
|
|||||||
$cache->addProperty('multiYearBudget');
|
$cache->addProperty('multiYearBudget');
|
||||||
|
|
||||||
if ($cache->has()) {
|
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) {
|
$currentStart = clone $start;
|
||||||
$currentEnd = clone $currentStart;
|
while ($currentStart < $end) {
|
||||||
$currentEnd->endOfYear();
|
// fix the date:
|
||||||
|
$currentEnd = clone $currentStart;
|
||||||
|
$currentEnd->endOfYear();
|
||||||
|
|
||||||
//echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '<br>';
|
// get data:
|
||||||
|
|
||||||
/** @var Budget $budget */
|
|
||||||
foreach ($budgets as $budget) {
|
|
||||||
if (is_null($budget->id)) {
|
if (is_null($budget->id)) {
|
||||||
$name = trans('firefly.noBudget');
|
$name = trans('firefly.noBudget');
|
||||||
$sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);
|
$sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);
|
||||||
|
$budgeted = 0;
|
||||||
} else {
|
} else {
|
||||||
$name = $budget->name;
|
$name = $budget->name;
|
||||||
$sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts);
|
$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.
|
$entries->push($entry);
|
||||||
|
|
||||||
// jump to next year.
|
|
||||||
$currentStart = clone $currentEnd;
|
|
||||||
$currentStart->addDay();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// generate chart with data:
|
||||||
$data = $this->generator->multiYear($collection);
|
$data = $this->generator->multiYear($entries);
|
||||||
|
|
||||||
//$cache->store($data);
|
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ function drawChart() {
|
|||||||
|
|
||||||
// draw budget chart based on selected budgets:
|
// draw budget chart based on selected budgets:
|
||||||
$('.budget-checkbox').on('change', updateBudgetChart);
|
$('.budget-checkbox').on('change', updateBudgetChart);
|
||||||
|
updateBudgetChart();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -37,12 +38,24 @@ function updateBudgetChart(e) {
|
|||||||
budgets.push(current.val());
|
budgets.push(current.val());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var budgetIds = budgets.join(',');
|
|
||||||
|
|
||||||
// remove old chart:
|
if(budgets.length > 0) {
|
||||||
$('#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.
|
var budgetIds = budgets.join(',');
|
||||||
columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');
|
|
||||||
|
// 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>
|
</div>
|
||||||
|
|
||||||
{% for account in accounts %}
|
{% 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="col-lg-12 col-md-12 col-sm-12">
|
||||||
<div class="box">
|
<div class="box">
|
||||||
<div class="box-header with-border">
|
<div class="box-header with-border">
|
||||||
@ -53,6 +53,9 @@
|
|||||||
<div class="box-body">
|
<div class="box-body">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-12">
|
<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>
|
<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user