Experimental code for issue #452

This commit is contained in:
James Cole 2016-12-11 16:02:04 +01:00
parent 74e01a52b9
commit e3b2f2d9a8
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
4 changed files with 136 additions and 8 deletions

View 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;
}
}

View 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;
}

View File

@ -17,6 +17,7 @@ use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
@ -469,21 +470,33 @@ class AccountController extends Controller
return $cache->get();
}
$chartData = [
];
foreach ($accounts as $account) {
$balances = [];
$current = clone $start;
$range = Steam::balanceInRange($account, $start, clone $end);
$previous = round(array_values($range)[0], 2);
while ($current <= $end) {
$format = $current->format('Y-m-d');
$currentSet = [
'label' => $account->name,
'entries' => [],
];
$balances = [];
$currentStart = clone $start;
$range = Steam::balanceInRange($account, $start, clone $end);
$previous = round(array_values($range)[0], 2);
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->formatLocalized(strval(trans('config.month_and_day')));
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
$previous = $balance;
$balances[] = $balance;
$current->addDay();
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
}
$account->balances = $balances;
$chartData[] = $currentSet;
}
$data = $this->generator->frontpage($accounts, $start, $end);
$generator = app(GeneratorInterface::class);
$data = $generator->multiSet($chartData);
$cache->store($data);
return $data;

View File

@ -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\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
$this->app->bind('FireflyIII\Helpers\Attachments\AttachmentHelperInterface', 'FireflyIII\Helpers\Attachments\AttachmentHelper');