private function Highcharts::populateData in Charts 8.4
Same name and namespace in other branches
- 5.0.x modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php \Drupal\charts_highcharts\Plugin\chart\Library\Highcharts::populateData()
Utility to populate data.
Parameters
array $element: The element.
array $chart_definition: The chart definition.
Return value
array Return the chart definition.
1 call to Highcharts::populateData()
- Highcharts::preRender in modules/
charts_highcharts/ src/ Plugin/ chart/ Library/ Highcharts.php - Pre render.
File
- modules/
charts_highcharts/ src/ Plugin/ chart/ Library/ Highcharts.php, line 303
Class
- Highcharts
- Defines a concrete class for a Highcharts.
Namespace
Drupal\charts_highcharts\Plugin\chart\LibraryCode
private function populateData(array &$element, array $chart_definition) {
/** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
$element_info = \Drupal::service('element_info');
$categories = [];
foreach (Element::children($element) as $key) {
if ($element[$key]['#type'] === 'chart_xaxis') {
$categories[] = $element[$key]['#labels'];
}
}
foreach (Element::children($element) as $key) {
if ($element[$key]['#type'] === 'chart_data') {
$series = [];
$series_data = [];
// Make sure defaults are loaded.
if (empty($element[$key]['#defaults_loaded'])) {
$element[$key] += $element_info
->getInfo($element[$key]['#type']);
}
// Convert target named axis keys to integers.
if (isset($element[$key]['#target_axis'])) {
$axis_name = $element[$key]['#target_axis'];
$axis_index = 0;
foreach (Element::children($element) as $axis_key) {
if ($element[$axis_key]['#type'] === 'chart_yaxis') {
if ($axis_key === $axis_name) {
break;
}
$axis_index++;
}
}
$series['yAxis'] = $axis_index;
}
// Allow data to provide the labels. This will override the axis.
// settings.
if ($element[$key]['#labels'] && $element[$key]['#chart_type'] !== 'scatter') {
foreach ($element[$key]['#labels'] as $label_index => $label) {
$series_data[$label_index][0] = $label;
}
}
// Populate the data.
foreach ($element[$key]['#data'] as $data_index => $data) {
if (isset($series_data[$data_index])) {
$series_data[$data_index][] = $data;
}
else {
$series_data[$data_index] = $data;
}
}
$series['type'] = $element[$key]['#chart_type'];
if ($element['#chart_type'] === 'donut') {
// Add innerSize to differentiate between donut and pie.
$series['innerSize'] = '40%';
}
$series['name'] = $element[$key]['#title'];
$series['color'] = $element[$key]['#color'];
// $series['marker']['radius'] = $element[$key]['#marker_radius'];
// $series['showInLegend'] = $element[$key]['#show_in_legend'];
// $series['connectNulls'] = TRUE;
// $series['tooltip']['valueDecimals'] = $element[$key].
// ['#decimal_count'];
// $series['tooltip']['xDateFormat'] = $element[$key]['#date_format'];
// $series['tooltip']['valuePrefix'] = $element[$key]['#prefix'];
// $series['tooltip']['valueSuffix'] = $element[$key]['#suffix'];
if ($element[$key]['#prefix'] || $element[$key]['#suffix']) {
$yaxis_index = isset($series['yAxis']) ? $series['yAxis'] : 0;
// For axis formatting, we need to use a format string.
// See http://docs.highcharts.com/#formatting.
$decimal_formatting = $element[$key]['#decimal_count'] ? ':.' . $element[$key]['#decimal_count'] . 'f' : '';
$chart_definition['yAxis'][$yaxis_index]['labels']['format'] = $element[$key]['#prefix'] . "{value{$decimal_formatting}}" . $element[$key]['#suffix'];
}
// Remove unnecessary keys to trim down the resulting JS settings.
ChartElement::trimArray($series);
// If you want a different type of scatter.
if (!empty($element['#alternative_scatter'])) {
$series = $series_data;
}
else {
$series['data'] = $series_data;
}
// Merge in series raw options.
if (!empty($element[$key]['#raw_options'])) {
$series = NestedArray::mergeDeepArray([
$element[$key]['#raw_options'],
$series,
]);
}
// Add the series to the main chart definition.
// Scatter colors adjustment.
if (!empty($element['#alternative_scatter'])) {
$chart_definition['series'] = $series;
}
else {
$chart_definition['series'][$key] = $series;
}
// Merge in any point-specific data points.
foreach (Element::children($element[$key]) as $sub_key) {
if ($element[$key][$sub_key]['#type'] === 'chart_data_item') {
// Make sure defaults are loaded.
if (empty($element[$key][$sub_key]['#defaults_loaded'])) {
$element[$key][$sub_key] += $element_info
->getInfo($element[$key][$sub_key]['#type']);
}
$data_item = $element[$key][$sub_key];
$series_point =& $chart_definition['series'][$key]['data'][$sub_key];
// Convert the point from a simple data value to a complex point.
if (!isset($series_point['data'])) {
$data = $series_point;
$series_point = [];
if (is_array($data)) {
$series_point['name'] = $data[0];
$series_point['y'] = $data[1];
}
else {
$series_point['y'] = $data;
}
}
if (isset($data_item['#data'])) {
if (is_array($data_item['#data'])) {
$series_point['x'] = $data_item['#data'][0];
$series_point['y'] = $data_item['#data'][1];
}
else {
$series_point['y'] = $data_item['#data'];
}
}
if ($data_item['#title']) {
$series_point['name'] = $data_item['#title'];
}
// Setting the color requires several properties for consistency.
$series_point['color'] = $data_item['#color'];
$series_point['fillColor'] = $data_item['#color'];
$series_point['states']['hover']['fillColor'] = $data_item['#color'];
$series_point['states']['select']['fillColor'] = $data_item['#color'];
ChartElement::trimArray($series_point);
// Merge in point raw options.
if (!empty($data_item['#raw_options'])) {
$series_point = NestedArray::mergeDeepArray([
$data_item['#raw_options'],
$series_point,
]);
}
}
}
}
}
return $chart_definition;
}