You are here

trait ChartTrait in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Dashboard/ChartTrait.php \Drupal\dashboards\Plugin\Dashboard\ChartTrait

Trait for build a chart.

Hierarchy

4 files declare their use of ChartTrait
Comments.php in modules/dashboards_comments/src/Plugin/Dashboard/Comments.php
MatomoBase.php in modules/dashboards_matomo/src/Plugin/Dashboard/MatomoBase.php
MostReaded.php in modules/dashboards_statistic/src/Plugin/Dashboard/MostReaded.php
Submissions.php in modules/dashboards_webform/src/Plugin/Dashboard/Submissions.php

File

src/Plugin/Dashboard/ChartTrait.php, line 10

Namespace

Drupal\dashboards\Plugin\Dashboard
View source
trait ChartTrait {
  use StringTranslationTrait;

  /**
   * Labels.
   *
   * @var array
   */
  protected $labels = [];

  /**
   * Rows.
   *
   * @var array
   */
  protected $rows = [];

  /**
   * Chart type.
   *
   * @var string
   */
  protected $type = 'pie';

  /**
   * Empty flag.
   *
   * @var bool
   */
  protected $empty = FALSE;

  /**
   * Add a label.
   *
   * @param string $chart
   *   Label to add.
   */
  public function setChartType($chart) : void {
    if (!in_array($chart, array_keys($this
      ->getAllowedStyles()))) {
      throw new \InvalidArgumentException($this
        ->t('Chart type @chart not allowed', [
        '@chart' => $chart,
      ]));
    }
    $this->type = $chart;
  }

  /**
   * Add a label.
   *
   * @param string $label
   *   Label to add.
   */
  public function addLabel($label) : void {
    $this->labels[] = $label;
  }

  /**
   * Get allowed styles.
   *
   * @return array
   *   Return array keyed by type.
   */
  public function getAllowedStyles() : array {
    return [
      'line' => $this
        ->t('Lines'),
      'pie' => $this
        ->t('Pies'),
      'bar' => $this
        ->t('Bars'),
      'radar' => $this
        ->t('Radar'),
      'polarArea' => $this
        ->t('Polar area'),
      'doughnut' => $this
        ->t('Doughnut'),
      'bubble' => $this
        ->t('Bubbles'),
    ];
  }

  /**
   * Set all labels.
   *
   * @param array $labels
   *   Labels to set.
   */
  public function setLabels(array $labels) : void {
    $this->labels = $labels;
  }

  /**
   * Add a row.
   *
   * @param array $row
   *   Row to add.
   */
  public function addRow(array $row) : void {
    $this->rows[] = $row;
  }

  /**
   * Set all rows.
   *
   * @param array $rows
   *   Rows to set.
   */
  public function setRows(array $rows) : void {
    $this->rows = $rows;
  }

  /**
   * Set this chart is empty.
   *
   * @param bool $empty
   *   Empty flag to set.
   */
  public function setEmpty(bool $empty) : void {
    $this->empty = $empty;
  }

  /**
   * Set all rows.
   *
   * @param array $conf
   *   Plugin configuration.
   * @param bool $plain
   *   Show only table.
   *
   * @return array
   *   Return renderable array.
   */
  public function renderChart(array $conf = [], bool $plain = FALSE) : array {
    if (count($this->rows) == 0) {
      return [
        '#markup' => $this
          ->t('No data found'),
      ];
    }
    $table = [
      '#type' => 'table',
      '#header' => $this->labels,
      '#rows' => $this->rows,
      '#attributes' => [
        'class' => [
          'dashboard-table',
          'table',
        ],
      ],
      '#attached' => [
        'library' => [
          'dashboards/chart',
        ],
      ],
    ];
    $attributes = [
      'data-app' => 'chart',
      'data-chart-type' => $this->type,
    ];
    if (isset($conf['legend']) && $conf['legend']) {
      $attributes['data-chart-display-legend'] = '1';
    }
    $build = [
      '#prefix' => '<div>',
      '#suffix' => '</div>',
      'chart' => [
        '#type' => 'container',
        '#attributes' => $attributes,
        [
          '#type' => 'container',
          [
            '#type' => 'details',
            '#title' => $this
              ->t('Show data'),
            '#open' => FALSE,
            'content' => $table,
          ],
        ],
      ],
    ];
    if ($plain == TRUE) {
      unset($build['#attributes']);
      unset($build['chart']['table']['#attached']);
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChartTrait::$empty protected property Empty flag.
ChartTrait::$labels protected property Labels.
ChartTrait::$rows protected property Rows.
ChartTrait::$type protected property Chart type.
ChartTrait::addLabel public function Add a label.
ChartTrait::addRow public function Add a row.
ChartTrait::getAllowedStyles public function Get allowed styles.
ChartTrait::renderChart public function Set all rows.
ChartTrait::setChartType public function Add a label.
ChartTrait::setEmpty public function Set this chart is empty.
ChartTrait::setLabels public function Set all labels.
ChartTrait::setRows public function Set all rows.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.