mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Experimental code for issue #452
This commit is contained in:
parent
74e01a52b9
commit
e3b2f2d9a8
64
app/Generator/Chart/Basic/ChartJsGenerator.php
Normal file
64
app/Generator/Chart/Basic/ChartJsGenerator.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ChartJsGenerator.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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Generator\Chart\Basic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ChartJsGenerator
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Generator\Chart\Basic
|
||||||
|
*/
|
||||||
|
class ChartJsGenerator implements GeneratorInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will generate a Chart JS compatible array from the given input. Expects this format:
|
||||||
|
*
|
||||||
|
* 0: [
|
||||||
|
* 'label' => 'label of set',
|
||||||
|
* 'entries' =>
|
||||||
|
* [
|
||||||
|
* 'label-of-entry' => 'value'
|
||||||
|
* ]
|
||||||
|
* ]
|
||||||
|
* 1: [
|
||||||
|
* 'label' => 'label of another set',
|
||||||
|
* 'entries' =>
|
||||||
|
* [
|
||||||
|
* 'label-of-entry' => 'value'
|
||||||
|
* ]
|
||||||
|
* ]
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function multiSet(array $data): array
|
||||||
|
{
|
||||||
|
$chartData = [
|
||||||
|
'count' => count($data),
|
||||||
|
'labels' => array_keys($data[0]['entries']), // take ALL labels from the first set.
|
||||||
|
'datasets' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($data as $set) {
|
||||||
|
$chartData['datasets'][] = [
|
||||||
|
'label' => $set['label'],
|
||||||
|
'data' => array_values($set['entries']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $chartData;
|
||||||
|
}
|
||||||
|
}
|
48
app/Generator/Chart/Basic/GeneratorInterface.php
Normal file
48
app/Generator/Chart/Basic/GeneratorInterface.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* GeneratorInterface.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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Generator\Chart\Basic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface GeneratorInterface
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Generator\Chart\Basic
|
||||||
|
*/
|
||||||
|
interface GeneratorInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Will generate a Chart JS compatible array from the given input. Expects this format:
|
||||||
|
*
|
||||||
|
* 0: [
|
||||||
|
* 'label' => 'label of set',
|
||||||
|
* 'entries' =>
|
||||||
|
* [
|
||||||
|
* 'label-of-entry' => 'value'
|
||||||
|
* ]
|
||||||
|
* ]
|
||||||
|
* 1: [
|
||||||
|
* 'label' => 'label of another set',
|
||||||
|
* 'entries' =>
|
||||||
|
* [
|
||||||
|
* 'label-of-entry' => 'value'
|
||||||
|
* ]
|
||||||
|
* ]
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function multiSet(array $data): array;
|
||||||
|
|
||||||
|
}
|
@ -17,6 +17,7 @@ use Carbon\Carbon;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
|
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
|
||||||
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
@ -469,21 +470,33 @@ class AccountController extends Controller
|
|||||||
return $cache->get();
|
return $cache->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$chartData = [
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
|
$currentSet = [
|
||||||
|
'label' => $account->name,
|
||||||
|
'entries' => [],
|
||||||
|
];
|
||||||
$balances = [];
|
$balances = [];
|
||||||
$current = clone $start;
|
$currentStart = clone $start;
|
||||||
$range = Steam::balanceInRange($account, $start, clone $end);
|
$range = Steam::balanceInRange($account, $start, clone $end);
|
||||||
$previous = round(array_values($range)[0], 2);
|
$previous = round(array_values($range)[0], 2);
|
||||||
while ($current <= $end) {
|
while ($currentStart <= $end) {
|
||||||
$format = $current->format('Y-m-d');
|
$format = $currentStart->format('Y-m-d');
|
||||||
|
$label = $currentStart->formatLocalized(strval(trans('config.month_and_day')));
|
||||||
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
|
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
|
||||||
$previous = $balance;
|
$previous = $balance;
|
||||||
$balances[] = $balance;
|
$balances[] = $balance;
|
||||||
$current->addDay();
|
$currentStart->addDay();
|
||||||
|
$currentSet['entries'][$label] = $balance;
|
||||||
}
|
}
|
||||||
$account->balances = $balances;
|
$account->balances = $balances;
|
||||||
|
$chartData[] = $currentSet;
|
||||||
}
|
}
|
||||||
$data = $this->generator->frontpage($accounts, $start, $end);
|
$generator = app(GeneratorInterface::class);
|
||||||
|
$data = $generator->multiSet($chartData);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
@ -93,6 +93,9 @@ class FireflyServiceProvider extends ServiceProvider
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// chart generator:
|
||||||
|
$this->app->bind('FireflyIII\Generator\Chart\Basic\GeneratorInterface', 'FireflyIII\Generator\Chart\Basic\ChartJsGenerator');
|
||||||
|
|
||||||
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
|
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
|
||||||
$this->app->bind('FireflyIII\Repositories\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
|
$this->app->bind('FireflyIII\Repositories\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
|
||||||
$this->app->bind('FireflyIII\Helpers\Attachments\AttachmentHelperInterface', 'FireflyIII\Helpers\Attachments\AttachmentHelper');
|
$this->app->bind('FireflyIII\Helpers\Attachments\AttachmentHelperInterface', 'FireflyIII\Helpers\Attachments\AttachmentHelper');
|
||||||
|
Loading…
Reference in New Issue
Block a user