You are here

protected function Highcharts::buildYaxis in Charts 8.3

Build the y-axis.

Parameters

string $title: Title.

mixed $yaxisLabels: Labels.

array $options: Options.

array $seriesData: SeriesData.

array $categories: Categories.

Return value

\Drupal\charts_highcharts\Settings\Highcharts\Yaxis

1 call to Highcharts::buildYaxis()
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 308

Class

Highcharts
Defines a concrete class for a Highcharts.

Namespace

Drupal\charts_highcharts\Plugin\chart

Code

protected function buildYaxis($title, $yaxisLabels, array $options, array $seriesData, array $categories) {
  $chartYaxis = new Yaxis();
  $yAxisTitle = new YaxisTitle();
  $yAxisTitle
    ->setText($title);
  if (!empty($options['yaxis_min'])) {
    $chartYaxis
      ->setMin($options['yaxis_min']);
  }
  if (!empty($options['yaxis_max'])) {
    $chartYaxis
      ->setMax($options['yaxis_max']);
  }
  if (isset($options['yaxis_categories'])) {
    $chartYaxis
      ->setCategories($options['yaxis_categories']);
  }
  if (!empty($options['yaxis_interval']) && is_numeric($options['yaxis_interval'])) {
    $chartYaxis
      ->setTickInterval($options['yaxis_interval']);
  }
  if (isset($options['yaxis_show_first_label'])) {
    $chartYaxis
      ->setShowFirstLabel($options['yaxis_show_first_label']);
    if ($options['yaxis_show_first_label']) {
      $chartYaxis
        ->setStartOnTick(TRUE);
    }
  }
  if (isset($options['yaxis_show_last_label'])) {
    $chartYaxis
      ->setShowLastLabel($options['yaxis_show_last_label']);

    // endOnTick needs to be TRUE for showLastLabel = TRUE to work.
    if ($options['yaxis_show_last_label']) {
      $chartYaxis
        ->setEndOnTick(TRUE);
    }
  }
  if (isset($options['yaxis_tickmark_placement'])) {
    switch ($options['yaxis_tickmark_placement']) {
      case 'on':
      case 'between':
        $chartYaxis
          ->setTickmarkPlacement($options['yaxis_tickmark_placement']);
        break;
      default:
    }
  }
  if (isset($options['yaxis_line_width']) && is_int($options['yaxis_line_width'])) {
    $chartYaxis
      ->setLineWidth($options['yaxis_line_width']);
  }

  // Polar options.
  if (isset($options['polar'])) {
    if (isset($options['yaxis_gridline_interpolation'])) {
      switch ($options['yaxis_gridline_interpolation']) {
        case 'circle':
        case 'polygon':
          $chartYaxis
            ->setGridLineInterpolation($options['yaxis_gridline_interpolation']);
          break;
        default:
      }
    }
  }

  // Gauge options.
  if ($options['type'] == 'gauge') {

    // Gauge will not work if grouping is set.
    $options['grouping'] = [];
    $plotBandsGreen = new PlotBands();
    $plotBandsYellow = new PlotBands();
    $plotBandsRed = new PlotBands();
    $gaugeColors = [];
    $plotBandsRed
      ->setFrom($options['red_from']);
    $plotBandsRed
      ->setTo($options['red_to']);
    $plotBandsRed
      ->setColor('red');
    array_push($gaugeColors, $plotBandsRed);
    $plotBandsYellow
      ->setFrom($options['yellow_from']);
    $plotBandsYellow
      ->setTo($options['yellow_to']);
    $plotBandsYellow
      ->setColor('yellow');
    array_push($gaugeColors, $plotBandsYellow);
    $plotBandsGreen
      ->setFrom($options['green_from']);
    $plotBandsGreen
      ->setTo($options['green_to']);
    $plotBandsGreen
      ->setColor('green');
    array_push($gaugeColors, $plotBandsGreen);
    $chartYaxis
      ->setPlotBands($gaugeColors);
    $chartYaxis
      ->setMin((int) $options['min']);
    $chartYaxis
      ->setMax((int) $options['max']);
    if (count($seriesData) > 1 || count($categories) > 1) {
      \Drupal::service('messenger')
        ->addMessage(t('The gauge
          chart type does not work well with more than one value.'), 'warning');
    }
  }
  $chartYaxis
    ->setLabels($yaxisLabels);
  $chartYaxis
    ->setTitle($yAxisTitle);
  return $chartYaxis;
}