Piecharts: Avoid the segment fill obscuring the labels
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 18 Jan 2015 14:19:10 +0000 (15:19 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Mon, 19 Jan 2015 17:02:25 +0000 (18:02 +0100)
src/output/charts/piechart-cairo.c

index 7b847f123ef43663b7e26a8660fd73b68671b85a..bd0452ca3bfe8ae9ea55e2b2449458b44b192d70 100644 (file)
@@ -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 * cos (angle + segment_angle/2.0);
-
-      const double label_y = centre_y +
-       radius * sin (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);