Clean up chart code.

This commit is contained in:
James Cole
2016-12-11 17:05:48 +01:00
parent 553e9270e5
commit 08c4542847
5 changed files with 123 additions and 326 deletions

View File

@@ -13,6 +13,8 @@ declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Basic;
use FireflyIII\Support\ChartColour;
/**
* Class ChartJsGenerator
*
@@ -88,4 +90,38 @@ class ChartJsGenerator implements GeneratorInterface
return $chartData;
}
/**
* Expects data as:
*
* key => value
*
* @param array $data
*
* @return array
*/
public function pieChart(array $data): array
{
$chartData = [
'datasets' => [
0 => [],
],
'labels' => [],
];
$index = 0;
foreach ($data as $key => $value) {
// make larger than 0
if (bccomp($value, '0') === -1) {
$value = bcmul($value, '-1');
}
$chartData['datasets'][0]['data'][] = round($value, 2);
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
$chartData['labels'][] = $key;
$index++;
}
return $chartData;
}
}

View File

@@ -20,6 +20,18 @@ namespace FireflyIII\Generator\Chart\Basic;
*/
interface GeneratorInterface
{
/**
* Expects data as:
*
* key => value
*
* @param array $data
*
* @return array
*/
public function pieChart(array $data): array;
/**
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
*