class Billboard in Charts 5.0.x
Same name and namespace in other branches
- 8.4 modules/charts_billboard/src/Plugin/chart/Library/Billboard.php \Drupal\charts_billboard\Plugin\chart\Library\Billboard
Define a concrete class for a Chart.
Plugin annotation
@Chart(
id = "billboard",
name = @Translation("Billboard.js")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\charts\Plugin\chart\Library\ChartBase implements ChartInterface uses StringTranslationTrait
- class \Drupal\charts_billboard\Plugin\chart\Library\Billboard
- class \Drupal\charts\Plugin\chart\Library\ChartBase implements ChartInterface uses StringTranslationTrait
Expanded class hierarchy of Billboard
File
- modules/
charts_billboard/ src/ Plugin/ chart/ Library/ Billboard.php, line 20
Namespace
Drupal\charts_billboard\Plugin\chart\LibraryView source
class Billboard extends ChartBase {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['placeholder'] = [
'#title' => $this
->t('Placeholder'),
'#type' => 'fieldset',
'#description' => $this
->t('This is a placeholder for Billboard.js-specific library options. If you would like to help build this out, please work from <a href="@issue_link">this issue</a>.', [
'@issue_link' => Url::fromUri('https://www.drupal.org/project/charts/issues/3046983')
->toString(),
]),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function preRender(array $element) {
// Populate chart settings.
$chart_definition = [];
$chart_definition = $this
->populateOptions($element, $chart_definition);
$chart_definition = $this
->populateData($element, $chart_definition);
$chart_definition = $this
->populateAxes($element, $chart_definition);
if (!isset($element['#id'])) {
$element['#id'] = Html::getUniqueId('chart-billboard');
}
$chart_definition['bindto'] = '#' . $element['#id'];
$element['#attached']['library'][] = 'charts_billboard/billboard';
$element['#attributes']['class'][] = 'charts-billboard charts-bb';
$element['#chart_definition'] = $chart_definition;
return $element;
}
/**
* Get the chart type.
*
* @param string $chart_type
* The chart type.
* @param bool $is_polar
* Whether the polar is checked.
*
* @return string
* The chart type.
*/
protected function getType($chart_type, $is_polar = FALSE) {
// If Polar is checked, then convert to Radar chart type.
if ($is_polar) {
$type = 'radar';
}
else {
$type = $chart_type == 'column' ? 'bar' : $chart_type;
}
return $type;
}
/**
* Get options.
*
* @param string $type
* The chart type.
* @param array $element
* The element.
*
* @return array
* The returned options.
*/
protected function getOptionsByType($type, array $element) {
$options = $this
->getOptionsByCustomProperty($element, $type);
if ($type === 'bar') {
$options['width'] = $element['#width'];
}
return $options;
}
/**
* Get the options by custom property.
*
* @param array $element
* The element.
* @param string $type
* The chart type.
*
* @return array
* The return options.
*/
protected function getOptionsByCustomProperty(array $element, $type) {
$options = [];
$properties = Element::properties($element);
// Remove properties which are not related to this chart type.
$properties = array_filter($properties, function ($property) use ($type) {
$query = '#chart_' . $type . '_';
return substr($property, 0, strlen($query)) === $query;
});
foreach ($properties as $property) {
$query = '#chart_' . $type . '_';
$option_key = substr($property, strlen($query), strlen($property));
$options[$option_key] = $element[$property];
}
return $options;
}
/**
* Populate options.
*
* @param array $element
* The element.
* @param array $chart_definition
* The chart definition.
*
* @return array
* Return the chart definition.
*/
private function populateOptions(array $element, array $chart_definition) {
$type = $this
->getType($element['#chart_type'], $element['#polar']);
$chart_definition['title']['text'] = $element['#title'] ? $element['#title'] : '';
$chart_definition['legend']['show'] = !empty($element['#legend_position']);
if ($type !== 'scatter') {
$chart_definition['axis']['x']['type'] = 'category';
}
$chart_definition['data']['labels'] = (bool) $element['#data_labels'];
if ($type === 'pie' || $type === 'donut') {
// Do nothing.
}
elseif ($type === 'gauge') {
$chart_definition['gauge']['min'] = $element['#gauge']['min'];
$chart_definition['gauge']['max'] = $element['#gauge']['max'];
$chart_definition['color']['pattern'] = [
'red',
'yellow',
'green',
];
$chart_definition['color']['threshold']['values'] = [
$element['#gauge']['red_from'],
$element['#gauge']['yellow_from'],
$element['#gauge']['green_from'],
];
}
elseif ($type === 'line' || $type === 'spline') {
$chart_definition['point']['show'] = !empty($element['#data_markers']);
}
else {
/*
* Billboard does not use bar, so column must be used. Since 'column'
* is changed
* to 'bar' in getType(), we need to use the value from the element.
*/
if ($element['#chart_type'] === 'bar') {
$chart_definition['axis']['rotated'] = TRUE;
}
elseif ($element['#chart_type'] === 'column') {
$type = 'bar';
$chart_definition['axis']['rotated'] = FALSE;
}
}
$chart_definition['data']['type'] = $type;
// Merge in chart raw options.
if (!empty($element['#raw_options'])) {
$chart_definition = NestedArray::mergeDeepArray([
$element['#raw_options'],
$chart_definition,
]);
}
return $chart_definition;
}
/**
* Populate axes.
*
* @param array $element
* The element.
* @param array $chart_definition
* The chart definition.
*
* @return array
* Return the chart definition.
*/
private function populateAxes(array $element, array $chart_definition) {
/** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
$element_info = \Drupal::service('element_info');
$children = Element::children($element);
$axes = array_filter($children, function ($child) use ($element) {
$type = $element[$child]['#type'];
return $type === 'chart_xaxis' || $type === 'chart_yaxis';
});
$chart_type = $this
->getType($element['#chart_type'], $element['#polar']);
if ($axes) {
foreach ($axes as $key) {
// Make sure defaults are loaded.
if (empty($element[$key]['#defaults_loaded'])) {
$element[$key] += $element_info
->getInfo($element[$key]['#type']);
}
$axis_type = $element[$key]['#type'] === 'chart_xaxis' ? 'x' : 'y';
if ($axis_type === 'x') {
$categories = $element[$key]['#labels'] ? array_map('strip_tags', $element[$key]['#labels']) : [];
if (!in_array($chart_type, [
'pie',
'donut',
])) {
if ($chart_type === 'scatter') {
//
}
else {
$chart_definition['data']['columns'][] = [
'x',
];
$chart_definition['data']['x'] = 'x';
$categories_keys = array_keys($chart_definition['data']['columns']);
$categories_key = end($categories_keys);
foreach ($categories as $category) {
$chart_definition['data']['columns'][$categories_key][] = $category;
}
}
}
else {
$chart_definition['data']['columns'] = array_map(NULL, $categories, $chart_definition['data']['columns']);
}
}
}
}
return $chart_definition;
}
/**
* Populate data.
*
* @param array $element
* The element.
* @param array $chart_definition
* The chart definition.
*
* @return array
* Return the chart definition.
*/
private function populateData(array &$element, array $chart_definition) {
$type = $this
->getType($element['#chart_type'], $element['#polar']);
$types = [];
/** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
$element_info = \Drupal::service('element_info');
$children = Element::children($element);
$children = array_filter($children, function ($child) use ($element) {
return $element[$child]['#type'] === 'chart_data';
});
$columns = $chart_definition['data']['columns'] ?? [];
$column_keys = array_keys($columns);
$columns_key_start = $columns ? end($column_keys) + 1 : 0;
foreach ($children as $key) {
$child_element = $element[$key];
// Make sure defaults are loaded.
if (empty($child_element['#defaults_loaded'])) {
$child_element += $element_info
->getInfo($child_element['#type']);
}
if ($child_element['#color'] && $type !== 'gauge') {
$chart_definition['color']['pattern'][] = $child_element['#color'];
}
if (!in_array($type, [
'pie',
'donut',
])) {
$series_title = strip_tags($child_element['#title']);
$types[$series_title] = $child_element['#chart_type'] ? $this
->getType($child_element['#chart_type']) : $type;
if ($type !== 'scatter') {
$columns[$columns_key_start][] = $series_title;
foreach ($child_element['#data'] as $datum) {
if (gettype($datum) === 'array') {
if ($type === 'gauge') {
array_shift($datum);
}
$columns[$columns_key_start][] = array_map('strip_tags', $datum);
}
else {
$columns[$columns_key_start][] = strip_tags($datum);
}
}
}
else {
$row = [];
$row[$series_title][0] = $series_title;
$row[$series_title . '_x'][0] = $series_title . '_x';
foreach ($child_element['#data'] as $datum) {
$row[$series_title][] = $datum[0];
$row[$series_title . '_x'][] = $datum[1];
}
$chart_definition['data']['xs'][$series_title] = $series_title . '_x';
foreach ($row as $value) {
$columns[] = $value;
}
$columns = array_values($columns);
}
}
else {
foreach ($child_element['#data'] as $datum) {
$columns[] = $datum;
}
}
$columns_key_start++;
}
if ($element['#stacking']) {
$chart_definition['data']['groups'] = [
array_keys($types),
];
}
$chart_definition['data']['types'] = $types;
$chart_definition['data']['columns'] = $columns;
return $chart_definition;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Billboard:: |
public | function |
Form constructor. Overrides ChartBase:: |
|
Billboard:: |
protected | function | Get the options by custom property. | |
Billboard:: |
protected | function | Get options. | |
Billboard:: |
protected | function | Get the chart type. | |
Billboard:: |
private | function | Populate axes. | |
Billboard:: |
private | function | Populate data. | |
Billboard:: |
private | function | Populate options. | |
Billboard:: |
public | function |
Pre render. Overrides ChartInterface:: |
|
ChartBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
1 |
ChartBase:: |
public | function |
Return the name of the chart. Overrides ChartInterface:: |
|
ChartBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ChartBase:: |
public static | function | Gets the default hex colors. | |
ChartBase:: |
public static | function | Gets defaults settings. | |
ChartBase:: |
protected | function | Gets options properties. | |
ChartBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ChartBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
ChartBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
ChartInterface:: |
constant | Used to define a dual axis. | ||
ChartInterface:: |
constant | Used to define a single axis. | ||
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
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. |