'label of set', * 'type' => bar or line, optional * 'yAxisID' => ID of yAxis, optional, will not be included when unused. * 'fill' => if to fill a line? optional, will not be included when unused. * 'entries' => * [ * 'label-of-entry' => 'value' * ] * ] * 1: [ * 'label' => 'label of another set', * 'type' => bar or line, optional * 'yAxisID' => ID of yAxis, optional, will not be included when unused. * 'fill' => if to fill a line? optional, will not be included when unused. * 'entries' => * [ * 'label-of-entry' => 'value' * ] * ] * * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five. * * @param array $data * * @return array */ public function multiSet(array $data): array { reset($data); $first = current($data); $labels = is_array($first['entries']) ? array_keys($first['entries']) : []; $chartData = [ 'count' => count($data), 'labels' => $labels, // take ALL labels from the first set. 'datasets' => [], ]; unset($first, $labels); foreach ($data as $set) { $currentSet = [ 'label' => $set['label'], 'type' => $set['type'] ?? 'line', 'data' => array_values($set['entries']), ]; if (isset($set['yAxisID'])) { $currentSet['yAxisID'] = $set['yAxisID']; } if (isset($set['fill'])) { $currentSet['fill'] = $set['fill']; } $chartData['datasets'][] = $currentSet; } 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'][] = $value; $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: * * '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; } }