You are here

protected function Highcharts::buildXaxis in Charts 8.3

Build the x-axis.

Parameters

array $options: Options.

array $seriesData: SeriesData.

array $categories: Categories.

Return value

\Drupal\charts_highcharts\Settings\Highcharts\Xaxis X-axis.

1 call to Highcharts::buildXaxis()
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 181

Class

Highcharts
Defines a concrete class for a Highcharts.

Namespace

Drupal\charts_highcharts\Plugin\chart

Code

protected function buildXaxis(array $options, array $seriesData, array $categories) {
  $chartXaxis = new Xaxis();
  $chartLabels = new ChartLabel();

  // Set x-axis label rotation.
  if (isset($options['xaxis_labels_rotation'])) {
    $chartLabels
      ->setRotation($options['xaxis_labels_rotation']);
  }
  $xAxisTitle = new XaxisTitle();
  if (isset($options['xaxis_title'])) {
    $xAxisTitle
      ->setText($options['xaxis_title']);
  }

  // If donut or pie and only one data point with multiple fields in use.
  if (($options['type'] == 'pie' || $options['type'] == 'donut') && count($seriesData[0]['data']) == 1) {
    unset($categories);
    $categories = [];
    for ($i = 0; $i < count($seriesData); $i++) {
      array_push($categories, $seriesData[$i]['name']);
    }
  }
  if (!empty($options['reverse_series']) && $options['reverse_series'] == 1) {
    unset($categories);
    $categories = [];
    for ($i = 0; $i < count($seriesData); $i++) {
      array_push($categories, $seriesData[$i]['name']);
    }
  }
  $chartXaxis
    ->setCategories($categories);
  if (isset($options['xaxis_tickmark_placement'])) {
    switch ($options['xaxis_tickmark_placement']) {
      case 'on':
      case 'between':
        $chartXaxis
          ->setTickmarkPlacement($options['xaxis_tickmark_placement']);
        break;
      default:
    }
  }

  // Set x-axis title.
  $chartXaxis
    ->setTitle($xAxisTitle);
  $chartXaxis
    ->setLabels($chartLabels);

  // Set min.
  if (!empty($options['xaxis_min'])) {
    $chartXaxis
      ->setMin((int) $options['xaxis_min']);
  }

  // Set max.
  if (!empty($options['xaxis_max'])) {
    $chartXaxis
      ->setMax((int) $options['xaxis_max']);
  }

  // Set interval.
  if (!empty($options['xaxis_interval'])) {
    $chartXaxis
      ->setTickInterval((int) $options['xaxis_interval']);
  }

  // Set line width.
  if (isset($options['xaxis_line_width']) && is_int($options['xaxis_line_width'])) {
    $chartXaxis
      ->setLineWidth($options['xaxis_line_width']);
  }
  return $chartXaxis;
}