mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-26 02:40:43 -06:00
Experimental bill chart [skip ci]
This commit is contained in:
parent
a27d80d765
commit
4403b65bae
@ -28,6 +28,7 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
*
|
||||
* 0: [
|
||||
* 'label' => 'label of set',
|
||||
* 'type' => bar or line, optional
|
||||
* 'entries' =>
|
||||
* [
|
||||
* 'label-of-entry' => 'value'
|
||||
@ -35,6 +36,7 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
* ]
|
||||
* 1: [
|
||||
* 'label' => 'label of another set',
|
||||
* 'type' => bar or line, optional
|
||||
* 'entries' =>
|
||||
* [
|
||||
* 'label-of-entry' => 'value'
|
||||
@ -57,6 +59,7 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
foreach ($data as $set) {
|
||||
$chartData['datasets'][] = [
|
||||
'label' => $set['label'],
|
||||
'type' => $set['type'] ?? 'line',
|
||||
'data' => array_values($set['entries']),
|
||||
];
|
||||
}
|
||||
@ -64,6 +67,40 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
return $chartData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expects data as:
|
||||
*
|
||||
* key => value
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function pieChart(array $data): array
|
||||
{
|
||||
$chartData = [
|
||||
'datasets' => [
|
||||
0 => [],
|
||||
],
|
||||
'labels' => [],
|
||||
];
|
||||
$index = 0;
|
||||
foreach ($data as $key => $value) {
|
||||
|
||||
// make larger than 0
|
||||
if (bccomp($value, '0') === -1) {
|
||||
$value = bcmul($value, '-1');
|
||||
}
|
||||
|
||||
$chartData['datasets'][0]['data'][] = round($value, 2);
|
||||
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
|
||||
$chartData['labels'][] = $key;
|
||||
$index++;
|
||||
}
|
||||
|
||||
return $chartData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
|
||||
*
|
||||
@ -90,38 +127,4 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
|
||||
return $chartData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expects data as:
|
||||
*
|
||||
* key => value
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function pieChart(array $data): array
|
||||
{
|
||||
$chartData = [
|
||||
'datasets' => [
|
||||
0 => [],
|
||||
],
|
||||
'labels' => [],
|
||||
];
|
||||
$index = 0;
|
||||
foreach ($data as $key => $value) {
|
||||
|
||||
// make larger than 0
|
||||
if (bccomp($value, '0') === -1) {
|
||||
$value = bcmul($value, '-1');
|
||||
}
|
||||
|
||||
$chartData['datasets'][0]['data'][] = round($value, 2);
|
||||
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
|
||||
$chartData['labels'][] = $key;
|
||||
$index++;
|
||||
}
|
||||
|
||||
return $chartData;
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ namespace FireflyIII\Http\Controllers\Chart;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
|
||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\Transaction;
|
||||
@ -75,40 +75,65 @@ class BillController extends Controller
|
||||
/** @var GeneratorInterface $generator */
|
||||
$generator = app(GeneratorInterface::class);
|
||||
$data = $generator->pieChart($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the overview for a bill. The min/max amount and matched journals.
|
||||
* @param JournalCollectorInterface $collector
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function single(Bill $bill)
|
||||
public function single(JournalCollectorInterface $collector, Bill $bill)
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty('single');
|
||||
$cache->addProperty('bill');
|
||||
$cache->addProperty('chart.bill.single');
|
||||
$cache->addProperty($bill->id);
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get());
|
||||
}
|
||||
|
||||
// get first transaction or today for start:
|
||||
$collector = new JournalCollector(auth()->user());
|
||||
$collector->setAllAssetAccounts()->setBills(new Collection([$bill]));
|
||||
$results = $collector->getJournals();
|
||||
|
||||
// resort:
|
||||
$results = $collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->getJournals();
|
||||
$results = $results->sortBy(
|
||||
function (Transaction $transaction) {
|
||||
return $transaction->date->format('U');
|
||||
}
|
||||
);
|
||||
|
||||
$data = $this->generator->single($bill, $results);
|
||||
$chartData = [
|
||||
[
|
||||
'type' => 'bar',
|
||||
'label' => trans('firefly.min-amount'),
|
||||
'entries' => [],
|
||||
],
|
||||
[
|
||||
'type' => 'bar',
|
||||
'label' => trans('firefly.max-amount'),
|
||||
'entries' => [],
|
||||
],
|
||||
[
|
||||
'type' => 'line',
|
||||
'label' => trans('firefly.journal-amount'),
|
||||
'entries' => [],
|
||||
],
|
||||
];
|
||||
|
||||
/** @var Transaction $entry */
|
||||
foreach ($results as $entry) {
|
||||
$date = $entry->date->formatLocalized(strval(trans('config.month_and_day')));
|
||||
// minimum amount of bill:
|
||||
$chartData[0]['entries'][$date] = $bill->amount_min;
|
||||
// maximum amount of bill:
|
||||
$chartData[1]['entries'][$date] = $bill->amount_min;
|
||||
// amount of journal:
|
||||
$chartData[2]['entries'][$date] = bcmul($entry->transaction_amount, '-1');
|
||||
}
|
||||
|
||||
/** @var GeneratorInterface $generator */
|
||||
$generator = app(GeneratorInterface::class);
|
||||
$data = $generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
|
Loading…
Reference in New Issue
Block a user