View source
<?php
namespace Drupal\charts\Plugin\chart\Library;
use Drupal\charts\Settings\ChartsDefaultSettings;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
abstract class ChartBase extends PluginBase implements ChartInterface {
use StringTranslationTrait;
public function getChartName() {
return $this->pluginDefinition['name'];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function defaultConfiguration() {
return [];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public static function getDefaultSettings() {
$defaults = [
'type' => 'line',
'library' => NULL,
'grouping' => FALSE,
'fields' => [
'label' => NULL,
'data_providers' => NULL,
],
'display' => [
'title' => '',
'title_position' => 'out',
'data_labels' => FALSE,
'data_markers' => TRUE,
'legend' => TRUE,
'legend_position' => 'right',
'background' => '',
'three_dimensional' => FALSE,
'polar' => FALSE,
'tooltips' => TRUE,
'tooltips_use_html' => FALSE,
'dimensions' => [
'width' => NULL,
'width_units' => '%',
'height' => NULL,
'height_units' => 'px',
],
'gauge' => [
'green_to' => 100,
'green_from' => 85,
'yellow_to' => 85,
'yellow_from' => 50,
'red_to' => 50,
'red_from' => 0,
'max' => 100,
'min' => 0,
],
'colors' => self::getDefaultColors(),
],
];
return $defaults;
}
public static function getDefaultColors() {
return [
'#2f7ed8',
'#0d233a',
'#8bbc21',
'#910000',
'#1aadce',
'#492970',
'#f28f43',
'#77a1e5',
'#c42525',
'#a6c96a',
];
}
protected function getOptionsFromElementProperties(array $element) {
$options = [];
$properties_mapping = ChartsDefaultSettings::getLegacySettingsMappingKeys();
$filtered_element = array_filter($element, function ($property) use ($properties_mapping) {
$property = ltrim($property, '#');
return isset($properties_mapping[$property]);
});
foreach ($filtered_element as $property => $value) {
$property = ltrim($property, '#');
$property_map = $properties_mapping[$property];
if (substr($property_map, 0, 7) === 'display') {
$property_map = substr($property_map, 8, strlen($property_map));
if (substr($property_map, 0, 10) === 'dimensions') {
$property_map = substr($property_map, 11, strlen($property_map));
$options['display']['dimensions'][$property_map] = $value;
}
elseif (substr($property_map, 0, 5) === 'gauge') {
$property_map = substr($property_map, 6, strlen($property_map));
$options['display']['gauge'][$property_map] = $value;
}
else {
$options['display'][$property_map] = $value;
}
}
elseif (substr($property_map, 0, 5) === 'xaxis') {
$property_map = substr($property_map, 6, strlen($property_map));
$options['xaxis'][$property_map] = $value;
}
elseif (substr($property_map, 0, 5) === 'yaxis') {
$property_map = substr($property_map, 6, strlen($property_map));
if (substr($property_map, 0, 9) === 'secondary') {
$property_map = substr($property_map, 10, strlen($property_map));
$options['yaxis']['secondary'][$property_map] = $value;
}
else {
$options['yaxis'][$property_map] = $value;
}
}
elseif (substr($property_map, 0, 6) === 'fields') {
$property_map = substr($property_map, 7, strlen($property_map));
if ($property_map === 'data_providers' && is_array($value)) {
$data_providers = !empty($options['fields']['data_providers']) ? $options['fields']['data_providers'] : [];
if ($property === 'data_fields' || $property == 'field_colors') {
$options['fields']['data_providers'] = ChartsDefaultSettings::getFieldsDataProviders($data_providers, $value);
}
}
else {
$options['fields'][$property_map] = $value;
}
}
else {
$options[$property_map] = $property_map !== 'color' ? $value : $value[0];
}
}
return $options;
}
}