You are here

private function Chartjs::populateDatasets in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 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 259

Class

Chartjs
Define a concrete class for a Chart.

Namespace

Drupal\charts_chartjs\Plugin\chart\Library

Code

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') {
      $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 === 'scatter') {
            $data = [
              'y' => $data[1],
              'x' => $data[0],
            ];
          }

          /*
           * This is here to account for differences between Views and
           * the API. Will change if someone can find a better way.
           */
          if (in_array($chart_type, [
            'pie',
            'doughnut',
          ]) && !empty($data[1])) {
            $data = $data[1];
          }
          $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;
}