You are here

private function Highcharts::populateOptions in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php \Drupal\charts_highcharts\Plugin\chart\Library\Highcharts::populateOptions()

Populate options.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return the chart definition.

1 call to Highcharts::populateOptions()
Highcharts::preRender in modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php
Pre render.

File

modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php, line 206

Class

Highcharts
Defines a concrete class for a Highcharts.

Namespace

Drupal\charts_highcharts\Plugin\chart\Library

Code

private function populateOptions(array $element, array $chart_definition) {
  $chart_type = $this
    ->getType($element['#chart_type']);
  $chart_definition['chart']['width'] = $element['#width'] ? $element['#width'] : NULL;
  $chart_definition['chart']['height'] = $element['#height'] ? $element['#height'] : NULL;
  $chart_definition['chart']['type'] = $chart_type;
  $chart_definition['chart']['backgroundColor'] = $element['#background'];
  $chart_definition['chart']['polar'] = $element['#polar'] ?? NULL;
  $chart_definition['chart']['options3d']['enabled'] = $element['#three_dimensional'] ?? NULL;
  $chart_definition['credits']['enabled'] = FALSE;
  $chart_definition['title']['text'] = $element['#title'] ? $element['#title'] : '';
  $chart_definition['title']['style']['color'] = $element['#title_color'];
  $chart_definition['title']['verticalAlign'] = $element['#title_position'] === 'in' ? 'top' : NULL;
  $chart_definition['title']['y'] = $element['#title_position'] === 'in' ? 24 : NULL;
  $chart_definition['colors'] = $element['#colors'];
  $chart_definition['title']['verticalAlign'] = $element['#title_position'] === 'in' ? 'top' : NULL;
  $chart_definition['title']['y'] = $element['#title_position'] === 'in' ? 24 : NULL;
  $chart_definition['colors'] = $element['#colors'];
  $chart_definition['tooltip']['enabled'] = $element['#tooltips'] ? TRUE : FALSE;
  $chart_definition['tooltip']['useHTML'] = $element['#tooltips_use_html'] ? TRUE : FALSE;
  $chart_definition['plotOptions']['series']['stacking'] = $element['#stacking'] ? $element['#stacking'] : '';
  $chart_definition['plotOptions']['series']['dataLabels']['enabled'] = $element['#data_labels'] ? TRUE : FALSE;
  if ($element['#chart_type'] === 'gauge') {
    $chart_definition['yAxis']['plotBands'][] = [
      'from' => $element['#gauge']['red_from'],
      'to' => $element['#gauge']['red_to'],
      'color' => 'red',
    ];
    $chart_definition['yAxis']['plotBands'][] = [
      'from' => $element['#gauge']['yellow_from'],
      'to' => $element['#gauge']['yellow_to'],
      'color' => 'yellow',
    ];
    $chart_definition['yAxis']['plotBands'][] = [
      'from' => $element['#gauge']['green_from'],
      'to' => $element['#gauge']['green_to'],
      'color' => 'green',
    ];
    $chart_definition['yAxis']['min'] = (int) $element['#gauge']['min'];
    $chart_definition['yAxis']['max'] = (int) $element['#gauge']['max'];
  }

  // These changes are for consistency with Google. Perhaps too specific?
  if ($element['#chart_type'] === 'pie') {
    $chart_definition['plotOptions']['pie']['dataLabels']['distance'] = -30;
    $chart_definition['plotOptions']['pie']['dataLabels']['color'] = 'white';
    $chart_definition['plotOptions']['pie']['dataLabels']['format'] = '{percentage:.1f}%';
    $chart_definition['tooltip']['pointFormat'] = '<b>{point.y} ({point.percentage:.1f}%)</b><br/>';
  }
  if ($element['#legend'] === TRUE) {
    $chart_definition['legend']['enabled'] = $element['#legend'];
    if (in_array($element['#chart_type'], [
      'pie',
      'donut',
      'gauge',
    ])) {
      $chart_definition['plotOptions'][$element['#chart_type']]['showInLegend'] = TRUE;
    }
    if (!empty($element['#legend_title'])) {
      $chart_definition['legend']['title']['text'] = $element['#legend_title'];
    }
    if ($element['#legend_position'] === 'bottom') {
      $chart_definition['legend']['verticalAlign'] = 'bottom';
      $chart_definition['legend']['layout'] = 'horizontal';
    }
    elseif ($element['#legend_position'] === 'top') {
      $chart_definition['legend']['verticalAlign'] = 'top';
      $chart_definition['legend']['layout'] = 'horizontal';
    }
    else {
      $chart_definition['legend']['align'] = $element['#legend_position'];
      $chart_definition['legend']['verticalAlign'] = 'middle';
      $chart_definition['legend']['layout'] = 'vertical';
    }

    // Setting more legend configuration based on the plugin form entry.
    $legend_configuration = $this->configuration['legend'] ?? [];
    if (!empty($legend_configuration['layout'])) {
      $chart_definition['legend']['layout'] = $legend_configuration['layout'];
    }
    if (!empty($legend_configuration['background_color'])) {
      $chart_definition['legend']['backgroundColor'] = $legend_configuration['background_color'];
    }
    if (!empty($legend_configuration['border_width'])) {
      $chart_definition['legend']['borderWidth'] = $legend_configuration['border_width'];
    }
    if (!empty($legend_configuration['shadow'])) {
      $chart_definition['legend']['shadow'] = TRUE;
    }
    if (!empty($legend_configuration['item_style']['color'])) {
      $chart_definition['legend']['itemStyle']['color'] = $legend_configuration['item_style']['color'];
    }
    if (!empty($legend_configuration['item_style']['overflow'])) {
      $chart_definition['legend']['itemStyle']['overflow'] = $legend_configuration['item_style']['overflow'];
    }
  }
  else {
    $chart_definition['legend']['enabled'] = FALSE;
  }

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