Loading modules/charts_api_example/src/Controller/ChartsApiExample.php +1 −0 Original line number Diff line number Diff line Loading @@ -311,6 +311,7 @@ class ChartsApiExample extends ControllerBase { 'x_axis' => [ '#type' => 'chart_xaxis', '#title' => $this->t('Height'), '#labels' => [], ], 'y_axis' => [ '#type' => 'chart_yaxis', Loading modules/charts_chartjs/src/Plugin/chart/Library/Chartjs.php +2 −2 Original line number Diff line number Diff line Loading @@ -239,7 +239,7 @@ class Chartjs extends ChartBase { foreach ($children as $child) { $type = $element[$child]['#type']; if ($type === 'chart_xaxis') { $categories = array_map('strip_tags', $element[$child]['#labels']); $categories = is_array($element[$child]['#labels']) ? array_map('strip_tags', $element[$child]['#labels']): []; // Merge in axis raw options. if (!empty($element[$child]['#raw_options'])) { $categories = NestedArray::mergeDeepArray([$element[$child]['#raw_options'], $categories,]); Loading Loading @@ -308,7 +308,7 @@ class Chartjs extends ChartBase { if (!in_array($chart_type, ['pie', 'doughnut'])) { $dataset->borderColor = $element[$key]['#color']; } $dataset->backgroundColor = $element[$key]['#color']; $dataset->backgroundColor = $element[$key]['#color'] ?? '#000000'; // $series_type = isset($element[$key]['#chart_type']) ? $this->populateChartType($element[$key]) : $chart_type; $dataset->type = $series_type; if (!empty($element[$key]['#chart_type']) && $element[$key]['#chart_type'] === 'area') { Loading modules/charts_google/src/Plugin/chart/Library/Google.php +5 −1 Original line number Diff line number Diff line Loading @@ -297,13 +297,16 @@ class Google extends ChartBase { $series_number + 1 => $data_value[1], ]; } if ($chart_type['id'] === 'bubble') { elseif ($chart_type['id'] === 'bubble') { $chart_definition['data'][] = [ 0 => $data_value[0], $series_number + 1 => $data_value[1], $series_number + 2 => $data_value[2], ]; } else { $chart_definition['data'][$index + 1] = $data_value; } } // Most charts provide a single-dimension array of values. else { Loading Loading @@ -407,6 +410,7 @@ class Google extends ChartBase { // Ensure consistent column count. $column_count = count($data[0]); foreach ($data as $row => $values) { for ($n = 0; $n < $column_count; $n++) { Loading src/Element/Chart.php +47 −49 Original line number Diff line number Diff line Loading @@ -188,7 +188,7 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { // Include the library specific render callback via their plugin manager. // Use the first charting library if the requested library is not available. $library = isset($element['#chart_library']) ? $element['#chart_library'] : ''; $library = $element['#chart_library'] ?? ''; $library = $this->getLibrary($library); $element['#chart_library'] = $library; Loading Loading @@ -307,12 +307,9 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { */ public static function buildElement(array $settings, $chart_id) { $type = $settings['type']; $single_axis = in_array($type, ['pie', 'donut']); $display_colors = $settings['display']['colors'] ?? []; // Overriding element colors for pie and donut chart types when the // settings display colors is empty. $overrides_element_colors = !$display_colors && ($type === 'pie' || $type === 'donut'); $element = [ '#type' => 'chart', '#chart_type' => $type, Loading @@ -323,6 +320,8 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { '#data_labels' => $settings['display']['data_labels'] ?? [], '#colors' => $display_colors, '#background' => $settings['display']['background'] ?? 'transparent', '#three_dimensional' => $settings['display']['three_dimensional'] ?? FALSE, '#polar' => $settings['display']['polar'] ?? FALSE, '#legend' => !empty($settings['display']['legend_position']), '#legend_position' => $settings['display']['legend_position'] ?? '', '#gauge' => $settings['display']['gauge'] ?? [], Loading @@ -336,14 +335,14 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { if (!empty($settings['series'])) { $table = $settings['series']; // Extracting the categories. $categories = ChartDataCollectorTable::getCategoriesFromCollectedTable($table); $categories = ChartDataCollectorTable::getCategoriesFromCollectedTable($table, $type); // Extracting the rest of the data. $series_data = ChartDataCollectorTable::getSeriesFromCollectedTable($table, $type); $element['xaxis'] = [ '#type' => 'chart_xaxis', '#labels' => $categories['data'], '#title' => $settings['xaxis']['title'] ? $settings['xaxis']['title'] : FALSE, '#labels' => $single_axis ? '' : $categories['data'], '#title' => $settings['xaxis']['title'] ?? FALSE, '#labels_rotation' => $settings['xaxis']['labels_rotation'], ]; Loading @@ -351,7 +350,7 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { $element['yaxis'] = [ '#type' => 'chart_yaxis', '#title' => $settings['yaxis']['title'] ? $settings['yaxis']['title'] : '', '#title' => $settings['yaxis']['title'] ?? '', '#labels_rotation' => $settings['yaxis']['labels_rotation'], '#max' => $settings['yaxis']['max'], '#min' => $settings['yaxis']['min'], Loading @@ -365,7 +364,7 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { if (!empty($settings['yaxis']['inherit']) && $series_count === 2) { $element['secondary_yaxis'] = [ '#type' => 'chart_yaxis', '#title' => $settings['yaxis']['secondary']['title'] ? $settings['yaxis']['secondary']['title'] : '', '#title' => $settings['yaxis']['secondary']['title'] ?? '', '#labels_rotation' => $settings['yaxis']['secondary']['labels_rotation'], '#max' => $settings['yaxis']['secondary']['max'], '#min' => $settings['yaxis']['secondary']['min'], Loading @@ -376,10 +375,29 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { ]; } $series_counter = 0; // Overriding element colors for pie and donut chart types when the // settings display colors is empty. $overrides_element_colors = !$display_colors && ($type === 'pie' || $type === 'donut'); if ($single_axis) { $new_series = []; $labels = []; foreach ($series_data as $datum) { $new_series[] = $datum['data'][0][1]; $labels[] = $datum['name']; if ($overrides_element_colors) { $element['#colors'][] = $datum['color']; } } $element['xaxis']['#labels'] = $labels; // @todo Address more than one series. $element[$chart_id] = [ '#type' => 'chart_data', '#data' => $new_series, '#title' => $series_data[0]['title'] ?? '', ]; } else { $series_counter = 0; foreach ($series_data as $data_index => $data) { $key = $chart_id . '_' . $data_index; $element[$key] = [ Loading @@ -389,9 +407,6 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { ]; if (!empty($data['color'])) { $element[$key]['#color'] = $data['color']; if ($overrides_element_colors) { $element['#colors'][] = $data['color']; } } if (isset($element['yaxis'])) { $element[$key]['#prefix'] = $settings['yaxis']['prefix']; Loading @@ -407,23 +422,6 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { $series_counter++; } } else { $element[$chart_id] = [ '#type' => 'chart_data', '#title' => $series_data[0]['name'], '#data' => $series_data[0]['data'], '#prefix' => $settings['xaxis']['prefix'] ?? '', '#suffix' => $settings['xaxis']['suffix'] ?? '', ]; if (!empty($series_data[0]['color'])) { $element[$chart_id]['#color'] = $series_data[0]['color']; if ($overrides_element_colors) { $element['#colors'][] = $series_data[0]['color']; } } $element['xaxis'] += [ '#axis_type' => $settings['type'], ]; } } Loading src/Element/ChartDataCollectorTable.php +32 −11 Original line number Diff line number Diff line Loading @@ -726,13 +726,15 @@ class ChartDataCollectorTable extends FormElement { * * @param array $data * The data. * @param string $type * The chart type. * * @return array * The category label and data. */ public static function getCategoriesFromCollectedTable(array $data) { public static function getCategoriesFromCollectedTable(array $data, $type) { $categories_identifier = $data['table_categories_identifier']; $table = $data_table = $data['data_collector_table']; $table = $data['data_collector_table']; $categories = [ 'label' => '', 'data' => [], Loading @@ -744,6 +746,7 @@ class ChartDataCollectorTable extends FormElement { $categories['label'] = $first_row[$category_col_key]; $data = []; if ($is_first_column) { if (!in_array($type, ['pie', 'donut'])) { // Extracting the categories data. $col_cells = array_column($table, $category_col_key); foreach ($col_cells as $cell) { Loading @@ -756,6 +759,13 @@ class ChartDataCollectorTable extends FormElement { $data[] = is_array($cell) ? $cell['data'] : $cell; } } } else { $col_cells = array_values($first_row); foreach ($col_cells as $cell) { $data[] = is_array($cell) ? $cell['data'] : $cell; } } $categories['data'] = $data; // Removing the category label from categories. $categories_data = $categories['data']; Loading Loading @@ -806,6 +816,15 @@ class ChartDataCollectorTable extends FormElement { unset($row[$name_key]); foreach (array_values($row) as $column) { // Get all the data in this column and break out of this loop. if ($is_single_axis) { if (is_numeric($column) || is_string($column)) { $series[$i]['data'][] = [$series[$i]['name'], self::castValueToNumeric($column)]; } elseif (is_array($column) && isset($column['data'])) { $series[$i]['data'][] = [$series[$i]['name'], self::castValueToNumeric($column['data'])]; } } else { if (is_numeric($column) || is_string($column)) { $series[$i]['data'][] = self::castValueToNumeric($column); } Loading @@ -814,6 +833,7 @@ class ChartDataCollectorTable extends FormElement { } } } } else { $j = 0; foreach ($row as $column_key => $column) { Loading @@ -831,7 +851,8 @@ class ChartDataCollectorTable extends FormElement { $cell_value = is_array($column) && isset($column['data']) ? $column['data'] : $column; $cell_value = self::castValueToNumeric($cell_value); if ($is_single_axis) { $series[$j]['data'][] = [$row[$j]['data'], $cell_value]; $series[$j]['data'][] = [$series[$j]['name'], $cell_value]; $series[$j]['title'][] = $row[0]['data']; } else { $series[$j]['data'][] = $cell_value; Loading Loading
modules/charts_api_example/src/Controller/ChartsApiExample.php +1 −0 Original line number Diff line number Diff line Loading @@ -311,6 +311,7 @@ class ChartsApiExample extends ControllerBase { 'x_axis' => [ '#type' => 'chart_xaxis', '#title' => $this->t('Height'), '#labels' => [], ], 'y_axis' => [ '#type' => 'chart_yaxis', Loading
modules/charts_chartjs/src/Plugin/chart/Library/Chartjs.php +2 −2 Original line number Diff line number Diff line Loading @@ -239,7 +239,7 @@ class Chartjs extends ChartBase { foreach ($children as $child) { $type = $element[$child]['#type']; if ($type === 'chart_xaxis') { $categories = array_map('strip_tags', $element[$child]['#labels']); $categories = is_array($element[$child]['#labels']) ? array_map('strip_tags', $element[$child]['#labels']): []; // Merge in axis raw options. if (!empty($element[$child]['#raw_options'])) { $categories = NestedArray::mergeDeepArray([$element[$child]['#raw_options'], $categories,]); Loading Loading @@ -308,7 +308,7 @@ class Chartjs extends ChartBase { if (!in_array($chart_type, ['pie', 'doughnut'])) { $dataset->borderColor = $element[$key]['#color']; } $dataset->backgroundColor = $element[$key]['#color']; $dataset->backgroundColor = $element[$key]['#color'] ?? '#000000'; // $series_type = isset($element[$key]['#chart_type']) ? $this->populateChartType($element[$key]) : $chart_type; $dataset->type = $series_type; if (!empty($element[$key]['#chart_type']) && $element[$key]['#chart_type'] === 'area') { Loading
modules/charts_google/src/Plugin/chart/Library/Google.php +5 −1 Original line number Diff line number Diff line Loading @@ -297,13 +297,16 @@ class Google extends ChartBase { $series_number + 1 => $data_value[1], ]; } if ($chart_type['id'] === 'bubble') { elseif ($chart_type['id'] === 'bubble') { $chart_definition['data'][] = [ 0 => $data_value[0], $series_number + 1 => $data_value[1], $series_number + 2 => $data_value[2], ]; } else { $chart_definition['data'][$index + 1] = $data_value; } } // Most charts provide a single-dimension array of values. else { Loading Loading @@ -407,6 +410,7 @@ class Google extends ChartBase { // Ensure consistent column count. $column_count = count($data[0]); foreach ($data as $row => $values) { for ($n = 0; $n < $column_count; $n++) { Loading
src/Element/Chart.php +47 −49 Original line number Diff line number Diff line Loading @@ -188,7 +188,7 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { // Include the library specific render callback via their plugin manager. // Use the first charting library if the requested library is not available. $library = isset($element['#chart_library']) ? $element['#chart_library'] : ''; $library = $element['#chart_library'] ?? ''; $library = $this->getLibrary($library); $element['#chart_library'] = $library; Loading Loading @@ -307,12 +307,9 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { */ public static function buildElement(array $settings, $chart_id) { $type = $settings['type']; $single_axis = in_array($type, ['pie', 'donut']); $display_colors = $settings['display']['colors'] ?? []; // Overriding element colors for pie and donut chart types when the // settings display colors is empty. $overrides_element_colors = !$display_colors && ($type === 'pie' || $type === 'donut'); $element = [ '#type' => 'chart', '#chart_type' => $type, Loading @@ -323,6 +320,8 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { '#data_labels' => $settings['display']['data_labels'] ?? [], '#colors' => $display_colors, '#background' => $settings['display']['background'] ?? 'transparent', '#three_dimensional' => $settings['display']['three_dimensional'] ?? FALSE, '#polar' => $settings['display']['polar'] ?? FALSE, '#legend' => !empty($settings['display']['legend_position']), '#legend_position' => $settings['display']['legend_position'] ?? '', '#gauge' => $settings['display']['gauge'] ?? [], Loading @@ -336,14 +335,14 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { if (!empty($settings['series'])) { $table = $settings['series']; // Extracting the categories. $categories = ChartDataCollectorTable::getCategoriesFromCollectedTable($table); $categories = ChartDataCollectorTable::getCategoriesFromCollectedTable($table, $type); // Extracting the rest of the data. $series_data = ChartDataCollectorTable::getSeriesFromCollectedTable($table, $type); $element['xaxis'] = [ '#type' => 'chart_xaxis', '#labels' => $categories['data'], '#title' => $settings['xaxis']['title'] ? $settings['xaxis']['title'] : FALSE, '#labels' => $single_axis ? '' : $categories['data'], '#title' => $settings['xaxis']['title'] ?? FALSE, '#labels_rotation' => $settings['xaxis']['labels_rotation'], ]; Loading @@ -351,7 +350,7 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { $element['yaxis'] = [ '#type' => 'chart_yaxis', '#title' => $settings['yaxis']['title'] ? $settings['yaxis']['title'] : '', '#title' => $settings['yaxis']['title'] ?? '', '#labels_rotation' => $settings['yaxis']['labels_rotation'], '#max' => $settings['yaxis']['max'], '#min' => $settings['yaxis']['min'], Loading @@ -365,7 +364,7 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { if (!empty($settings['yaxis']['inherit']) && $series_count === 2) { $element['secondary_yaxis'] = [ '#type' => 'chart_yaxis', '#title' => $settings['yaxis']['secondary']['title'] ? $settings['yaxis']['secondary']['title'] : '', '#title' => $settings['yaxis']['secondary']['title'] ?? '', '#labels_rotation' => $settings['yaxis']['secondary']['labels_rotation'], '#max' => $settings['yaxis']['secondary']['max'], '#min' => $settings['yaxis']['secondary']['min'], Loading @@ -376,10 +375,29 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { ]; } $series_counter = 0; // Overriding element colors for pie and donut chart types when the // settings display colors is empty. $overrides_element_colors = !$display_colors && ($type === 'pie' || $type === 'donut'); if ($single_axis) { $new_series = []; $labels = []; foreach ($series_data as $datum) { $new_series[] = $datum['data'][0][1]; $labels[] = $datum['name']; if ($overrides_element_colors) { $element['#colors'][] = $datum['color']; } } $element['xaxis']['#labels'] = $labels; // @todo Address more than one series. $element[$chart_id] = [ '#type' => 'chart_data', '#data' => $new_series, '#title' => $series_data[0]['title'] ?? '', ]; } else { $series_counter = 0; foreach ($series_data as $data_index => $data) { $key = $chart_id . '_' . $data_index; $element[$key] = [ Loading @@ -389,9 +407,6 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { ]; if (!empty($data['color'])) { $element[$key]['#color'] = $data['color']; if ($overrides_element_colors) { $element['#colors'][] = $data['color']; } } if (isset($element['yaxis'])) { $element[$key]['#prefix'] = $settings['yaxis']['prefix']; Loading @@ -407,23 +422,6 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface { $series_counter++; } } else { $element[$chart_id] = [ '#type' => 'chart_data', '#title' => $series_data[0]['name'], '#data' => $series_data[0]['data'], '#prefix' => $settings['xaxis']['prefix'] ?? '', '#suffix' => $settings['xaxis']['suffix'] ?? '', ]; if (!empty($series_data[0]['color'])) { $element[$chart_id]['#color'] = $series_data[0]['color']; if ($overrides_element_colors) { $element['#colors'][] = $series_data[0]['color']; } } $element['xaxis'] += [ '#axis_type' => $settings['type'], ]; } } Loading
src/Element/ChartDataCollectorTable.php +32 −11 Original line number Diff line number Diff line Loading @@ -726,13 +726,15 @@ class ChartDataCollectorTable extends FormElement { * * @param array $data * The data. * @param string $type * The chart type. * * @return array * The category label and data. */ public static function getCategoriesFromCollectedTable(array $data) { public static function getCategoriesFromCollectedTable(array $data, $type) { $categories_identifier = $data['table_categories_identifier']; $table = $data_table = $data['data_collector_table']; $table = $data['data_collector_table']; $categories = [ 'label' => '', 'data' => [], Loading @@ -744,6 +746,7 @@ class ChartDataCollectorTable extends FormElement { $categories['label'] = $first_row[$category_col_key]; $data = []; if ($is_first_column) { if (!in_array($type, ['pie', 'donut'])) { // Extracting the categories data. $col_cells = array_column($table, $category_col_key); foreach ($col_cells as $cell) { Loading @@ -756,6 +759,13 @@ class ChartDataCollectorTable extends FormElement { $data[] = is_array($cell) ? $cell['data'] : $cell; } } } else { $col_cells = array_values($first_row); foreach ($col_cells as $cell) { $data[] = is_array($cell) ? $cell['data'] : $cell; } } $categories['data'] = $data; // Removing the category label from categories. $categories_data = $categories['data']; Loading Loading @@ -806,6 +816,15 @@ class ChartDataCollectorTable extends FormElement { unset($row[$name_key]); foreach (array_values($row) as $column) { // Get all the data in this column and break out of this loop. if ($is_single_axis) { if (is_numeric($column) || is_string($column)) { $series[$i]['data'][] = [$series[$i]['name'], self::castValueToNumeric($column)]; } elseif (is_array($column) && isset($column['data'])) { $series[$i]['data'][] = [$series[$i]['name'], self::castValueToNumeric($column['data'])]; } } else { if (is_numeric($column) || is_string($column)) { $series[$i]['data'][] = self::castValueToNumeric($column); } Loading @@ -814,6 +833,7 @@ class ChartDataCollectorTable extends FormElement { } } } } else { $j = 0; foreach ($row as $column_key => $column) { Loading @@ -831,7 +851,8 @@ class ChartDataCollectorTable extends FormElement { $cell_value = is_array($column) && isset($column['data']) ? $column['data'] : $column; $cell_value = self::castValueToNumeric($cell_value); if ($is_single_axis) { $series[$j]['data'][] = [$row[$j]['data'], $cell_value]; $series[$j]['data'][] = [$series[$j]['name'], $cell_value]; $series[$j]['title'][] = $row[0]['data']; } else { $series[$j]['data'][] = $cell_value; Loading