diff --git a/includes/charts.pages.inc b/includes/charts.pages.inc index f59e2e572a79d669b7af7851ffb74d2b1da9a1be..7907c1af2756c7ccf22a0a148baaeea9f7bc7874 100644 --- a/includes/charts.pages.inc +++ b/includes/charts.pages.inc @@ -283,6 +283,15 @@ function charts_settings_form($form, $defaults = [], $field_options = [], $paren } } + // Enable stacking. + $form['grouping'] = [ + '#type' => 'checkbox', + '#title' => t('Stacking'), + '#description' => t('Enable stacking'), + '#default_value' => $options['grouping'], + '#weight' => '-19', + ]; + if ($field_options) { $first_field = key($field_options); diff --git a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php index b0df36f43e5a4602a605e7a0cde6a74dbcd764db..a488fd51cee46c9597300ca9439ef99181f78e19 100644 --- a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php +++ b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php @@ -110,6 +110,7 @@ class ChartsBlock extends BlockBase { '#weight' => '-16', ]; // Enable stacking. + unset($form['grouping']['#parents']); $form['grouping'] = [ '#type' => 'checkbox', '#title' => $this->t('Stacking'), diff --git a/modules/charts_c3/src/Plugin/chart/C3.php b/modules/charts_c3/src/Plugin/chart/C3.php index d130c217e7446416785f48b0f09c6b736a06fec5..f100d93560c822432f9cb47ed6cc5eaf8e02efba 100644 --- a/modules/charts_c3/src/Plugin/chart/C3.php +++ b/modules/charts_c3/src/Plugin/chart/C3.php @@ -136,10 +136,20 @@ class C3 extends AbstractChart { } $chartData->types = $types; + if ($options['type'] != 'pie' && $options['type'] != 'donut') { $c3->setAxis($chartAxis); } + // Determines if chart is stacked. + if (!empty($options['grouping'] && $options['grouping'] == TRUE)) { + $seriesNames = []; + for ($i = 0; $i < count($seriesData); $i++) { + array_push($seriesNames, $seriesData[$i]['name']); + } + $chartData->setGroups([$seriesNames]); + } + $chartColor = new ChartColor(); $seriesColors = []; for ($i = 0; $i < count($seriesData); $i++) { diff --git a/modules/charts_c3/src/Settings/CThree/ChartData.php b/modules/charts_c3/src/Settings/CThree/ChartData.php index 8768a57637fb204b09fc424570741bf36926c576..f92bebb2382f9b6b134c431ffdb5072761bfe3ba 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartData.php +++ b/modules/charts_c3/src/Settings/CThree/ChartData.php @@ -11,11 +11,12 @@ class ChartData implements \JsonSerializable { private $type; private $labels = TRUE; private $x = 'x'; + private $groups = ''; /** * Get X. * - * @return array + * @return mixed * X. */ public function getX() { @@ -92,6 +93,26 @@ class ChartData implements \JsonSerializable { $this->labels = $labels; } + /** + * Get Stacking. + * + * @return array + * Stacking. + */ + public function getGroups() { + return $this->groups; + } + + /** + * Set Stacking. + * + * @param array $groups + * Stacking. + */ + public function setGroups($groups) { + $this->groups = $groups; + } + /** * Json Serialize. * diff --git a/src/Form/ChartsConfigForm.php b/src/Form/ChartsConfigForm.php index 3cd8617e6305bcce0d8b6c553b841331ec68289d..1d9d853298179fca5d99020da7f3a515d0e39bcf 100644 --- a/src/Form/ChartsConfigForm.php +++ b/src/Form/ChartsConfigForm.php @@ -269,6 +269,16 @@ class ChartsConfigForm extends ConfigFormBase { } } + // Enable stacking. + $form['grouping'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Stacking'), + '#description' => $this->t('Enable stacking'), + '#default_value' => $options['grouping'], + '#weight' => '-15', + '#parents' => array_merge($parents, ['grouping']), + ]; + $form['display'] = [ '#title' => $this->t('Display'), '#type' => 'details', diff --git a/src/Plugin/views/style/ChartsPluginStyleChart.php b/src/Plugin/views/style/ChartsPluginStyleChart.php index 6aa0f5efd53ab18259ffed3508e6475bfefa57e4..60324b6d1a1e5096abe9c1f29e4b20b167580da9 100644 --- a/src/Plugin/views/style/ChartsPluginStyleChart.php +++ b/src/Plugin/views/style/ChartsPluginStyleChart.php @@ -66,7 +66,7 @@ class ChartsPluginStyleChart extends StylePluginBase { // Limit grouping options (we only support one grouping field). 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 determine stacking of the chart. Generally this will be the same field as what you select for the "Label field" below. If you do not have more than one "Provides data" field below, there will be nothing to stack. If you want to have another series displayed, use a "Chart attachment" display, and set it to attach to this display. Stacking is still a work in progress for C3 Charts.'); + $form['grouping'][0]['field']['#description'] = t('If grouping by a particular field, that field will be used to determine stacking of the chart. Generally this will be the same field as what you select for the "Label field" below. If you do not have more than one "Provides data" field below, there will be nothing to stack. If you want to have another series displayed, use a "Chart attachment" display, and set it to attach to this display.'); $form['grouping'][0]['field']['#attributes']['class'][] = 'charts-grouping-field'; // Grouping by rendered version has no effect in charts. Hide the options. $form['grouping'][0]['rendered']['#access'] = FALSE;