Experimental bill chart [skip ci]

This commit is contained in:
James Cole
2016-12-11 17:46:30 +01:00
parent a27d80d765
commit 4403b65bae
2 changed files with 77 additions and 49 deletions

View File

@@ -28,6 +28,7 @@ class ChartJsGenerator implements GeneratorInterface
*
* 0: [
* 'label' => 'label of set',
* 'type' => bar or line, optional
* 'entries' =>
* [
* 'label-of-entry' => 'value'
@@ -35,6 +36,7 @@ class ChartJsGenerator implements GeneratorInterface
* ]
* 1: [
* 'label' => 'label of another set',
* 'type' => bar or line, optional
* 'entries' =>
* [
* 'label-of-entry' => 'value'
@@ -57,6 +59,7 @@ class ChartJsGenerator implements GeneratorInterface
foreach ($data as $set) {
$chartData['datasets'][] = [
'label' => $set['label'],
'type' => $set['type'] ?? 'line',
'data' => array_values($set['entries']),
];
}
@@ -64,6 +67,40 @@ 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;
}
/**
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
*
@@ -90,38 +127,4 @@ 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;
}
}