You are here

public static function ChartDataCollectorTable::getSeriesFromCollectedTable in Charts 8.4

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

Gets the series from the data collected by this element.

Parameters

array $data: The data.

string $type: The type of chart.

Return value

array The series.

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

File

src/Element/ChartDataCollectorTable.php, line 769

Class

ChartDataCollectorTable
Provides a chart data collector table form element.

Namespace

Drupal\charts\Element

Code

public static function getSeriesFromCollectedTable(array $data, $type) {
  $table = $data['data_collector_table'];
  $categories_identifier = $data['table_categories_identifier'];

  /** @var \Drupal\charts\TypeManager $chart_type_plugin_manager */
  $chart_type_plugin_manager = \Drupal::service('plugin.manager.charts_type');
  $chart_type = $chart_type_plugin_manager
    ->getDefinition($type);
  $is_single_axis = $chart_type['axis'] === ChartInterface::SINGLE_AXIS;
  $is_first_column = $categories_identifier === self::FIRST_COLUMN;
  $first_row = current($table);
  $category_col_key = key($first_row);

  // Skip the first row if it's considered as the  holding categories data.
  if (!$is_first_column) {
    array_shift($table);
  }
  $series = [];
  $i = 0;
  foreach ($table as $row_key => $row) {
    if (!$is_first_column) {
      $name_key = key($row);
      $series[$i]['name'] = $row[$name_key]['data'];
      $series[$i]['color'] = $row[$name_key]['color'];

      // Removing the name from data array.
      unset($row[$name_key]);
      foreach (array_values($row) as $column) {

        // Get all the data in this column and break out of this loop.
        if (is_numeric($column) || is_string($column)) {
          $series[$i]['data'][] = self::castValueToNumeric($column);
        }
        elseif (is_array($column) && isset($column['data'])) {
          $series[$i]['data'][] = self::castValueToNumeric($column['data']);
        }
      }
    }
    else {
      $j = 0;
      foreach ($row as $column_key => $column) {

        // Skipping the category label and it's data.
        if ($column_key === $category_col_key || !is_numeric($column_key)) {
          continue;
        }
        elseif ($i === 0) {

          // This is the first row which holds the data names.
          $series[$j]['name'] = $column['data'] ?? $column;
          $series[$j]['color'] = $column['color'] ?? ChartsDefaultColors::randomColor();
        }
        else {

          // Get all the data in this column and break out of this loop.
          $cell_value = is_array($column) && isset($column['data']) ? $column['data'] : $column;
          $cell_value = self::castValueToNumeric($cell_value);
          if ($is_single_axis) {
            $series[$j]['data'][] = [
              $row[$j]['data'],
              $cell_value,
            ];
          }
          else {
            $series[$j]['data'][] = $cell_value;
          }
        }
        $j++;
      }
    }
    $i++;
  }
  return $series;
}