numbers back in tables
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 16 Jan 2022 04:50:36 +0000 (20:50 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 16 Jan 2022 04:50:36 +0000 (20:50 -0800)
src/language/stats/ctables.c

index bba0fb67fcf0859457e724f276a43dbc580bbc6f..3240d48dd056bdcd4d2d24e3e6d11a2e62be881f 100644 (file)
@@ -1959,7 +1959,7 @@ ctables_summary_add (union ctables_summary *s,
     }
 }
 
-static double UNUSED
+static double
 ctables_summary_value (const struct ctables_cell *cell,
                        union ctables_summary *s,
                        const struct ctables_summary_spec *ss)
@@ -2531,7 +2531,7 @@ ctables_category_create_label (const struct ctables_category *cat,
 }
 
 static void
-ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t)
+ctables_table_output (struct ctables *ct, struct ctables_table *t)
 {
   struct pivot_table *pt = pivot_table_create__ (
     (t->title
@@ -2545,7 +2545,8 @@ ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t
     pivot_table_set_caption (
       pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
 
-  if (t->summary_axis != t->slabels_axis)
+  bool summary_dimension = t->summary_axis != t->slabels_axis;
+  if (summary_dimension)
     {
       struct pivot_dimension *d = pivot_dimension_create (
         pt, t->slabels_axis, N_("Summaries"));
@@ -2555,7 +2556,8 @@ ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t
           d->root, pivot_value_new_text (specs->specs[i].label));
     }
 
-  if (t->clabels_example)
+  bool categories_dimension = t->clabels_example != NULL;
+  if (categories_dimension)
     {
       struct pivot_dimension *d = pivot_dimension_create (
         pt, t->label_axis[t->clabels_from_axis],
@@ -2726,8 +2728,12 @@ ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t
                 {
                   const struct ctables_summary_spec_set *specs = &t->summary_specs;
                   for (size_t m = 0; m < specs->n; m++)
-                    pivot_category_create_leaf (
-                      parent, pivot_value_new_text (specs->specs[m].label));
+                    {
+                      int leaf = pivot_category_create_leaf (
+                        parent, pivot_value_new_text (specs->specs[m].label));
+                      if (!m)
+                        prev_leaf = leaf;
+                    }
                 }
               else
                 {
@@ -2745,7 +2751,7 @@ ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t
                     NOT_REACHED ();
 
                   if (k == n_levels - 1)
-                    pivot_category_create_leaf (parent, label);
+                    prev_leaf = pivot_category_create_leaf (parent, label);
                   else
                     groups[k] = pivot_category_create_group__ (parent, label);
                 }
@@ -2756,6 +2762,44 @@ ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t
       free (sorted);
       free (groups);
     }
+
+  struct ctables_cell *cell;
+  HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
+    {
+      if (cell->hide)
+        continue;
+
+      const struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].stack_idx];
+      const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
+      for (size_t j = 0; j < specs->n; j++)
+        {
+          size_t dindexes[5];
+          size_t n_dindexes = 0;
+
+          if (summary_dimension)
+            dindexes[n_dindexes++] = specs->specs[j].axis_idx;
+
+          if (categories_dimension)
+            {
+              dindexes[n_dindexes++] = 0; /* XXX */
+            }
+
+          for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+            if (d[a])
+              {
+                int leaf = cell->axes[a].leaf;
+                if (a == t->summary_axis && !summary_dimension)
+                  leaf += j;
+                dindexes[n_dindexes++] = leaf;
+              }
+
+          double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
+          struct pivot_value *value = pivot_value_new_number (d);
+          value->numeric.format = specs->specs[j].format;
+          pivot_table_put (pt, dindexes, n_dindexes, value);
+        }
+    }
+
   pivot_table_submit (pt);
 }
 
@@ -3009,7 +3053,7 @@ ctables_execute (struct dataset *ds, struct ctables *ct)
       if (t->clabels_example)
         ctables_sort_clabels_values (t);
 
-      ctables_table_output_different_axis (ct, ct->tables[i]);
+      ctables_table_output (ct, ct->tables[i]);
     }
   return proc_commit (ds);
 }