mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 10:53:37 +00:00
More code for issue #452
This commit is contained in:
@@ -61,4 +61,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;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -21,7 +21,7 @@ namespace FireflyIII\Generator\Chart\Basic;
|
|||||||
interface GeneratorInterface
|
interface GeneratorInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Will generate a Chart JS compatible array from the given input. Expects this format:
|
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
|
||||||
*
|
*
|
||||||
* 0: [
|
* 0: [
|
||||||
* 'label' => 'label of set',
|
* 'label' => 'label of set',
|
||||||
@@ -45,4 +45,17 @@ interface GeneratorInterface
|
|||||||
*/
|
*/
|
||||||
public function multiSet(array $data): array;
|
public function multiSet(array $data): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
}
|
}
|
@@ -58,10 +58,8 @@ class AccountController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Account $account
|
* @param Account $account
|
||||||
* @param string $date
|
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
* @throws FireflyException
|
|
||||||
*/
|
*/
|
||||||
public function all(Account $account)
|
public function all(Account $account)
|
||||||
{
|
{
|
||||||
@@ -81,21 +79,20 @@ class AccountController extends Controller
|
|||||||
$range = Steam::balanceInRange($account, $start, $end);
|
$range = Steam::balanceInRange($account, $start, $end);
|
||||||
$current = clone $start;
|
$current = clone $start;
|
||||||
$previous = array_values($range)[0];
|
$previous = array_values($range)[0];
|
||||||
$labels = [];
|
|
||||||
$chartData = [];
|
$chartData = [];
|
||||||
|
|
||||||
while ($end >= $current) {
|
while ($end >= $current) {
|
||||||
$theDate = $current->format('Y-m-d');
|
$theDate = $current->format('Y-m-d');
|
||||||
$balance = $range[$theDate] ?? $previous;
|
$balance = $range[$theDate] ?? $previous;
|
||||||
|
$label = $current->formatLocalized($format);
|
||||||
$labels[] = $current->formatLocalized($format);
|
$chartData[$label] = $balance;
|
||||||
$chartData[] = $balance;
|
|
||||||
$previous = $balance;
|
$previous = $balance;
|
||||||
$current->addDay();
|
$current->addDay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var GeneratorInterface $generator */
|
||||||
$data = $this->generator->single($account, $labels, $chartData);
|
$generator = app(GeneratorInterface::class);
|
||||||
|
$data = $generator->singleSet($account->name, $chartData);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
@@ -470,16 +467,13 @@ class AccountController extends Controller
|
|||||||
return $cache->get();
|
return $cache->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
$chartData = [
|
$chartData = [];
|
||||||
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$currentSet = [
|
$currentSet = [
|
||||||
'label' => $account->name,
|
'label' => $account->name,
|
||||||
'entries' => [],
|
'entries' => [],
|
||||||
];
|
];
|
||||||
$balances = [];
|
|
||||||
$currentStart = 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);
|
||||||
@@ -488,13 +482,12 @@ class AccountController extends Controller
|
|||||||
$label = $currentStart->formatLocalized(strval(trans('config.month_and_day')));
|
$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;
|
|
||||||
$currentStart->addDay();
|
$currentStart->addDay();
|
||||||
$currentSet['entries'][$label] = $balance;
|
$currentSet['entries'][$label] = $balance;
|
||||||
}
|
}
|
||||||
$account->balances = $balances;
|
|
||||||
$chartData[] = $currentSet;
|
$chartData[] = $currentSet;
|
||||||
}
|
}
|
||||||
|
/** @var GeneratorInterface $generator */
|
||||||
$generator = app(GeneratorInterface::class);
|
$generator = app(GeneratorInterface::class);
|
||||||
$data = $generator->multiSet($chartData);
|
$data = $generator->multiSet($chartData);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
Reference in New Issue
Block a user