trait ChartTrait in Dashboards with Layout Builder 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Dashboard/ChartTrait.php \Drupal\dashboards\Plugin\Dashboard\ChartTrait
Trait for build a chart.
Hierarchy
- trait \Drupal\dashboards\Plugin\Dashboard\ChartTrait uses StringTranslationTrait
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\DashboardView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChartTrait:: |
protected | property | Empty flag. | |
ChartTrait:: |
protected | property | Labels. | |
ChartTrait:: |
protected | property | Rows. | |
ChartTrait:: |
protected | property | Chart type. | |
ChartTrait:: |
public | function | Add a label. | |
ChartTrait:: |
public | function | Add a row. | |
ChartTrait:: |
public | function | Get allowed styles. | |
ChartTrait:: |
public | function | Set all rows. | |
ChartTrait:: |
public | function | Add a label. | |
ChartTrait:: |
public | function | Set this chart is empty. | |
ChartTrait:: |
public | function | Set all labels. | |
ChartTrait:: |
public | function | Set all rows. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |