[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Pivot table: New feature to suppress the display of rows/cols wh
From: |
John Darrington |
Subject: |
[PATCH] Pivot table: New feature to suppress the display of rows/cols which are empty. |
Date: |
Thu, 13 Jun 2019 16:43:58 +0200 |
This change adds a feature to the pivot table implementation such that columns
and/or rows which are completely empty (except for the headings) are not
rendered.
* src/output/pivot-output.c (pivot_table_submit_layer): Keep an array recording
the occupancy of each row and column. (compose_headings): Use it to hide empty
rows/columns.
* src/output/pivot-table.h [struct pivot_table]: New members hide_empty_columns,
hide_empty_rows.
---
src/output/pivot-output.c | 83 ++++++++++++++++++++++++++++++-----------------
src/output/pivot-table.h | 2 ++
2 files changed, 56 insertions(+), 29 deletions(-)
diff --git a/src/output/pivot-output.c b/src/output/pivot-output.c
index 69a6f7fec..27ece3dc4 100644
--- a/src/output/pivot-output.c
+++ b/src/output/pivot-output.c
@@ -22,6 +22,7 @@
#include "data/settings.h"
#include "libpspp/assertion.h"
+#include "libpspp/sparse-array.h"
#include "libpspp/pool.h"
#include "output/tab.h"
#include "output/table.h"
@@ -190,7 +191,8 @@ compose_headings (struct tab_table *t,
struct footnote **footnotes,
enum settings_value_show show_values,
enum settings_value_show show_variables,
- bool rotate_inner_labels, bool rotate_outer_labels)
+ bool rotate_inner_labels, bool rotate_outer_labels,
+ const struct sparse_array *occupied)
{
enum table_axis b = !a;
int b_size = a_axis->label_depth;
@@ -213,7 +215,7 @@ compose_headings (struct tab_table *t,
{
const struct pivot_category *c = find_category (
d, dim_index, column_enumeration + x1 * stride, row_ofs);
- if (!c)
+ if (!c || !sparse_array_get (occupied, x1))
{
x1++;
continue;
@@ -363,6 +365,50 @@ pivot_table_submit_layer (const struct pivot_table *pt,
free (content);
}
+ size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
+ size_t y = 0;
+ struct sparse_array *occupied_rows = sparse_array_create (1);
+ struct sparse_array *occupied_columns = sparse_array_create (1);
+ PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
+ &pt->axes[PIVOT_AXIS_ROW])
+ {
+ size_t x = 0;
+ if (! pt->hide_empty_rows)
+ sparse_array_insert (occupied_rows, y);
+
+ PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
+ column_enumeration,
+ &pt->axes[PIVOT_AXIS_COLUMN])
+ {
+ if (! pt->hide_empty_columns)
+ {
+ if (! sparse_array_get (occupied_columns, x))
+ sparse_array_insert (occupied_columns, x);
+ }
+ pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
+ const struct pivot_value *value = pivot_table_get (pt, dindexes);
+ if (value)
+ {
+ if (! sparse_array_get (occupied_columns, x))
+ sparse_array_insert (occupied_columns, x);
+
+ if (! sparse_array_get (occupied_rows, y))
+ sparse_array_insert (occupied_rows, y);
+ }
+ fill_cell (table,
+ x + stub[H], y + stub[V],
+ x + stub[H], y + stub[V],
+ &pt->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
+ value, footnotes,
+ pt->show_values, pt->show_variables, false);
+
+ x++;
+ }
+
+ y++;
+ }
+ free (dindexes);
+
compose_headings (table,
&pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
pt->borders,
@@ -375,7 +421,8 @@ pivot_table_submit_layer (const struct pivot_table *pt,
PIVOT_AREA_COLUMN_LABELS,
&pt->areas[PIVOT_AREA_CORNER], footnotes,
pt->show_values, pt->show_variables,
- pt->rotate_inner_column_labels, false);
+ pt->rotate_inner_column_labels, false,
+ occupied_columns);
compose_headings (table,
&pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
@@ -389,33 +436,11 @@ pivot_table_submit_layer (const struct pivot_table *pt,
PIVOT_AREA_ROW_LABELS,
&pt->areas[PIVOT_AREA_CORNER], footnotes,
pt->show_values, pt->show_variables,
- false, pt->rotate_outer_row_labels);
+ false, pt->rotate_outer_row_labels,
+ occupied_rows);
- size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
- size_t y = 0;
- PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
- &pt->axes[PIVOT_AXIS_ROW])
- {
- size_t x = 0;
- PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
- column_enumeration,
- &pt->axes[PIVOT_AXIS_COLUMN])
- {
- pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
- const struct pivot_value *value = pivot_table_get (pt, dindexes);
- fill_cell (table,
- x + stub[H], y + stub[V],
- x + stub[H], y + stub[V],
- &pt->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
- value, footnotes,
- pt->show_values, pt->show_variables, false);
-
- x++;
- }
-
- y++;
- }
- free (dindexes);
+ sparse_array_destroy (occupied_rows);
+ sparse_array_destroy (occupied_columns);
if (pt->corner_text && stub[H] && stub[V])
fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
diff --git a/src/output/pivot-table.h b/src/output/pivot-table.h
index 6104cb131..1164abb1d 100644
--- a/src/output/pivot-table.h
+++ b/src/output/pivot-table.h
@@ -371,6 +371,8 @@ struct pivot_table
bool row_labels_in_corner;
bool show_grid_lines;
bool omit_empty;
+ bool hide_empty_columns;
+ bool hide_empty_rows;
size_t *current_layer; /* axis[PIVOT_AXIS_LAYER]->n_dimensions elements. */
char *table_look;
enum settings_value_show show_values;
--
2.11.0
- [PATCH] Pivot table: New feature to suppress the display of rows/cols which are empty.,
John Darrington <=