View source
<?php
namespace Drupal\charts\Plugin\views\style;
use Drupal\core\form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\views\Plugin\views\style\StylePluginBase;
\Drupal::moduleHandler()
->loadInclude('charts', 'inc', 'charts.theme');
\Drupal::moduleHandler()
->loadInclude('charts', 'inc', 'includes/charts.pages');
class ChartsPluginStyleChart extends StylePluginBase {
protected $usesGrouping = FALSE;
protected $usesFields = TRUE;
protected $usesRowPlugin = TRUE;
protected function defineOptions() {
$options = parent::defineOptions();
$defaults = \Drupal::state()
->get('charts_default_settings', array());
$defaults += charts_default_settings();
foreach ($defaults as $default_key => $default_value) {
$options[$default_key]['default'] = $default_value;
}
if ($this->view->style_plugin === 'chart_extension') {
$options['type']['default'] = NULL;
}
$options['path'] = array(
'default' => 'charts',
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$handlers = $this->displayHandler
->getHandlers('field');
if (empty($handlers)) {
$form['error_markup'] = array(
'#markup' => '<div class="error messages">' . t('You need at least one field before you can configure your table settings') . '</div>',
);
}
if (isset($form['grouping'][0])) {
$form['grouping'][0]['field']['#title'] = t('Grouping field');
$form['grouping'][0]['field']['#description'] = t('If grouping by a particular field, that field will be used to generate multiple data series on the same chart.');
$form['grouping'][0]['field']['#attributes']['class'][] = 'charts-grouping-field';
$form['grouping'][0]['rendered']['#access'] = FALSE;
$form['grouping'][0]['rendered_strip']['#access'] = FALSE;
}
if (isset($form['grouping'][1])) {
$form['grouping'][1]['#access'] = FALSE;
}
$field_options = $this->displayHandler
->getFieldLabels();
$form = charts_settings_form($form, $this->options, $field_options, array(
'style_options',
));
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
}
public function validate() {
$errors = parent::validate();
$dataFields = $this->options['data_fields'];
$dataFieldsValueState = array();
$dataFieldsCounter = 0;
foreach ($dataFields as $value) {
if ($dataFieldsCounter > 0) {
if (empty($value)) {
array_push($dataFieldsValueState, 0);
}
else {
array_push($dataFieldsValueState, 1);
}
}
$dataFieldsCounter++;
}
if (array_sum($dataFieldsValueState) < 1) {
$errors[] = $this
->t('At least one data field must be selected in the chart configuration before this chart may be shown');
}
return $errors;
}
public function render() {
$field_handlers = $this->view
->getHandlers('field');
$label_field = FALSE;
$label_field_key = NULL;
if ($this->options['label_field'] && array_key_exists($this->options['label_field'], $field_handlers)) {
$label_field = $field_handlers[$this->options['label_field']];
$label_field_key = $this->options['label_field'];
}
$data_field_options = array_filter($this->options['data_fields']);
$data_fields = array();
foreach ($data_field_options as $field_key) {
if (isset($field_handlers[$field_key])) {
$data_fields[$field_key] = $field_handlers[$field_key];
}
}
if (isset($data_fields[$label_field_key])) {
unset($data_fields[$label_field_key]);
}
$chart_id = $this->view
->id() . '__' . $this->view->current_display;
$chart = array(
'#type' => 'chart',
'#chart_type' => $this->options['type'],
'#chart_library' => $this->options['library'],
'#chart_id' => $chart_id,
'#id' => 'chart_' . $chart_id,
'#title' => $this->options['title_position'] ? $this->options['title'] : FALSE,
'#title_position' => $this->options['title_position'],
'#tooltips' => $this->options['tooltips'],
'#data_labels' => $this->options['data_labels'],
'#colors' => $this->options['colors'],
'#background' => $this->options['background'] ? $this->options['background'] : 'transparent',
'#legend' => $this->options['legend_position'] ? TRUE : FALSE,
'#legend_position' => $this->options['legend_position'] ? $this->options['legend_position'] : NULL,
'#width' => $this->options['width'],
'#height' => $this->options['height'],
'#view' => $this->view,
'#theme' => 'views_view_charts',
);
$chart_type_info = charts_get_type($this->options['type']);
if ($chart_type_info['axis'] === CHARTS_SINGLE_AXIS) {
$data_field_key = key($data_fields);
$data_field = $data_fields[$data_field_key];
$data = array();
$this
->renderFields($this->view->result);
$renders = $this->rendered_fields;
foreach ($renders as $row_number => $row) {
$data_row = array();
if ($label_field_key) {
$data_row[] = htmlspecialchars_decode($this
->getField($row_number, $label_field_key), ENT_QUOTES);
}
$value = $this
->getField($row_number, $data_field_key);
if ($value === '') {
$value = NULL;
}
else {
$value = (double) str_replace(array(
',',
' ',
), '', $value);
}
$data_row[] = $value;
$data[] = $data_row;
}
if ($label_field) {
$chart['#legend_title'] = $label_field->options['label'];
}
$chart[$this->view->current_display . '_series'] = array(
'#type' => 'chart_data',
'#data' => $data,
'#title' => $data_field->options['label'],
);
}
else {
$chart['xaxis'] = array(
'#type' => 'chart_xaxis',
'#title' => $this->options['xaxis_title'] ? $this->options['xaxis_title'] : FALSE,
'#labels_rotation' => $this->options['xaxis_labels_rotation'],
);
$chart['yaxis'] = array(
'#type' => 'chart_yaxis',
'#title' => $this->options['yaxis_title'] ? $this->options['yaxis_title'] : FALSE,
'#labels_rotation' => $this->options['yaxis_labels_rotation'],
'#max' => $this->options['yaxis_max'],
'#min' => $this->options['yaxis_min'],
);
$sets = $this
->renderGrouping($this->view->result, $this->options['grouping'], TRUE);
foreach ($sets as $series_label => $data_set) {
$series_index = isset($series_index) ? $series_index + 1 : 0;
$series_key = $this->view->current_display . '__' . $field_key . '_' . $series_index;
foreach ($data_fields as $field_key => $field_handler) {
$chart[$series_key] = array(
'#type' => 'chart_data',
'#data' => array(),
'#color' => $series_label === '' && isset($this->options['field_colors'][$field_key]) ? $this->options['field_colors'][$field_key] : NULL,
'#title' => $series_label ? $series_label : $field_handler->options['label'],
'#prefix' => $this->options['yaxis_prefix'] ? $this->options['yaxis_prefix'] : NULL,
'#suffix' => $this->options['yaxis_suffix'] ? $this->options['yaxis_suffix'] : NULL,
'#decimal_count' => $this->options['yaxis_decimal_count'] ? $this->options['yaxis_decimal_count'] : NULL,
);
}
$row_number = 0;
foreach ($data_set['rows'] as $result_number => $row) {
if ($label_field_key && !isset($chart['xaxis']['#labels'][$row_number])) {
$chart['xaxis']['#labels'][$row_number] = $this
->getField($result_number, $label_field_key);
}
foreach ($data_fields as $field_key => $field_handler) {
if (isset($this->options['grouping'][0]['field']) && $field_key === $this->options['grouping'][0]['field']) {
continue;
}
$value = $this
->getField($result_number, $field_key);
if ($value === '') {
$value = NULL;
}
else {
$value = (double) str_replace(array(
',',
' ',
), '', $value);
}
$chart[$series_key]['#data'][] = $value;
}
$row_number++;
}
}
}
$children_displays = $this
->getChildrenChartDisplays();
$attachments = array();
$service = \Drupal::service('charts.charts_attachment');
foreach ($children_displays as $child_display) {
if (!$this->view
->access($child_display)) {
continue;
}
$subview = $this->view
->createDuplicate();
$subview
->setDisplay($child_display);
foreach ($this->options as $option_name => $option_value) {
if (strpos($option_name, 'yaxis') === 0 && $this->view->storage
->getDisplay($child_display)['display_options']['inherit_yaxis']) {
$subview->display_handler->options['style_options'][$option_name] = $option_value;
}
elseif (strpos($option_name, 'xaxis') === 0) {
$subview->display_handler->options['style_options'][$option_name] = $option_value;
}
}
$subview
->preExecute();
$subview
->execute();
if (empty($subview->result)) {
continue;
}
$subchart = $subview->style_plugin
->render();
array_push($attachments, $subview);
if ($this->view->storage
->getDisplay($child_display)['display_options']['inherit_yaxis'] !== '1' && isset($subchart['yaxis'])) {
$chart['secondary_yaxis'] = $subchart['yaxis'];
$chart['secondary_yaxis']['#opposite'] = TRUE;
}
foreach (Element::children($subchart) as $key) {
if ($subchart[$key]['#type'] === 'chart_data') {
$chart[$key] = $subchart[$key];
if ($subchart['#chart_type'] !== $chart['#chart_type']) {
$chart[$key]['#chart_type'] = $subchart['#chart_type'];
}
if ($this->view->storage
->getDisplay($child_display)['display_options']['inherit_yaxis'] !== '1') {
$chart[$key]['#target_axis'] = 'secondary_yaxis';
}
}
}
}
$service
->setAttachmentViews($attachments);
return $chart;
}
function get_parent_chart_display() {
$parent_display = FALSE;
return $parent_display;
}
function getChildrenChartDisplays() {
$children_displays = $this->displayHandler
->getAttachedDisplays();
return $children_displays;
}
}