firefly-iii/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php

95 lines
2.6 KiB
PHP
Raw Normal View History

2015-06-27 10:32:52 -05:00
<?php
/**
* ChartJsBillChartGenerator.php
2016-04-01 09:44:46 -05:00
* 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.
*/
2015-06-27 10:32:52 -05:00
2016-05-20 04:59:54 -05:00
declare(strict_types = 1);
2015-06-27 10:32:52 -05:00
namespace FireflyIII\Generator\Chart\Bill;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
2016-11-11 13:52:48 -06:00
use FireflyIII\Support\ChartColour;
2015-06-27 10:32:52 -05:00
use Illuminate\Support\Collection;
/**
* Class ChartJsBillChartGenerator
*
* @package FireflyIII\Generator\Chart\Bill
*/
class ChartJsBillChartGenerator implements BillChartGeneratorInterface
2015-06-27 10:32:52 -05:00
{
/**
* @param string $paid
* @param string $unpaid
2015-06-27 10:32:52 -05:00
*
* @return array
*/
2016-02-18 00:21:48 -06:00
public function frontpage(string $paid, string $unpaid): array
2015-06-27 10:32:52 -05:00
{
$data = [
2016-04-10 03:05:50 -05:00
'datasets' => [
[
'data' => [round($unpaid, 2), round(bcmul($paid, '-1'), 2)],
2016-11-11 13:52:48 -06:00
'backgroundColor' => [ChartColour::getColour(0), ChartColour::getColour(1)],
2016-04-10 03:05:50 -05:00
],
2016-01-15 16:12:52 -06:00
],
2016-04-10 03:05:50 -05:00
'labels' => [strval(trans('firefly.unpaid')), strval(trans('firefly.paid'))],
2015-06-27 10:32:52 -05:00
];
return $data;
}
/**
* @param Bill $bill
* @param Collection $entries
*
* @return array
*/
2016-02-18 00:21:48 -06:00
public function single(Bill $bill, Collection $entries): array
2015-06-27 10:32:52 -05:00
{
$format = (string)trans('config.month');
2016-04-26 14:40:15 -05:00
$data = ['count' => 3, 'labels' => [], 'datasets' => [],];
2015-06-28 05:41:58 -05:00
$minAmount = [];
$maxAmount = [];
$actualAmount = [];
/** @var Transaction $entry */
2015-06-28 05:41:58 -05:00
foreach ($entries as $entry) {
$data['labels'][] = $entry->date->formatLocalized($format);
$minAmount[] = round($bill->amount_min, 2);
$maxAmount[] = round($bill->amount_max, 2);
2016-04-26 14:40:15 -05:00
// journalAmount has been collected in BillRepository::getJournals
$actualAmount[] = bcmul($entry->transaction_amount, '-1');
2015-06-28 05:41:58 -05:00
}
$data['datasets'][] = [
2016-04-10 10:03:36 -05:00
'type' => 'bar',
2015-06-28 05:41:58 -05:00
'label' => trans('firefly.minAmount'),
'data' => $minAmount,
];
$data['datasets'][] = [
2016-04-10 10:03:36 -05:00
'type' => 'line',
2015-06-28 05:41:58 -05:00
'label' => trans('firefly.billEntry'),
'data' => $actualAmount,
];
$data['datasets'][] = [
2016-04-10 10:03:36 -05:00
'type' => 'bar',
2015-06-28 05:41:58 -05:00
'label' => trans('firefly.maxAmount'),
'data' => $maxAmount,
];
$data['count'] = count($data['datasets']);
return $data;
2015-06-27 10:32:52 -05:00
}
2015-06-28 01:24:12 -05:00
}