mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-29 14:42:59 +00:00
Experimental bill chart [skip ci]
This commit is contained in:
@@ -28,6 +28,7 @@ class ChartJsGenerator implements GeneratorInterface
|
|||||||
*
|
*
|
||||||
* 0: [
|
* 0: [
|
||||||
* 'label' => 'label of set',
|
* 'label' => 'label of set',
|
||||||
|
* 'type' => bar or line, optional
|
||||||
* 'entries' =>
|
* 'entries' =>
|
||||||
* [
|
* [
|
||||||
* 'label-of-entry' => 'value'
|
* 'label-of-entry' => 'value'
|
||||||
@@ -35,6 +36,7 @@ class ChartJsGenerator implements GeneratorInterface
|
|||||||
* ]
|
* ]
|
||||||
* 1: [
|
* 1: [
|
||||||
* 'label' => 'label of another set',
|
* 'label' => 'label of another set',
|
||||||
|
* 'type' => bar or line, optional
|
||||||
* 'entries' =>
|
* 'entries' =>
|
||||||
* [
|
* [
|
||||||
* 'label-of-entry' => 'value'
|
* 'label-of-entry' => 'value'
|
||||||
@@ -57,6 +59,7 @@ class ChartJsGenerator implements GeneratorInterface
|
|||||||
foreach ($data as $set) {
|
foreach ($data as $set) {
|
||||||
$chartData['datasets'][] = [
|
$chartData['datasets'][] = [
|
||||||
'label' => $set['label'],
|
'label' => $set['label'],
|
||||||
|
'type' => $set['type'] ?? 'line',
|
||||||
'data' => array_values($set['entries']),
|
'data' => array_values($set['entries']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -64,33 +67,6 @@ class ChartJsGenerator implements GeneratorInterface
|
|||||||
return $chartData;
|
return $chartData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
|
|
||||||
*
|
|
||||||
* 'label-of-entry' => value
|
|
||||||
* 'label-of-entry' => value
|
|
||||||
*
|
|
||||||
* @param string $setLabel
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function singleSet(string $setLabel, array $data): array
|
|
||||||
{
|
|
||||||
$chartData = [
|
|
||||||
'count' => 1,
|
|
||||||
'labels' => array_keys($data), // take ALL labels from the first set.
|
|
||||||
'datasets' => [
|
|
||||||
[
|
|
||||||
'label' => $setLabel,
|
|
||||||
'data' => array_values($data),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
return $chartData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expects data as:
|
* Expects data as:
|
||||||
*
|
*
|
||||||
@@ -124,4 +100,31 @@ class ChartJsGenerator implements GeneratorInterface
|
|||||||
|
|
||||||
return $chartData;
|
return $chartData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
|
||||||
|
*
|
||||||
|
* 'label-of-entry' => value
|
||||||
|
* 'label-of-entry' => value
|
||||||
|
*
|
||||||
|
* @param string $setLabel
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function singleSet(string $setLabel, array $data): array
|
||||||
|
{
|
||||||
|
$chartData = [
|
||||||
|
'count' => 1,
|
||||||
|
'labels' => array_keys($data), // take ALL labels from the first set.
|
||||||
|
'datasets' => [
|
||||||
|
[
|
||||||
|
'label' => $setLabel,
|
||||||
|
'data' => array_values($data),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return $chartData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,7 @@ namespace FireflyIII\Http\Controllers\Chart;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||||
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
|
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
|
||||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
@@ -75,40 +75,65 @@ class BillController extends Controller
|
|||||||
/** @var GeneratorInterface $generator */
|
/** @var GeneratorInterface $generator */
|
||||||
$generator = app(GeneratorInterface::class);
|
$generator = app(GeneratorInterface::class);
|
||||||
$data = $generator->pieChart($chartData);
|
$data = $generator->pieChart($chartData);
|
||||||
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($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 = new CacheProperties;
|
||||||
$cache->addProperty('single');
|
$cache->addProperty('chart.bill.single');
|
||||||
$cache->addProperty('bill');
|
|
||||||
$cache->addProperty($bill->id);
|
$cache->addProperty($bill->id);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get());
|
return Response::json($cache->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get first transaction or today for start:
|
$results = $collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->getJournals();
|
||||||
$collector = new JournalCollector(auth()->user());
|
|
||||||
$collector->setAllAssetAccounts()->setBills(new Collection([$bill]));
|
|
||||||
$results = $collector->getJournals();
|
|
||||||
|
|
||||||
// resort:
|
|
||||||
$results = $results->sortBy(
|
$results = $results->sortBy(
|
||||||
function (Transaction $transaction) {
|
function (Transaction $transaction) {
|
||||||
return $transaction->date->format('U');
|
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);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
|
|||||||
Reference in New Issue
Block a user