You are here

public static function Chart::buildElement in Charts 8.4

Same name and namespace in other branches
  1. 5.0.x src/Element/Chart.php \Drupal\charts\Element\Chart::buildElement()

Build the element.

Parameters

array $settings: The settings.

string $chart_id: The chart id.

Return value

array The element.

3 calls to Chart::buildElement()
BaseSettings::processSeriesForm in src/Element/BaseSettings.php
Process series form.
ChartConfigItemDefaultFormatter::viewElement in src/Plugin/Field/FieldFormatter/ChartConfigItemDefaultFormatter.php
Builds a renderable array for a single chart item.
ChartsBlock::build in modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Element/Chart.php, line 305

Class

Chart
Provides a chart render element.

Namespace

Drupal\charts\Element

Code

public static function buildElement(array $settings, $chart_id) {
  $type = $settings['type'];
  $display_colors = $settings['display']['colors'] ?? [];

  // Overriding element colors for pie and donut chart types when the
  // settings display colors is empty.
  $overrides_element_colors = !$display_colors && ($type === 'pie' || $type === 'donut');
  $element = [
    '#type' => 'chart',
    '#chart_type' => $type,
    '#chart_library' => $settings['library'],
    '#title' => $settings['display']['title'],
    '#title_position' => $settings['display']['title_position'],
    '#tooltips' => $settings['display']['tooltips'],
    '#data_labels' => $settings['display']['data_labels'] ?? [],
    '#colors' => $display_colors,
    '#background' => $settings['display']['background'] ? $settings['display']['background'] : 'transparent',
    '#legend' => !empty($settings['display']['legend_position']),
    '#legend_position' => $settings['display']['legend_position'] ? $settings['display']['legend_position'] : '',
    '#width' => $settings['display']['dimensions']['width'],
    '#height' => $settings['display']['dimensions']['height'],
    '#width_units' => $settings['display']['dimensions']['width_units'],
    '#height_units' => $settings['display']['dimensions']['height_units'],
  ];
  if (!empty($settings['series'])) {
    $table = $settings['series'];

    // Extracting the categories.
    $categories = ChartDataCollectorTable::getCategoriesFromCollectedTable($table);

    // Extracting the rest of the data.
    $series_data = ChartDataCollectorTable::getSeriesFromCollectedTable($table, $type);
    $element['xaxis'] = [
      '#type' => 'chart_xaxis',
      '#labels' => $categories['data'],
      '#title' => $settings['xaxis']['title'] ? $settings['xaxis']['title'] : FALSE,
      '#labels_rotation' => $settings['xaxis']['labels_rotation'],
    ];
    if (!empty($series_data)) {
      $element['yaxis'] = [
        '#type' => 'chart_yaxis',
        '#title' => $settings['yaxis']['title'] ? $settings['yaxis']['title'] : '',
        '#labels_rotation' => $settings['yaxis']['labels_rotation'],
        '#max' => $settings['yaxis']['max'],
        '#min' => $settings['yaxis']['min'],
        '#prefix' => $settings['yaxis']['prefix'],
        '#suffix' => $settings['yaxis']['suffix'],
        '#decimal_count' => $settings['yaxis']['decimal_count'],
      ];

      // Create a secondary axis if needed.
      $series_count = count($series_data);
      if (!empty($settings['yaxis']['inherit']) && $series_count === 2) {
        $element['secondary_yaxis'] = [
          '#type' => 'chart_yaxis',
          '#title' => $settings['yaxis']['secondary']['title'] ? $settings['yaxis']['secondary']['title'] : '',
          '#labels_rotation' => $settings['yaxis']['secondary']['labels_rotation'],
          '#max' => $settings['yaxis']['secondary']['max'],
          '#min' => $settings['yaxis']['secondary']['min'],
          '#prefix' => $settings['yaxis']['secondary']['prefix'],
          '#suffix' => $settings['yaxis']['secondary']['suffix'],
          '#decimal_count' => $settings['yaxis']['secondary']['decimal_count'],
          '#opposite' => TRUE,
        ];
      }
      $series_counter = 0;

      // Overriding element colors for pie and donut chart types when the
      // settings display colors is empty.
      $overrides_element_colors = !$display_colors && ($type === 'pie' || $type === 'donut');
      foreach ($series_data as $data_index => $data) {
        $key = $chart_id . '_' . $data_index;
        $element[$key] = [
          '#type' => 'chart_data',
          '#data' => $data['data'],
          '#title' => $data['name'],
        ];
        if (!empty($data['color'])) {
          $element[$key]['#color'] = $data['color'];
          if ($overrides_element_colors) {
            $element['#colors'][] = $data['color'];
          }
        }
        if (isset($element['yaxis'])) {
          $element[$key]['#prefix'] = $settings['yaxis']['prefix'];
          $element[$key]['#suffix'] = $settings['yaxis']['suffix'];
          $element[$key]['#decimal_count'] = $settings['yaxis']['decimal_count'];
        }
        if (isset($element['secondary_yaxis']) && $series_counter === 1) {
          $element[$key]['#target_axis'] = 'secondary_yaxis';
          $element[$key]['#prefix'] = $settings['yaxis']['secondary']['prefix'];
          $element[$key]['#suffix'] = $settings['yaxis']['secondary']['suffix'];
          $element[$key]['#decimal_count'] = $settings['yaxis']['secondary']['decimal_count'];
        }
        $series_counter++;
      }
    }
    else {
      $element[$chart_id] = [
        '#type' => 'chart_data',
        '#title' => $series_data[0]['name'],
        '#data' => $series_data[0]['data'],
        '#prefix' => $settings['xaxis']['prefix'] ?? '',
        '#suffix' => $settings['xaxis']['suffix'] ?? '',
      ];
      if (!empty($series_data[0]['color'])) {
        $element[$chart_id]['#color'] = $series_data[0]['color'];
        if ($overrides_element_colors) {
          $element['#colors'][] = $series_data[0]['color'];
        }
      }
      $element['xaxis'] += [
        '#axis_type' => $settings['type'],
      ];
    }
  }
  return $element;
}