Make some charts multi-currency.

This commit is contained in:
James Cole
2018-08-28 05:21:23 +02:00
parent 46136d94e9
commit e1c829f4fa
12 changed files with 196 additions and 18 deletions

View File

@@ -140,6 +140,46 @@ class ChartJsGenerator implements GeneratorInterface
return $chartData;
}
/**
* Expects data as:.
*
* key => [value => x, 'currency_symbol' => 'x']
*
* @param array $data
*
* @return array
*/
public function multiCurrencyPieChart(array $data): array
{
$chartData = [
'datasets' => [
0 => [],
],
'labels' => [],
];
$amounts = array_column($data, 'amount');
$next = next($amounts);
$sortFlag = SORT_ASC;
if (!\is_bool($next) && 1 === bccomp((string)$next, '0')) {
$sortFlag = SORT_DESC;
}
array_multisort($amounts, $sortFlag, $data);
unset($next, $sortFlag, $amounts);
$index = 0;
foreach ($data as $key => $valueArray) {
// make larger than 0
$chartData['datasets'][0]['data'][] = (float)app('steam')->positive((string)$valueArray['amount']);
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
$chartData['datasets'][0]['currency_symbol'][] = $valueArray['currency_symbol'];
$chartData['labels'][] = $key;
++$index;
}
return $chartData;
}
/**
* Will generate a (ChartJS) compatible array from the given input. Expects this format:.
*

View File

@@ -27,6 +27,13 @@ namespace FireflyIII\Generator\Chart\Basic;
*/
interface GeneratorInterface
{
/**
* @param array $data
*
* @return array
*/
public function multiCurrencyPieChart(array $data): array;
/**
* Will generate a Chart JS compatible array from the given input. Expects this format.
*