Piecharts: Avoid the segment fill obscuring the labels
[pspp] / src / output / charts / piechart-cairo.c
index 501c0bd466adb7cf2d391ba0ec6d86d5285afcd0..bd0452ca3bfe8ae9ea55e2b2449458b44b192d70 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2009 Free Software Foundation, Inc.
+   Copyright (C) 2009, 2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <config.h>
 
-#include <output/charts/piechart.h>
+#include "output/charts/piechart.h"
 
 #include <math.h>
 
-#include <output/cairo-chart.h>
+#include "output/cairo-chart.h"
 
 #include "gl/minmax.h"
 
@@ -57,17 +57,17 @@ xrchart_draw_piechart (const struct chart_item *chart_item, cairo_t *cr,
   double angle;
   int i;
 
-  centre_x = (geom->data_right + geom->data_left) / 2.0 ;
-  centre_y = (geom->data_top + geom->data_bottom) / 2.0 ;
+  centre_x = (geom->axis[SCALE_ABSCISSA].data_max + geom->axis[SCALE_ORDINATE].data_min) / 2.0 ;
+  centre_y = (geom->axis[SCALE_ORDINATE].data_max + geom->axis[SCALE_ORDINATE].data_min) / 2.0 ;
 
-  left_label = geom->data_left + (geom->data_right - geom->data_left)/10.0;
-  right_label = geom->data_right - (geom->data_right - geom->data_left)/10.0;
+  left_label = geom->axis[SCALE_ORDINATE].data_min + (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min)/10.0;
+  right_label = geom->axis[SCALE_ABSCISSA].data_max - (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min)/10.0;
 
-  radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
-                1.0 / 4.0 * (geom->data_right - geom->data_left));
+  radius = MIN (5.0 / 12.0 * (geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min),
+                1.0 / 4.0 * (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min));
 
-  radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
-                1.0 / 4.0 * (geom->data_right - geom->data_left));
+  radius = MIN (5.0 / 12.0 * (geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min),
+                1.0 / 4.0 * (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min));
 
   xrchart_write_title (cr, geom, "%s", chart_item_get_title (chart_item));
 
@@ -75,25 +75,41 @@ xrchart_draw_piechart (const struct chart_item *chart_item, cairo_t *cr,
   for (i = 0; i < pie->n_slices; i++)
     total_magnitude += pie->slices[i].magnitude;
 
+
+  /* Draw the segments */
   angle = 0.0;
   for (i = 0; i < pie->n_slices ; ++i )
     {
       const double segment_angle =
        pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
 
-      const double label_x = centre_x -
-       radius * sin(angle + segment_angle/2.0);
-
-      const double label_y = centre_y +
-       radius * cos(angle + segment_angle/2.0);
-
       /* Fill the segment */
       draw_segment (cr,
                     centre_x, centre_y, radius,
                     angle, segment_angle,
                     &data_colour[i % XRCHART_N_COLOURS]);
 
-      /* Now add the labels */
+      angle += segment_angle;
+    }
+
+
+  /* Now add the labels.
+     Don't put this in the loop above;  the labels must
+     be put in last, otherwise the segment fill could
+     obscure them.
+   */
+  angle = 0.0;
+  for (i = 0; i < pie->n_slices ; ++i )
+    {
+      const double segment_angle =
+       pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
+
+      const double label_x = centre_x +
+       radius * cos (angle + segment_angle/2.0);
+
+      const double label_y = centre_y +
+       radius * sin (angle + segment_angle/2.0);
+
       if ( label_x < centre_x )
        {
           cairo_move_to (cr, label_x, label_y);