You are here

protected function Google::createChartsOptions in Charts 8.3

Create charts options.

Parameters

array $options: Options.

array $seriesData: Series data.

array $attachmentDisplayOptions: Attachment Display Options.

array $customOptions: Overrides.

Return value

\Drupal\charts_google\Settings\Google\GoogleOptions GoogleOptions object with chart options or settings to be used by google visualization framework.

1 call to Google::createChartsOptions()
Google::buildVariables in modules/charts_google/src/Plugin/chart/Google.php
Creates a JSON Object formatted for Google Charts JavaScript to use.

File

modules/charts_google/src/Plugin/chart/Google.php, line 199

Class

Google
Define a concrete class for a Chart.

Namespace

Drupal\charts_google\Plugin\chart

Code

protected function createChartsOptions($options = [], $seriesData = [], $attachmentDisplayOptions = []) {
  $noAttachmentDisplays = count($attachmentDisplayOptions) === 0;
  $chartSelected = [];
  $seriesTypes = [];
  $firstVaxis = new ChartAxes();
  if (isset($options['yaxis_min'])) {
    $firstVaxis
      ->setMinValue($options['yaxis_min']);
  }
  if (isset($options['yaxis_view_min'])) {
    $firstVaxis
      ->setViewWindowValue('min', $options['yaxis_view_min']);
  }
  if (isset($options['yaxis_view_max'])) {
    $firstVaxis
      ->setViewWindowValue('max', $options['yaxis_view_max']);
  }
  if (isset($options['yaxis_max'])) {
    $firstVaxis
      ->setMaxValue($options['yaxis_max']);
  }

  // A format string for numeric or date axis labels.
  if (isset($options['yaxis_title'])) {
    $firstVaxis
      ->setTitle($options['yaxis_title']);
  }
  if (isset($options['yaxis_title_color'])) {
    $firstVaxis
      ->setTitleTextStyleValue('color', $options['yaxis_title_color']);
  }
  if (isset($options['yaxis_title_font'])) {
    $firstVaxis
      ->setTitleTextStyleValue('fontName', $options['yaxis_title_font']);
  }
  if (isset($options['yaxis_title_size'])) {
    $firstVaxis
      ->setTitleTextStyleValue('fontSize', $options['yaxis_title_size']);
  }
  if (isset($options['yaxis_title_bold'])) {
    $firstVaxis
      ->setTitleTextStyleValue('bold', $options['yaxis_title_bold']);
  }
  if (isset($options['yaxis_title_italic'])) {
    $firstVaxis
      ->setTitleTextStyleValue('italic', $options['yaxis_title_italic']);
  }

  // Axis title position.
  if (isset($options['yaxis_title_position'])) {
    $firstVaxis
      ->setTextPosition($options['yaxis_title_position']);
  }
  if (isset($options['yaxis_baseline'])) {
    $firstVaxis
      ->setBaseline($options['yaxis_baseline']);
  }
  if (isset($options['yaxis_baseline_color'])) {
    $firstVaxis
      ->setBaselineColor($options['yaxis_baseline_color']);
  }
  if (isset($options['yaxis_direction'])) {
    $firstVaxis
      ->setDirection($options['yaxis_direction']);
  }

  // A format string for numeric or date axis labels.
  if (isset($options['yaxis_format'])) {
    $firstVaxis
      ->setFormat($options['yaxis_format']);
  }
  if (isset($options['yaxis_view_window_mode'])) {
    $firstVaxis
      ->setViewWindowMode($options['yaxis_view_window_mode']);
  }
  $firstHaxis = new ChartAxes();
  if (isset($options['xaxis_min'])) {
    $firstHaxis
      ->setMinValue($options['xaxis_min']);
  }
  if (isset($options['xaxis_view_min'])) {
    $firstHaxis
      ->setViewWindowValue('min', $options['xaxis_view_min']);
  }
  if (isset($options['xaxis_view_max'])) {
    $firstHaxis
      ->setViewWindowValue('max', $options['xaxis_view_max']);
  }
  if (isset($options['xaxis_max'])) {
    $firstHaxis
      ->setMaxValue($options['xaxis_max']);
  }

  // A format string for numeric or date axis labels.
  if (isset($options['xaxis_title'])) {
    $firstHaxis
      ->setTitle($options['xaxis_title']);
  }
  if (isset($options['xaxis_title_color'])) {
    $firstHaxis
      ->setTitleTextStyleValue('color', $options['xaxis_title_color']);
  }
  if (isset($options['xaxis_title_font'])) {
    $firstHaxis
      ->setTitleTextStyleValue('fontName', $options['xaxis_title_font']);
  }
  if (isset($options['xaxis_title_size'])) {
    $firstHaxis
      ->setTitleTextStyleValue('fontSize', $options['xaxis_title_size']);
  }
  if (isset($options['xaxis_title_bold'])) {
    $firstHaxis
      ->setTitleTextStyleValue('bold', $options['xaxis_title_bold']);
  }
  if (isset($options['xaxis_title_italic'])) {
    $firstHaxis
      ->setTitleTextStyleValue('italic', $options['xaxis_title_italic']);
  }

  // Axis title position.
  if (isset($options['xaxis_title_position'])) {
    $firstHaxis
      ->setTextPosition($options['xaxis_title_position']);
  }
  if (isset($options['xaxis_baseline'])) {
    $firstHaxis
      ->setBaseline($options['xaxis_baseline']);
  }
  if (isset($options['xaxis_baseline_color'])) {
    $firstHaxis
      ->setBaselineColor($options['xaxis_baseline_color']);
  }
  if (isset($options['xaxis_direction'])) {
    $firstHaxis
      ->setDirection($options['xaxis_direction']);
  }

  // A format string for numeric or date axis labels.
  if (isset($options['xaxis_format'])) {
    $firstHaxis
      ->setFormat($options['xaxis_format']);
  }
  if (isset($options['xaxis_view_window_mode'])) {
    $firstHaxis
      ->setViewWindowMode($options['xaxis_view_window_mode']);
  }
  $vAxes = [];
  $hAxes = [];
  array_push($vAxes, $firstVaxis);
  array_push($hAxes, $firstHaxis);

  // Sets secondary axis from the first attachment only.
  if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) {
    $secondVaxis = new ChartAxes();
    $secondVaxis
      ->setTitle($attachmentDisplayOptions[0]['style']['options']['yaxis_title']);
    array_push($vAxes, $secondVaxis);
  }
  array_push($chartSelected, $this
    ->processChartType($options['type']));

  // @todo: make sure this works for more than one attachment.
  for ($i = 0; $i < count($attachmentDisplayOptions); $i++) {
    $attachmentChartType = $this
      ->processChartType($attachmentDisplayOptions[$i]['style']['options']['type']);
    if ($attachmentDisplayOptions[$i]['inherit_yaxis'] == 0 && $i == 0) {
      $seriesTypes[$i + 1] = [
        'type' => $attachmentChartType,
        'targetAxisIndex' => 1,
      ];
    }
    else {
      $seriesTypes[$i + 1] = [
        'type' => $attachmentChartType,
      ];
    }
    array_push($chartSelected, $attachmentChartType);
  }
  if (count($seriesData) != 0) {
    for ($i = 0; $i < count($seriesData); $i++) {
      if (isset($seriesData[$i]['type'])) {
        $seriesTypes[$i]['type'] = $this
          ->processChartType($seriesData[$i]['type']);
      }
      else {
        $seriesTypes[$i]['type'] = '';
      }
    }
  }
  $chartSelected = array_unique($chartSelected);
  $googleOptions = new GoogleOptions();
  if (count($seriesData) > 1) {
    $parentChartType = $this
      ->processChartType($options['type']);
    $googleOptions->seriesType = $parentChartType;
    $googleOptions->series = $seriesTypes;
  }
  $googleOptions
    ->setTitle($options['title']);
  if (isset($options['subtitle'])) {
    $googleOptions
      ->setSubTitle($options['subtitle']);
  }
  $googleOptions
    ->setVerticalAxes($vAxes);
  $googleOptions
    ->setHorizontalAxes($hAxes);
  if (isset($options['sliceVisibilityThreshold'])) {
    $googleOptions
      ->setSliceVisibilityThreshold($options['sliceVisibilityThreshold']);
  }
  if (in_array('donut', $chartSelected)) {
    $googleOptions->pieHole = '0.25';
  }
  $chartArea = new ChartArea();

  // Chart Area width.
  if (isset($options['chart_area']['width'])) {
    $chartArea
      ->setWidth($options['chart_area']['width']);
  }

  // Chart Area height.
  if (isset($options['chart_area']['height'])) {
    $chartArea
      ->setHeight($options['chart_area']['height']);
  }

  // Chart Area padding top.
  if (isset($options['chart_area']['top'])) {
    $chartArea
      ->setPaddingTop($options['chart_area']['top']);
  }

  // Chart Area padding left.
  if (isset($options['chart_area']['left'])) {
    $chartArea
      ->setPaddingLeft($options['chart_area']['left']);
  }
  $googleOptions
    ->setChartArea($chartArea);
  $seriesCount = count($seriesData);
  $categoriesCount = count($seriesData[0]['data']);
  $seriesColors = [];
  if ($options['type'] == 'pie' || $options['type'] == 'donut') {
    if ($seriesCount > 1) {
      for ($i = 0; $i < $seriesCount; $i++) {
        $seriesColor = $seriesData[$i]['color'];
        array_push($seriesColors, $seriesColor);
      }
    }
    else {
      for ($i = 0; $i < $categoriesCount; $i++) {

        // Use default colors if only one series.
        if (isset($options['colors'][$i])) {
          $seriesColor = $options['colors'][$i];
          array_push($seriesColors, $seriesColor);
        }
      }
    }
  }
  else {
    for ($i = 0; $i < $seriesCount; $i++) {
      if (isset($seriesData[$i]['color'])) {
        $seriesColor = $seriesData[$i]['color'];
        array_push($seriesColors, $seriesColor);
      }
      else {
        $seriesColor = '#000000';
        array_push($seriesColors, $seriesColor);
      }
    }
  }
  $googleOptions
    ->setColors($seriesColors);

  // Width of the chart, in pixels.
  if (isset($options['width'])) {
    $googleOptions
      ->setWidth($options['width']);
  }

  // Height of the chart, in pixels.
  if (isset($options['height'])) {
    $googleOptions
      ->setHeight($options['height']);
  }

  // Determines if chart is three-dimensional.
  if (isset($options['three_dimensional'])) {
    $googleOptions
      ->setThreeDimensional($options['three_dimensional']);
  }

  // Determines if chart is stacked.
  if (!empty($options['grouping'])) {
    $options['grouping'] = TRUE;
    $googleOptions
      ->setStacking($options['grouping']);
  }

  // 'legend' can be a string (for position) or an array with legend
  // properties: [position: 'top', textStyle: [color: 'blue', fontSize: 16]].
  if (isset($options['legend'])) {
    $googleOptions
      ->setLegend($options['legend']);
  }

  // Sets the markers.
  if (isset($options['data_markers'])) {
    if ($options['type'] == 'line' || $options['type'] == 'spline') {
      if ($options['data_markers'] == 'FALSE') {
        $googleOptions
          ->setPointSize(0);
      }
      else {
        $googleOptions
          ->setPointSize(5);
      }
    }
  }
  if (isset($options['background'])) {
    $googleOptions
      ->setBackgroundColor($options['background']);
  }

  // Set legend position.
  if (isset($options['legend_position'])) {
    if (empty($options['legend_position'])) {
      $options['legend_position'] = 'none';
      $googleOptions
        ->setLegend($options['legend_position']);
    }
    else {
      $googleOptions
        ->setLegend($options['legend_position']);
    }
  }

  // Where to place the chart title, compared to the chart area.
  if (isset($options['title_position'])) {
    $googleOptions
      ->setTitlePosition($options['title_position']);
  }

  // Where to place the axis titles, compared to the chart area.
  if (isset($options['axis_titles_position'])) {
    $googleOptions
      ->setAxisTitlesPosition($options['axis_titles_position']);
  }

  // Determine what content to show in pie slices.
  if (isset($options['pie_slice_text'])) {
    $googleOptions
      ->setPieSliceText($options['pie_slice_text']);
  }

  // Set gauge specific options.
  if ($options['type'] == 'gauge') {
    $fields = [
      'green_to',
      'green_from',
      'red_to',
      'red_from',
      'yellow_to',
      'yellow_from',
      'max',
      'min',
    ];
    foreach ($fields as $field) {
      $options[$field] = isset($options[$field]) ? $options[$field] : NULL;
    }
    $googleOptions
      ->setGreenTo($options['green_to']);
    $googleOptions
      ->setGreenFrom($options['green_from']);
    $googleOptions
      ->setRedTo($options['red_to']);
    $googleOptions
      ->setRedFrom($options['red_from']);
    $googleOptions
      ->setYellowTo($options['yellow_to']);
    $googleOptions
      ->setYellowFrom($options['yellow_from']);
    $googleOptions
      ->setMax($options['max']);
    $googleOptions
      ->setMin($options['min']);
  }

  // Set curveType options if spline.
  if ($options['type'] == 'spline') {
    $googleOptions
      ->setCurveType('function');
  }

  // Set dataless region color option if geo.
  if ($options['type'] == 'geo') {
    if (empty($options['colorAxis'])) {
      $options['colorAxis'] = array();
    }
    $googleOptions
      ->setColorAxis($options['colorAxis']);
    $googleOptions
      ->setDatalessRegionColor($options['datalessRegionColor']);
  }
  return $googleOptions;
}