Boxplot: Do not attempt to draw whiskers if they are not defined
[pspp] / src / output / charts / boxplot-cairo.c
index 6aaa600be99d8eda6733d9e7d85274dea34a2825..d798bc2abbdbd6abd3f2de087d7549634fdb9539 100644 (file)
@@ -113,14 +113,20 @@ boxplot_draw_box (cairo_t *cr, const struct xrchart_geometry *geom,
 
   /* Draw centre line.
      (bottom half) */
-  cairo_move_to (cr, box_centre, bottom_whisker);
-  cairo_line_to (cr, box_centre, box_bottom);
-  cairo_stroke (cr);
+  if (! isnan (whisker[0]))
+    {
+      cairo_move_to (cr, box_centre, bottom_whisker);
+      cairo_line_to (cr, box_centre, box_bottom);
+      cairo_stroke (cr);
+    }
 
-  /* (top half) */
-  cairo_move_to (cr, box_centre, top_whisker);
-  cairo_line_to (cr, box_centre, box_top);
-  cairo_stroke (cr);
+  if (! isnan (whisker[1]))
+    {
+      /* (top half) */
+      cairo_move_to (cr, box_centre, top_whisker);
+      cairo_line_to (cr, box_centre, box_top);
+      cairo_stroke (cr);
+    }
 
   outliers = box_whisker_outliers (bw);
   for (ll = ll_head (outliers);
@@ -131,31 +137,8 @@ boxplot_draw_box (cairo_t *cr, const struct xrchart_geometry *geom,
     }
 
   /* Draw  tick  mark on x axis */
-  draw_tick(cr, geom, SCALE_ABSCISSA, box_centre - geom->axis[SCALE_ABSCISSA].data_min, "%s", name);
-}
-
-static void
-boxplot_draw_yscale (cairo_t *cr, struct xrchart_geometry *geom,
-                     double y_max, double y_min)
-{
-  double y_tick;
-  double d;
-
-  geom->axis[SCALE_ORDINATE].max = y_max;
-  geom->axis[SCALE_ORDINATE].min = y_min;
-
-  y_tick = chart_rounded_tick (fabs (geom->axis[SCALE_ORDINATE].max - geom->axis[SCALE_ORDINATE].min) / 5.0);
-
-  geom->axis[SCALE_ORDINATE].min = (ceil (geom->axis[SCALE_ORDINATE].min / y_tick) - 1.0) * y_tick;
-
-  geom->axis[SCALE_ORDINATE].max = (floor (geom->axis[SCALE_ORDINATE].max / y_tick) + 1.0) * y_tick;
-
-  geom->axis[SCALE_ORDINATE].scale = (fabs (geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min)
-                          / fabs (geom->axis[SCALE_ORDINATE].max - geom->axis[SCALE_ORDINATE].min));
-
-  for (d = geom->axis[SCALE_ORDINATE].min; d <= geom->axis[SCALE_ORDINATE].max; d += y_tick)
-    draw_tick (cr, geom, SCALE_ORDINATE,
-               (d - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale, "%g", d);
+  draw_tick (cr, geom, SCALE_ABSCISSA, false,
+            box_centre - geom->axis[SCALE_ABSCISSA].data_min, "%s", name);
 }
 
 void
@@ -166,7 +149,7 @@ xrchart_draw_boxplot (const struct chart_item *chart_item, cairo_t *cr,
   double box_width;
   size_t i;
 
-  boxplot_draw_yscale (cr, geom, boxplot->y_max, boxplot->y_min);
+  xrchart_write_yscale (cr, geom, boxplot->y_min, boxplot->y_max);
   xrchart_write_title (cr, geom, "%s", chart_item->title);
 
   box_width = (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / boxplot->n_boxes / 2.0;