You are here

public static function Util::createChartableData in Charts 8.3

Same name and namespace in other branches
  1. 8.4 src/Util/Util.php \Drupal\charts\Util\Util::createChartableData()
  2. 8 src/Util/Util.php \Drupal\charts\Util\Util::createChartableData()
  3. 5.0.x src/Util/Util.php \Drupal\charts\Util\Util::createChartableData()

Creates chart data to be used later by visualization frameworks.

Parameters

array $data: Data.

Return value

array Chart Data.

1 call to Util::createChartableData()
template_preprocess_views_view_charts in ./charts.module
Implements template_preprocess_hook().

File

src/Util/Util.php, line 101

Class

Util
Util.

Namespace

Drupal\charts\Util

Code

public static function createChartableData(array $data = []) {
  $chartData = [];
  $categories = [];
  $seriesData = [];
  for ($i = 0; $i < count($data[0]); $i++) {
    $seriesRowData = [
      'name' => '',
      'color' => '',
      'type' => '',
      'data' => [],
    ];
    for ($j = 0; $j < count($data); $j++) {
      $categories[$j] = $data[$j][$i]['label_field'];
      $seriesRowData['name'] = $data[$j][$i]['label'];
      $seriesRowData['type'] = $data[$j][$i]['type'];
      $seriesRowData['color'] = $data[$j][$i]['color'];
      array_push($seriesRowData['data'], json_decode($data[$j][$i]['value']));
    }
    array_push($seriesData, $seriesRowData);
  }
  $chartData[0] = $categories;
  $chartData[1] = $seriesData;
  return $chartData;
}