public function ChartsApiExample::display in Charts 8.3
Same name and namespace in other branches
- 8.4 modules/charts_api_example/src/Controller/ChartsApiExample.php \Drupal\charts_api_example\Controller\ChartsApiExample::display()
- 5.0.x modules/charts_api_example/src/Controller/ChartsApiExample.php \Drupal\charts_api_example\Controller\ChartsApiExample::display()
Display.
Return value
array Array to render.
1 string reference to 'ChartsApiExample::display'
- charts_api_example.routing.yml in modules/
charts_api_example/ charts_api_example.routing.yml - modules/charts_api_example/charts_api_example.routing.yml
File
- modules/
charts_api_example/ src/ Controller/ ChartsApiExample.php, line 56
Class
- ChartsApiExample
- Charts Api Example.
Namespace
Drupal\charts_api_example\ControllerCode
public function display() {
$library = $this->chartSettings['library'];
if (empty($library)) {
$this->messenger
->addError($this
->t('You need to first configure Charts default settings'));
return [];
}
// Customize options here.
$options = [
'type' => $this->chartSettings['type'],
'title' => $this
->t('Chart title'),
'xaxis_title' => $this
->t('X-Axis'),
'yaxis_title' => $this
->t('Y-Axis'),
'yaxis_min' => '',
'yaxis_max' => '',
'three_dimensional' => FALSE,
'title_position' => 'out',
'legend_position' => 'right',
'data_labels' => $this->chartSettings['data_labels'],
'tooltips' => $this->chartSettings['tooltips'],
// 'grouping' => TRUE,
'colors' => $this->chartSettings['colors'],
'min' => $this->chartSettings['min'],
'max' => $this->chartSettings['max'],
'yaxis_prefix' => $this->chartSettings['yaxis_prefix'],
'yaxis_suffix' => $this->chartSettings['yaxis_suffix'],
'data_markers' => $this->chartSettings['data_markers'],
'red_from' => $this->chartSettings['red_from'],
'red_to' => $this->chartSettings['red_to'],
'yellow_from' => $this->chartSettings['yellow_from'],
'yellow_to' => $this->chartSettings['yellow_to'],
'green_from' => $this->chartSettings['green_from'],
'green_to' => $this->chartSettings['green_to'],
];
// Sample data format.
$categories = [
'Category 1',
'Category 2',
'Category 3',
'Category 4',
];
$seriesData[] = [
'name' => 'Series 1',
'color' => '#0d233a',
'type' => $this->chartSettings['type'],
'data' => [
250,
350,
400,
200,
],
];
switch ($this->chartSettings['type']) {
default:
$seriesData[] = [
'name' => 'Series 2',
'color' => '#8bbc21',
'type' => 'column',
'data' => [
150,
450,
500,
300,
],
];
$seriesData[] = [
'name' => 'Series 3',
'color' => '#910000',
'type' => 'area',
'data' => [
0,
0,
60,
90,
],
];
case 'pie':
case 'donut':
}
// Creates a UUID for the chart ID.
$chartId = 'chart-' . $this->uuidService
->generate();
$build = [
'#theme' => 'charts_api_example',
'#library' => (string) $library,
'#categories' => $categories,
'#seriesData' => $seriesData,
'#options' => $options,
'#id' => $chartId,
'#override' => [],
];
return $build;
}