You are here

protected function Highcharts::buildChartLegend in Charts 8.3

Build the legend.

Parameters

array $options: Options

Return value

\Drupal\charts_highcharts\Settings\Highcharts\ChartLegend Chart legend.

1 call to Highcharts::buildChartLegend()
Highcharts::buildVariables in modules/charts_highcharts/src/Plugin/chart/Highcharts.php
Creates a JSON Object formatted for Highcharts JavaScript to use.

File

modules/charts_highcharts/src/Plugin/chart/Highcharts.php, line 51

Class

Highcharts
Defines a concrete class for a Highcharts.

Namespace

Drupal\charts_highcharts\Plugin\chart

Code

protected function buildChartLegend(array $options) {

  // Retrieve Highcharts-specific default settings.
  $highchartsConfig = \Drupal::config('charts_highcharts.settings')
    ->get();

  // Incorporate Highcharts-specific default settings into $options.
  $options = array_merge($options, $highchartsConfig);
  $chartLegend = new ChartLegend();
  if (!empty($options['legend_layout'])) {
    $chartLegend
      ->setLayout($options['legend_layout']);
  }
  if (!empty($options['legend_background_color'])) {
    $chartLegend
      ->setBackgroundColor($options['legend_background_color']);
  }
  if (!empty($options['legend_border_width'])) {
    $chartLegend
      ->setBorderWidth($options['legend_border_width']);
  }
  if (!empty($options['legend_shadow'])) {
    $chartLegend
      ->setShadow($options['legend_shadow']);
  }
  if (empty($options['legend_position'])) {
    $chartLegend
      ->setEnabled(FALSE);
  }
  elseif (in_array($options['legend_position'], [
    'left',
    'right',
  ])) {
    if (!empty($options['legend_position'])) {
      $chartLegend
        ->setAlign($options['legend_position']);
    }
    $chartLegend
      ->setVerticalAlign('top');
    $chartLegend
      ->setY(80);
    if (!empty($options['legend_position']) && $options['legend_position'] == 'left') {
      $chartLegend
        ->setX(0);
    }
  }
  else {
    if (!empty($options['legend_position'])) {
      $chartLegend
        ->setVerticalAlign($options['legend_position']);
    }
    $chartLegend
      ->setAlign('center');
    $chartLegend
      ->setX(0);
    $chartLegend
      ->setY(0);
    $chartLegend
      ->setFloating(FALSE);
  }
  $styles = new ChartLegendItemStyle();
  if (!empty($options['item_style_color'])) {
    $styles
      ->setColor($options['item_style_color']);
  }
  if (!empty($options['text_overflow'])) {
    $styles
      ->setTextOverflow($options['text_overflow']);
  }
  $chartLegend
    ->setItemStyle($styles);

  // Get language direction and set legend direction upon
  $direction = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getDirection();
  $chartLegend
    ->setDirection($direction == 'ltr' ? FALSE : TRUE);
  return $chartLegend;
}