You are here

private function Chartjs::populateOptions 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::populateOptions()

Populate options.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return the chart definition.

1 call to Chartjs::populateOptions()
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 128

Class

Chartjs
Define a concrete class for a Chart.

Namespace

Drupal\charts_chartjs\Plugin\chart\Library

Code

private function populateOptions(array $element, array $chart_definition) {
  $chart_type = $this
    ->populateChartType($element);
  $chart_definition['type'] = $chart_type;
  $children = Element::children($element);

  /*
   * Setting defaults based on what Views uses. However, API users may
   * have different keys for their X and Y axes.
   */
  $x_axis_key = 'xaxis';
  $y_axis_key = 'yaxis';
  foreach ($children as $child) {
    $type = $element[$child]['#type'];
    if ($type === 'chart_xaxis') {
      $x_axis_key = $child;
    }
    if ($type === 'chart_yaxis') {
      $y_axis_key = $child;
    }
  }
  $xaxis_configuration = $this->configuration['xaxis'] ?? [];
  $yaxis_configuration = $this->configuration['yaxis'] ?? [];
  if (!in_array($chart_type, [
    'pie',
    'doughnut',
  ])) {
    if (!empty($element['#stacking']) && $element['#stacking'] == 1) {
      $stacking = TRUE;
    }
    else {
      $stacking = FALSE;
    }
    if ($chart_type !== 'radar') {
      $chart_definition['options']['scales']['x'] = [
        'stacked' => $stacking,
        'ticks' => [
          'autoSkip' => $xaxis_configuration['autoskip'] ?? 1,
          'maxRotation' => $element[$x_axis_key]['#labels_rotation'] ?? 0,
          'minRotation' => $element[$x_axis_key]['#labels_rotation'] ?? 0,
        ],
      ];
      $chart_definition['options']['scales']['y'] = [
        'ticks' => [
          'beginAtZero' => NULL,
          'maxRotation' => $element[$y_axis_key]['#labels_rotation'] ?? 0,
          'minRotation' => $element[$y_axis_key]['#labels_rotation'] ?? 0,
        ],
        'maxTicksLimit' => 11,
        'precision' => NULL,
        'stepSize' => NULL,
        'suggestedMax' => NULL,
        'suggestedMin' => NULL,
        'stacked' => $stacking,
      ];
      if (!empty($element[$y_axis_key]['#min'])) {
        $chart_definition['options']['scales']['y']['min'] = $element[$y_axis_key]['#min'];
      }
      if (!empty($element[$y_axis_key]['#max'])) {
        $chart_definition['options']['scales']['y']['max'] = $element[$y_axis_key]['#max'];
      }
      if (!empty($element[$y_axis_key]['#title'])) {
        $chart_definition['options']['scales']['y']['title']['display'] = TRUE;
        $chart_definition['options']['scales']['y']['title']['text'] = $element[$y_axis_key]['#title'];
        $chart_definition['options']['scales']['y']['title']['align'] = $yaxis_configuration['vertical_axis_title_align'];
      }
      if (!empty($element[$x_axis_key]['#title'])) {
        $chart_definition['options']['scales']['x']['title']['display'] = TRUE;
        $chart_definition['options']['scales']['x']['title']['text'] = $element[$x_axis_key]['#title'];
        $chart_definition['options']['scales']['x']['title']['align'] = $xaxis_configuration['horizontal_axis_title_align'];
      }
    }
  }

  // Horizontal bar charts are configured by changing the bar chart indexAxis.
  // See https://www.chartjs.org/docs/latest/charts/bar.html#horizontal-bar-chart
  if ($element['#chart_type'] === 'bar') {
    $chart_definition['options']['indexAxis'] = 'y';
  }
  $chart_definition['options']['plugins']['title'] = $this
    ->buildTitle($element);
  $chart_definition['options']['plugins']['tooltip']['enabled'] = $element['#tooltips'];
  $chart_definition['options']['plugins']['legend'] = $this
    ->buildLegend($element);

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