You are here

private function Chartjs::populateCategories in Charts 8.4

Same name and namespace in other branches
  1. 5.0.x modules/charts_chartjs/src/Plugin/chart/Library/Chartjs.php \Drupal\charts_chartjs\Plugin\chart\Library\Chartjs::populateCategories()

Populate categories.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return the chart definition.

1 call to Chartjs::populateCategories()
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 257

Class

Chartjs
Define a concrete class for a Chart.

Namespace

Drupal\charts_chartjs\Plugin\chart\Library

Code

private function populateCategories(array $element, array $chart_definition) {
  $chart_type = $this
    ->populateChartType($element);
  $categories = [];
  foreach (Element::children($element) as $key) {
    if (in_array($chart_type, [
      'pie',
      'doughnut',
    ])) {
      foreach ($element[$key]['#data'] as $index => $datum) {
        $categories[] = $element[$key]['#data'][$index][0];
      }
    }
    else {
      $categories = array_map('strip_tags', $element['xaxis']['#labels']);
    }

    // Merge in axis raw options.
    if (!empty($element[$key]['#raw_options'])) {
      $categories = NestedArray::mergeDeepArray([
        $element[$key]['#raw_options'],
        $categories,
      ]);
    }
  }
  $chart_definition['data']['labels'] = $categories;
  return $chart_definition;
}