function _charts_highcharts_populate_chart_data in Charts 8
Same name and namespace in other branches
- 7.2 modules/charts_highcharts/charts_highcharts.inc \_charts_highcharts_populate_chart_data()
Utility to populate chart data.
_definition
Parameters
$chart:
Return value
mixed
1 call to _charts_highcharts_populate_chart_data()
- _charts_highcharts_render in modules/
charts_highcharts/ charts_highcharts.inc - Chart render callback; Convert all chart-level data.
File
- modules/
charts_highcharts/ charts_highcharts.inc, line 213 - Callbacks and utility functions for rendering a Highcharts Chart.
Code
function _charts_highcharts_populate_chart_data(&$chart, $chart_definition) {
$chart_definition['series'] = array();
foreach (\Drupal::state()
->getMultiple($chart) as $key) {
if ($chart[$key]['#type'] === 'chart_data') {
$series = array();
$series_data = array();
// Make sure defaults are loaded.
if (empty($chart[$key]['#defaults_loaded'])) {
$chart[$key] += \Drupal::service('element_info')
->getInfo($chart[$key]['#type']);
}
// Convert target named axis keys to integers.
if (isset($chart[$key]['#target_axis'])) {
$axis_name = $chart[$key]['#target_axis'];
$axis_index = 0;
foreach (\Drupal::state()
->getMultiple($chart) as $axis_key) {
if ($chart[$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 ($chart[$key]['#labels']) {
foreach ($chart[$key]['#labels'] as $label_index => $label) {
$series_data[$label_index][0] = $label;
}
}
// Populate the data.
foreach ($chart[$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'] = $chart[$key]['#chart_type'];
$series['name'] = $chart[$key]['#title'];
$series['color'] = $chart[$key]['#color'];
$series['marker']['radius'] = $chart[$key]['#marker_radius'];
$series['showInLegend'] = $chart[$key]['#show_in_legend'];
$series['connectNulls'] = TRUE;
$series['tooltip']['valueDecimals'] = $chart[$key]['#decimal_count'];
$series['tooltip']['xDateFormat'] = $chart[$key]['#date_format'];
$series['tooltip']['valuePrefix'] = $chart[$key]['#prefix'];
$series['tooltip']['valueSuffix'] = $chart[$key]['#suffix'];
if ($chart[$key]['#prefix'] || $chart[$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 = $chart[$key]['#decimal_count'] ? ':.' . $chart[$key]['#decimal_count'] . 'f' : '';
$chart_definition['yAxis'][$yaxis_index]['labels']['format'] = $chart[$key]['#prefix'] . "{value{$decimal_formatting}}" . $chart[$key]['#suffix'];
}
// Remove unnecessary keys to trim down the resulting JS settings.
charts_trim_array($series);
$series['data'] = $series_data;
// Merge in series raw options.
if (isset($chart[$key]['#raw_options'])) {
$series = NestedArray::mergeDeep($series, $chart[$key]['#raw_options']);
}
// Add the series to the main chart definition.
$chart_definition['series'][$key] = $series;
// Merge in any point-specific data points.
foreach (\Drupal::state()
->getMultiple($chart[$key]) as $sub_key) {
if ($chart[$key][$sub_key]['#type'] === 'chart_data_item') {
// Make sure defaults are loaded.
if (empty($chart[$key][$sub_key]['#defaults_loaded'])) {
$chart[$key][$sub_key] += \Drupal::service('element_info')
->getInfo($chart[$key][$sub_key]['#type']);
}
$data_item = $chart[$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 = array();
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'];
charts_trim_array($series_point);
// Merge in point raw options.
if (isset($data_item['#raw_options'])) {
$series_point = NestedArray::mergeDeep($series_point, $data_item['#raw_options']);
}
}
}
}
}
return $chart_definition;
}