You are here

public static function ChartDataCollectorTable::getCategoriesFromCollectedTable in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Element/ChartDataCollectorTable.php \Drupal\charts\Element\ChartDataCollectorTable::getCategoriesFromCollectedTable()

Gets the categories from the data collected by this element.

Parameters

array $data: The data.

Return value

array The category label and data.

1 call to ChartDataCollectorTable::getCategoriesFromCollectedTable()
Chart::buildElement in src/Element/Chart.php
Build the element.

File

src/Element/ChartDataCollectorTable.php, line 733

Class

ChartDataCollectorTable
Provides a chart data collector table form element.

Namespace

Drupal\charts\Element

Code

public static function getCategoriesFromCollectedTable(array $data) {
  $categories_identifier = $data['table_categories_identifier'];
  $table = $data_table = $data['data_collector_table'];
  $categories = [
    'label' => '',
    'data' => [],
  ];
  $is_first_column = $categories_identifier === self::FIRST_COLUMN;
  $first_row = current($table);
  $category_col_key = key($first_row);
  $categories['label'] = $first_row[$category_col_key];
  $data = [];
  if ($is_first_column) {

    // Extracting the categories data.
    $col_cells = array_column($table, $category_col_key);
    foreach ($col_cells as $cell) {
      $data[] = is_array($cell) ? $cell['data'] : $cell;
    }
  }
  else {
    $col_cells = array_values($first_row);
    foreach ($col_cells as $cell) {
      $data[] = is_array($cell) ? $cell['data'] : $cell;
    }
  }
  $categories['data'] = $data;

  // Removing the category label from categories.
  $categories_data = $categories['data'];
  array_shift($categories_data);
  $categories['data'] = $categories_data;
  return $categories;
}