output: Refactor implementation of charts.
[pspp-builds.git] / src / output / charts / plot-chart.c
index 3b4f1b377a12a624ceb929d00c617bb4bd581f30..75f21b66d051b6171b559c05d29032af5504d622 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2009 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
@@ -16,6 +16,8 @@
 
 #include <config.h>
 
+#include <output/charts/plot-chart.h>
+
 #include <stdio.h>
 #include <plot.h>
 #include <stdarg.h>
 #include <assert.h>
 #include <math.h>
 
-#include <output/charts/plot-chart.h>
-
-#include <math/chart-geometry.h>
-
-
-
-#include <libpspp/str.h>
 #include <libpspp/assertion.h>
+#include <libpspp/str.h>
+#include <math/chart-geometry.h>
+#include <output/chart-provider.h>
 #include <output/manager.h>
 #include <output/output.h>
 
@@ -57,156 +55,130 @@ const char *const data_colour[N_CHART_COLOURS] =
    If label is non zero, then print it at the tick mark
 */
 void
-draw_tick(struct chart *chart,
-         enum tick_orientation orientation,
-         double position,
-         const char *label, ...)
+draw_tick (plPlotter *lp, const struct chart_geometry *geom,
+           enum tick_orientation orientation,
+           double position,
+           const char *label, ...)
 {
   const int tickSize = 10;
 
-  assert(chart);
-
-  pl_savestate_r(chart->lp);
-
-  pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
+  pl_savestate_r (lp);
+  pl_move_r (lp, geom->data_left, geom->data_bottom);
 
-  if ( orientation == TICK_ABSCISSA )
-    pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
-  else if (orientation == TICK_ORDINATE )
-      pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
+  if (orientation == TICK_ABSCISSA)
+    pl_flinerel_r (lp, position, 0, position, -tickSize);
+  else if (orientation == TICK_ORDINATE)
+    pl_flinerel_r (lp, 0, position, -tickSize, position);
   else
     NOT_REACHED ();
 
-  if ( label ) {
-    char buf[10];
-    va_list ap;
-    va_start(ap,label);
-    vsnprintf(buf,10,label,ap);
-
-    if ( orientation == TICK_ABSCISSA )
-      pl_alabel_r(chart->lp, 'c','t', buf);
-    else if (orientation == TICK_ORDINATE )
-      {
-       if ( fabs(position) < DBL_EPSILON )
-           pl_moverel_r(chart->lp, 0, 10);
-
-       pl_alabel_r(chart->lp, 'r','c', buf);
-      }
-
-    va_end(ap);
-  }
+  if (label != NULL)
+    {
+      va_list ap;
+      char *s;
+
+      va_start (ap, label);
+      s = xvasprintf (label, ap);
+      if (orientation == TICK_ABSCISSA)
+        pl_alabel_r (lp, 'c', 't', s);
+      else if (orientation == TICK_ORDINATE)
+        {
+          if (fabs (position) < DBL_EPSILON)
+           pl_moverel_r (lp, 0, 10);
+          pl_alabel_r (lp, 'r', 'c', s);
+        }
+      free (s);
+      va_end (ap);
+    }
 
-  pl_restorestate_r(chart->lp);
+  pl_restorestate_r (lp);
 }
 
 
 /* Write the title on a chart*/
 void
-chart_write_title(struct chart *chart, const char *title, ...)
+chart_write_title (plPlotter *lp, const struct chart_geometry *geom,
+                   const char *title, ...)
 {
   va_list ap;
-  char buf[100];
+  char *s;
 
-  if ( ! chart )
-         return ;
+  pl_savestate_r (lp);
+  pl_ffontsize_r (lp, geom->font_size * 1.5);
+  pl_move_r (lp, geom->data_left, geom->title_bottom);
 
-  pl_savestate_r(chart->lp);
-  pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
-  pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
+  va_start(ap, title);
+  s = xvasprintf (title, ap);
+  pl_alabel_r (lp, 0, 0, s);
+  free (s);
+  va_end (ap);
 
-  va_start(ap,title);
-  vsnprintf(buf,100,title,ap);
-  pl_alabel_r(chart->lp,0,0,buf);
-  va_end(ap);
-
-  pl_restorestate_r(chart->lp);
+  pl_restorestate_r (lp);
 }
 
 
 /* Set the scale for the abscissa */
 void
-chart_write_xscale(struct chart *ch, double min, double max, int ticks)
+chart_write_xscale (plPlotter *lp, struct chart_geometry *geom,
+                    double min, double max, int ticks)
 {
   double x;
 
   const double tick_interval =
-    chart_rounded_tick( (max - min) / (double) ticks);
-
-  assert ( ch );
+    chart_rounded_tick ((max - min) / (double) ticks);
 
+  geom->x_max = ceil (max / tick_interval) * tick_interval;
+  geom->x_min = floor (min / tick_interval) * tick_interval;
+  geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
+    fabs(geom->x_max - geom->x_min);
 
-  ch->x_max = ceil( max / tick_interval ) * tick_interval ;
-  ch->x_min = floor ( min / tick_interval ) * tick_interval ;
-
-
-  ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
-    fabs(ch->x_max - ch->x_min);
-
-  for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
-    {
-      draw_tick (ch, TICK_ABSCISSA,
-                (x - ch->x_min) * ch->abscissa_scale, "%g", x);
-    }
-
+  for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
+    draw_tick (lp, geom, TICK_ABSCISSA,
+               (x - geom->x_min) * geom->abscissa_scale, "%g", x);
 }
 
 
 /* Set the scale for the ordinate */
 void
-chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
+chart_write_yscale (plPlotter *lp, struct chart_geometry *geom,
+                    double smin, double smax, int ticks)
 {
   double y;
 
   const double tick_interval =
-    chart_rounded_tick( (smax - smin) / (double) ticks);
-
-  if ( !ch )
-         return;
+    chart_rounded_tick ((smax - smin) / (double) ticks);
 
-  ch->y_max = ceil  ( smax / tick_interval ) * tick_interval ;
-  ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
+  geom->y_max = ceil (smax / tick_interval) * tick_interval;
+  geom->y_min = floor (smin / tick_interval) * tick_interval;
 
-  ch->ordinate_scale =
-    fabs(ch->data_top -  ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
+  geom->ordinate_scale =
+    (fabs (geom->data_top - geom->data_bottom)
+     / fabs (geom->y_max - geom->y_min));
 
-  for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
-    {
-    draw_tick (ch, TICK_ORDINATE,
-              (y - ch->y_min) * ch->ordinate_scale, "%g", y);
-    }
+  for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
+    draw_tick (lp, geom, TICK_ORDINATE,
+              (y - geom->y_min) * geom->ordinate_scale, "%g", y);
 }
 
-
 /* Write the abscissa label */
 void
-chart_write_xlabel(struct chart *ch, const char *label)
+chart_write_xlabel (plPlotter *lp, const struct chart_geometry *geom,
+                    const char *label)
 {
-  if ( ! ch )
-    return ;
-
-  pl_savestate_r(ch->lp);
-
-  pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
-  pl_alabel_r(ch->lp,0,'t',label);
-
-  pl_restorestate_r(ch->lp);
-
+  pl_savestate_r (lp);
+  pl_move_r (lp, geom->data_left, geom->abscissa_top);
+  pl_alabel_r (lp, 0, 't', label);
+  pl_restorestate_r (lp);
 }
 
-
-
 /* Write the ordinate label */
 void
-chart_write_ylabel(struct chart *ch, const char *label)
+chart_write_ylabel (plPlotter *lp, const struct chart_geometry *geom,
+                    const char *label)
 {
-  if ( ! ch )
-    return ;
-
-  pl_savestate_r(ch->lp);
-
-  pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
-  pl_textangle_r(ch->lp, 90);
-  pl_alabel_r(ch->lp, 0, 0, label);
-
-  pl_restorestate_r(ch->lp);
+  pl_savestate_r (lp);
+  pl_move_r (lp, geom->data_bottom, geom->ordinate_right);
+  pl_textangle_r (lp, 90);
+  pl_alabel_r (lp, 0, 0, label);
+  pl_restorestate_r(lp);
 }