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

137 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2016-05-20 11:59:54 +02:00
/**
* ChartJsBudgetChartGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
2016-05-20 11:59:54 +02:00
*/
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Budget;
use Illuminate\Support\Collection;
2015-06-27 17:05:39 +02:00
/**
* Class ChartJsBudgetChartGenerator
*
* @package FireflyIII\Generator\Chart\Budget
*/
class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
{
/**
2016-04-24 20:00:20 +02:00
*
* @param Collection $entries
* @param string $dateFormat
*
* @return array
*/
2016-05-22 15:48:34 +02:00
public function budgetLimit(Collection $entries, string $dateFormat = 'month_and_day'): array
{
2016-05-22 15:48:34 +02:00
$format = strval(trans('config.' . $dateFormat));
$data = [
2015-06-27 20:39:50 +02:00
'labels' => [],
'datasets' => [
[
'label' => 'Amount',
'data' => [],
2015-12-24 08:35:08 +01:00
],
2015-06-27 20:39:50 +02:00
],
];
/** @var array $entry */
foreach ($entries as $entry) {
$data['labels'][] = $entry[0]->formatLocalized($format);
2015-06-27 20:39:50 +02:00
$data['datasets'][0]['data'][] = $entry[1];
}
2015-07-06 17:45:59 +02:00
$data['count'] = count($data['datasets']);
2015-06-27 20:39:50 +02:00
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
2016-02-18 07:21:48 +01:00
public function frontpage(Collection $entries): array
{
2016-01-02 16:57:31 +01:00
$data = [
2015-06-28 18:00:11 +02:00
'count' => 0,
2015-06-27 17:05:39 +02:00
'labels' => [],
'datasets' => [],
];
$left = [];
$spent = [];
$overspent = [];
2016-01-02 16:57:31 +01:00
$filtered = $entries->filter(
function ($entry) {
return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0);
2015-06-27 17:05:39 +02:00
}
2016-01-02 16:57:31 +01:00
);
foreach ($filtered as $entry) {
$data['labels'][] = $entry[0];
$left[] = round($entry[1], 2);
2016-02-05 15:41:40 +01:00
$spent[] = round(bcmul($entry[2], '-1'), 2); // spent is coming in negative, must be positive
$overspent[] = round(bcmul($entry[3], '-1'), 2); // same
2015-06-27 17:05:39 +02:00
}
$data['datasets'][] = [
2016-04-17 19:58:09 +02:00
'label' => trans('firefly.overspent'),
'data' => $overspent,
2015-06-27 17:05:39 +02:00
];
2015-06-27 22:11:03 +02:00
$data['datasets'][] = [
2016-04-17 19:58:09 +02:00
'label' => trans('firefly.left'),
'data' => $left,
2015-06-27 17:05:39 +02:00
];
2016-04-17 19:59:15 +02:00
$data['datasets'][] = [
'label' => trans('firefly.spent'),
'data' => $spent,
];
2015-06-27 17:05:39 +02:00
2016-04-17 19:58:09 +02:00
$data['count'] = 3;
2015-06-27 22:11:03 +02:00
2015-06-27 17:05:39 +02:00
return $data;
}
2016-04-24 20:23:17 +02:00
/**
2016-11-19 13:37:44 +01:00
* @param array $entries
2016-04-24 20:23:17 +02:00
*
* @return array
*/
2016-11-19 13:37:44 +01:00
public function period(array $entries) : array
2016-04-24 20:23:17 +02:00
{
2016-11-19 13:37:44 +01:00
2016-04-24 20:23:17 +02:00
$data = [
2016-11-19 13:37:44 +01:00
'labels' => array_keys($entries),
2016-04-24 20:23:17 +02:00
'datasets' => [
0 => [
'label' => trans('firefly.budgeted'),
'data' => [],
],
1 => [
'label' => trans('firefly.spent'),
'data' => [],
],
],
'count' => 2,
];
2016-11-19 13:37:44 +01:00
foreach ($entries as $label => $entry) {
2016-04-24 20:23:17 +02:00
// data set 0 is budgeted
// data set 1 is spent:
$data['datasets'][0]['data'][] = $entry['budgeted'];
$data['datasets'][1]['data'][] = round(($entry['spent'] * -1), 2);
}
return $data;
}
2015-06-28 08:24:12 +02:00
}