Skip to content

Firefly III: a personal finances manager

Experimental bill chart [skip ci]

James Cole4403b65bae9f
diff --git a/app/Generator/Chart/Basic/ChartJsGenerator.php b/app/Generator/Chart/Basic/ChartJsGenerator.php
index a6709be0ff..5571599f44 100644
--- a/app/Generator/Chart/Basic/ChartJsGenerator.php
+++ b/app/Generator/Chart/Basic/ChartJsGenerator.php
@@ -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,33 +67,6 @@ class ChartJsGenerator implements GeneratorInterface
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:
*
@@ -102,13 +78,13 @@ class ChartJsGenerator implements GeneratorInterface
*/
public function pieChart(array $data): array
{
- $chartData = [
+ $chartData = [
'datasets' => [
0 => [],
],
'labels' => [],
];
- $index = 0;
+ $index = 0;
foreach ($data as $key => $value) {
// make larger than 0
@@ -124,4 +100,31 @@ class ChartJsGenerator implements GeneratorInterface
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;
+ }
}
\ No newline at end of file
diff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.php
index 85320c96f1..aa31009c9e 100644
--- a/app/Http/Controllers/Chart/BillController.php
+++ b/app/Http/Controllers/Chart/BillController.php
@@ -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 Bill $bill
+ * @param JournalCollectorInterface $collector
+ * @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);