class ChartsApiExample 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
- 5.0.x modules/charts_api_example/src/Controller/ChartsApiExample.php \Drupal\charts_api_example\Controller\ChartsApiExample
Charts Api Example.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\charts_api_example\Controller\ChartsApiExample
Expanded class hierarchy of ChartsApiExample
1 string reference to 'ChartsApiExample'
- charts_api_example.services.yml in modules/
charts_api_example/ charts_api_example.services.yml - modules/charts_api_example/charts_api_example.services.yml
1 service uses ChartsApiExample
- charts.api_example in modules/
charts_api_example/ charts_api_example.services.yml - Drupal\charts_api_example\Controller\ChartsApiExample
File
- modules/
charts_api_example/ src/ Controller/ ChartsApiExample.php, line 14
Namespace
Drupal\charts_api_example\ControllerView source
class ChartsApiExample extends ControllerBase {
/**
* The charts settings.
*
* @var \Drupal\charts\Services\ChartsSettingsServiceInterface
*/
protected $chartSettings;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The UUID service.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuidService;
/**
* Construct.
*
* @param \Drupal\charts\Services\ChartsSettingsServiceInterface $chartSettings
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* @param \Drupal\Component\Uuid\UuidInterface $uuidService
*/
public function __construct(ChartsSettingsServiceInterface $chartSettings, MessengerInterface $messenger, UuidInterface $uuidService) {
$this->chartSettings = $chartSettings
->getChartsSettings();
$this->messenger = $messenger;
$this->uuidService = $uuidService;
}
/**
* Display.
*
* @return array
* Array to render.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('charts.settings'), $container
->get('messenger'), $container
->get('uuid'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChartsApiExample:: |
protected | property | The charts settings. | |
ChartsApiExample:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
ChartsApiExample:: |
protected | property | The UUID service. | |
ChartsApiExample:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
ChartsApiExample:: |
public | function | Display. | |
ChartsApiExample:: |
public | function | Construct. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |