mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-03 12:47:17 -06:00
140 lines
3.2 KiB
PHP
140 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace FireflyIII\Generator\Chart\Budget;
|
|
|
|
|
|
use Config;
|
|
use Illuminate\Support\Collection;
|
|
use Preferences;
|
|
|
|
/**
|
|
* Class ChartJsBudgetChartGenerator
|
|
*
|
|
* @package FireflyIII\Generator\Chart\Budget
|
|
*/
|
|
class ChartJsBudgetChartGenerator implements BudgetChartGenerator
|
|
{
|
|
|
|
/**
|
|
* @param Collection $entries
|
|
*
|
|
* @return array
|
|
*/
|
|
public function budget(Collection $entries)
|
|
{
|
|
$data = [
|
|
'count' => 1,
|
|
'labels' => [],
|
|
'datasets' => [
|
|
[
|
|
'label' => 'Amount',
|
|
'data' => [],
|
|
]
|
|
],
|
|
];
|
|
|
|
/** @var array $entry */
|
|
foreach ($entries as $entry) {
|
|
$data['labels'][] = trans('firefly.spent');
|
|
$data['datasets'][0]['data'][] = $entry[1];
|
|
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*
|
|
* @param Collection $entries
|
|
*
|
|
* @return array
|
|
*/
|
|
public function budgetLimit(Collection $entries)
|
|
{
|
|
return $this->budget($entries);
|
|
}
|
|
|
|
/**
|
|
* @param Collection $entries
|
|
*
|
|
* @return array
|
|
*/
|
|
public function frontpage(Collection $entries)
|
|
{
|
|
$data = [
|
|
'count' => 0,
|
|
'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) {
|
|
$data['labels'][] = $entry[0];
|
|
$left[] = round($entry[1], 2);
|
|
$spent[] = round($entry[2], 2);
|
|
$overspent[] = round($entry[3], 2);
|
|
}
|
|
}
|
|
|
|
$data['datasets'][] = [
|
|
'label' => trans('firefly.left'),
|
|
'data' => $left,
|
|
];
|
|
$data['datasets'][] = [
|
|
'label' => trans('firefly.spent'),
|
|
'data' => $spent,
|
|
];
|
|
$data['datasets'][] = [
|
|
'label' => trans('firefly.overspent'),
|
|
'data' => $overspent,
|
|
];
|
|
|
|
$data['count'] = count($data['datasets']);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param Collection $budgets
|
|
* @param Collection $entries
|
|
*
|
|
* @return array
|
|
*/
|
|
public function year(Collection $budgets, Collection $entries)
|
|
{
|
|
// language:
|
|
$language = Preferences::get('language', 'en')->data;
|
|
$format = Config::get('firefly.month.' . $language);
|
|
|
|
$data = [
|
|
'count' => 0,
|
|
'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;
|
|
|
|
}
|
|
$data['count'] = count($data['datasets']);
|
|
|
|
return $data;
|
|
}
|
|
}
|