private function Chartjs::populateDatasets in Charts 8.4
Same name and namespace in other branches
- 5.0.x modules/charts_chartjs/src/Plugin/chart/Library/Chartjs.php \Drupal\charts_chartjs\Plugin\chart\Library\Chartjs::populateDatasets()
Populate Dataset.
Parameters
array $element: The element.
array $chart_definition: The chart definition.
Return value
array Return the chart definition.
1 call to Chartjs::populateDatasets()
- Chartjs::preRender in modules/
charts_chartjs/ src/ Plugin/ chart/ Library/ Chartjs.php - Pre render.
File
- modules/
charts_chartjs/ src/ Plugin/ chart/ Library/ Chartjs.php, line 295
Class
- Chartjs
- Define a concrete class for a Chart.
Namespace
Drupal\charts_chartjs\Plugin\chart\LibraryCode
private function populateDatasets(array $element, array $chart_definition) {
$chart_type = $this
->populateChartType($element);
$datasets = [];
foreach (Element::children($element) as $key) {
if ($element[$key]['#type'] === 'chart_data') {
if (in_array($chart_type, [
'pie',
'doughnut',
])) {
foreach ($element[$key]['#data'] as $index => $datum) {
array_shift($element[$key]['#data'][$index]);
}
}
$series_data = [];
$dataset = new \stdClass();
// Populate the data.
foreach ($element[$key]['#data'] as $data_index => $data) {
if (isset($series_data[$data_index])) {
$series_data[$data_index][] = $data;
}
else {
if ($chart_type && $chart_type === 'scatter') {
$data = [
'y' => $data[1],
'x' => $data[0],
];
}
$series_data[$data_index] = $data;
}
}
$dataset->label = $element[$key]['#title'];
$dataset->data = $series_data;
if (!in_array($chart_type, [
'pie',
'doughnut',
])) {
$dataset->borderColor = $element[$key]['#color'];
}
$dataset->backgroundColor = $element[$key]['#color'];
$series_type = isset($element[$key]['#chart_type']) ? $this
->populateChartType($element[$key]) : $chart_type;
$dataset->type = $series_type;
if (!empty($element[$key]['#chart_type']) && $element[$key]['#chart_type'] === 'area') {
$dataset->fill = 'origin';
$dataset->backgroundColor = $this
->getTranslucentColor($element[$key]['#color']);
}
elseif ($element['#chart_type'] === 'area') {
$dataset->fill = 'origin';
$dataset->backgroundColor = $this
->getTranslucentColor($element[$key]['#color']);
}
else {
$dataset->fill = FALSE;
}
$datasets[] = $dataset;
}
// Merge in axis raw options.
if (!empty($element[$key]['#raw_options'])) {
$datasets = NestedArray::mergeDeepArray([
$datasets,
$element[$key]['#raw_options'],
]);
}
}
$chart_definition['data']['datasets'] = $datasets;
return $chart_definition;
}