firefly-iii/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php

145 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace FireflyIII\Generator\Chart\Budget;
2015-06-27 13:39:50 -05:00
use Config;
use Illuminate\Support\Collection;
2015-06-27 13:39:50 -05:00
use Preferences;
2015-06-27 10:05:39 -05:00
/**
* Class ChartJsBudgetChartGenerator
*
* @package FireflyIII\Generator\Chart\Budget
*/
class ChartJsBudgetChartGenerator implements BudgetChartGenerator
{
/**
* @param Collection $entries
* @param string $dateFormat
*
* @return array
*/
public function budget(Collection $entries, $dateFormat = 'month')
{
// language:
$language = Preferences::get('language', 'en')->data;
$format = Config::get('firefly.' . $dateFormat . '.' . $language);
2015-06-27 13:39:50 -05:00
$data = [
'labels' => [],
'datasets' => [
[
'label' => 'Amount',
'data' => [],
]
],
];
/** @var array $entry */
foreach ($entries as $entry) {
$data['labels'][] = $entry[0]->formatLocalized($format);
2015-06-27 13:39:50 -05:00
$data['datasets'][0]['data'][] = $entry[1];
}
2015-07-06 10:45:59 -05:00
$data['count'] = count($data['datasets']);
2015-06-27 13:39:50 -05:00
return $data;
}
/**
2015-06-28 05:41:58 -05:00
* @codeCoverageIgnore
2015-06-28 11:00:11 -05:00
*
* @param Collection $entries
*
* @return array
*/
public function budgetLimit(Collection $entries)
{
return $this->budget($entries, 'monthAndDay');
}
/**
* @param Collection $entries
*
* @return array
*/
public function frontpage(Collection $entries)
{
2015-06-27 10:05:39 -05:00
$data = [
2015-06-28 11:00:11 -05:00
'count' => 0,
2015-06-27 10:05:39 -05:00
'labels' => [],
'datasets' => [],
];
// dataset: left
// dataset: spent
// dataset: overspent
$left = [];
$spent = [];
$overspent = [];
foreach ($entries as $entry) {
if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) {
2015-06-28 01:33:23 -05:00
$data['labels'][] = $entry[0];
$left[] = round($entry[1], 2);
$spent[] = round($entry[2], 2);
$overspent[] = round($entry[3], 2);
2015-06-27 10:05:39 -05:00
}
}
$data['datasets'][] = [
2015-06-27 15:11:03 -05:00
'label' => trans('firefly.left'),
'data' => $left,
2015-06-27 10:05:39 -05:00
];
$data['datasets'][] = [
2015-06-27 15:11:03 -05:00
'label' => trans('firefly.spent'),
'data' => $spent,
];
$data['datasets'][] = [
'label' => trans('firefly.overspent'),
2015-10-17 14:14:25 -05:00
'data' => $overspent * -1,
2015-06-27 10:05:39 -05:00
];
2015-06-27 15:11:03 -05:00
$data['count'] = count($data['datasets']);
2015-06-27 10:05:39 -05:00
return $data;
}
/**
* @param Collection $budgets
* @param Collection $entries
*
* @return array
*/
public function year(Collection $budgets, Collection $entries)
{
2015-06-27 13:39:50 -05:00
// language:
$language = Preferences::get('language', 'en')->data;
$format = Config::get('firefly.month.' . $language);
$data = [
'labels' => [],
'datasets' => [],
];
foreach ($budgets as $budget) {
$data['labels'][] = $budget->name;
}
/** @var array $entry */
foreach ($entries as $entry) {
$array = [
'label' => $entry[0]->formatLocalized($format),
'data' => [],
];
array_shift($entry);
$array['data'] = $entry;
$data['datasets'][] = $array;
}
2015-06-28 11:00:11 -05:00
$data['count'] = count($data['datasets']);
2015-06-27 13:39:50 -05:00
return $data;
}
2015-06-28 01:24:12 -05:00
}