Merge "master" into "output".
[pspp-builds.git] / src / output / charts / plot-hist.c
index 68c0a6a8d4173b971373a075f112354982614e79..334818b4cad9428975f8c2e1623ea567ff57ac5c 100644 (file)
-/* PSPP - computes sample statistics.
-   Copyright (C) 2004 Free Software Foundation, Inc.
-   Written by John Darrington <john@darrington.wattle.id.au>
+/* PSPP - a program for statistical analysis.
+   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 the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 
 #include <config.h>
 
 #include <stdio.h>
-#include "plot-hist.h"
-#include "plot-chart.h"
-#include <plot.h>
 #include <math.h>
 #include <gsl/gsl_histogram.h>
 #include <gsl/gsl_randist.h>
 #include <assert.h>
-#include "hash.h"
-#include "variable.h"
-#include "chart.h"
+
+#include <output/charts/plot-hist.h>
+#include <output/charts/plot-chart.h>
+#include <output/chart-provider.h>
+
+#include <data/variable.h>
+#include <libpspp/cast.h>
+#include <libpspp/hash.h>
+#include <output/chart.h>
+#include <math/histogram.h>
+#include <math/moments.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
+static const struct chart_class histogram_chart_class;
+
 /* Write the legend of the chart */
-void
-histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
+static void
+histogram_write_legend (cairo_t *cr, const struct chart_geometry *geom,
+                        double n, double mean, double stddev)
 {
-  char buf[100];
-  if ( !ch )
-    return ;
-
-  pl_savestate_r(ch->lp);
+  double y = geom->data_bottom;
+  cairo_save (cr);
 
-  sprintf(buf,"N = %.2f",norm->N);
-  pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
-  pl_alabel_r(ch->lp,0,'b',buf);
+  if (n != SYSMIS)
+    {
+      char *buf = xasprintf ("N = %.2f", n);
+      cairo_move_to (cr, geom->legend_left, y);
+      chart_label (cr, 'l', 'b', geom->font_size, buf);
+      y += geom->font_size * 1.5;
+      free (buf);
+    }
 
-  sprintf(buf,"Mean = %.1f",norm->mean);
-  pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
-  pl_alabel_r(ch->lp,0,'b',buf);
+  if (mean != SYSMIS)
+    {
+      char *buf = xasprintf ("Mean = %.1f", mean);
+      cairo_move_to (cr,geom->legend_left, y);
+      chart_label (cr, 'l', 'b', geom->font_size, buf);
+      y += geom->font_size * 1.5;
+      free (buf);
+    }
 
-  sprintf(buf,"Std. Dev = %.2f",norm->stddev);
-  pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
-  pl_alabel_r(ch->lp,0,'b',buf);
+  if (stddev != SYSMIS)
+    {
+      char *buf = xasprintf ("Std. Dev = %.2f", stddev);
+      cairo_move_to (cr, geom->legend_left, y);
+      chart_label (cr, 'l', 'b', geom->font_size, buf);
+      free (buf);
+    }
 
-  pl_restorestate_r(ch->lp);    
+  cairo_restore (cr);
 }
 
-static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
-
-
 static void
-hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
+hist_draw_bar (cairo_t *cr, const struct chart_geometry *geom,
+               const gsl_histogram *h, int bar)
 {
-  if ( !ch ) 
-    return ;
-
+  double upper;
+  double lower;
+  double height;
+
+  const size_t bins = gsl_histogram_bins (h);
+  const double x_pos = (geom->data_right - geom->data_left) * bar / (double) bins ;
+  const double width = (geom->data_right - geom->data_left) / (double) bins ;
+
+  assert ( 0 == gsl_histogram_get_range (h, bar, &lower, &upper));
+
+  assert ( upper >= lower);
+
+  height = gsl_histogram_get (h, bar) *
+    (geom->data_top - geom->data_bottom) / gsl_histogram_max_val (h);
+
+  cairo_rectangle (cr, geom->data_left + x_pos, geom->data_bottom,
+                   width, height);
+  cairo_save (cr);
+  cairo_set_source_rgb (cr,
+                        geom->fill_colour.red / 255.0,
+                        geom->fill_colour.green / 255.0,
+                        geom->fill_colour.blue / 255.0);
+  cairo_fill_preserve (cr);
+  cairo_restore (cr);
+  cairo_stroke (cr);
+
+  draw_tick (cr, geom, TICK_ABSCISSA,
+             x_pos + width / 2.0, "%g", (upper + lower) / 2.0);
+}
 
+struct histogram_chart
   {
-    double upper;
-    double lower;
-    double height;
-
-    const size_t bins = gsl_histogram_bins(hist);
-    const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
-    const double width = (ch->data_right - ch->data_left) / (double) bins ;
-
-
-    assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
-
-    assert( upper >= lower);
-
-    height = gsl_histogram_get(hist, bar) * 
-      (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
-
-    pl_savestate_r(ch->lp);
-    pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
-    pl_fillcolorname_r(ch->lp, ch->fill_colour); 
-    pl_filltype_r(ch->lp,1);
+    struct chart chart;
+    gsl_histogram *gsl_hist;
+    char *label;
+    double n;
+    double mean;
+    double stddev;
+    bool show_normal;
+  };
+
+/* Plots a histogram of the data in HIST with the given LABEL.
+   Labels the histogram with each of N, MEAN, and STDDEV that is
+   not SYSMIS.  If all three are not SYSMIS and SHOW_NORMAL is
+   true, also draws a normal curve on the histogram. */
+struct chart *
+histogram_chart_create (const struct histogram *hist, const char *label,
+                        double n, double mean, double stddev,
+                        bool show_normal)
+{
+  struct histogram_chart *h;
+
+  h = xmalloc (sizeof *h);
+  chart_init (&h->chart, &histogram_chart_class);
+  h->gsl_hist = hist->gsl_hist ? gsl_histogram_clone (hist->gsl_hist) : NULL;
+  h->label = xstrdup (label);
+  h->n = n;
+  h->mean = mean;
+  h->stddev = stddev;
+  h->show_normal = show_normal;
+  return &h->chart;
+}
 
+static void
+histogram_chart_draw (const struct chart *chart, cairo_t *cr,
+                      struct chart_geometry *geom)
+{
+  struct histogram_chart *h = UP_CAST (chart, struct histogram_chart, chart);
+  int i;
+  int bins;
 
-    pl_fboxrel_r(ch->lp,
-                x_pos, 0,
-                x_pos + width, height);
+  chart_write_title (cr, geom, _("HISTOGRAM"));
 
-    pl_restorestate_r(ch->lp);
+  chart_write_ylabel (cr, geom, _("Frequency"));
+  chart_write_xlabel (cr, geom, h->label);
 
+  if (h->gsl_hist == NULL)
     {
-      char buf[5];
-      snprintf(buf,5,"%g",(upper + lower) / 2.0);
-      draw_tick(ch, TICK_ABSCISSA,
-               x_pos + width / 2.0, buf);
+      /* Probably all values are SYSMIS. */
+      return;
     }
-  }
-}
-
 
+  bins = gsl_histogram_bins (h->gsl_hist);
 
+  chart_write_yscale (cr, geom, 0, gsl_histogram_max_val (h->gsl_hist), 5);
 
-void
-histogram_plot(const gsl_histogram *hist,
-              const char *factorname,
-              const struct normal_curve *norm, short show_normal)
-{
-  int i;
-  int bins;
-  
-  struct chart *ch;
+  for (i = 0; i < bins; i++)
+    hist_draw_bar (cr, geom, h->gsl_hist, i);
 
-  ch = chart_create();
-  chart_write_title(ch, _("HISTOGRAM"));
+  histogram_write_legend (cr, geom, h->n, h->mean, h->stddev);
 
-  chart_write_ylabel(ch, _("Frequency"));
-  chart_write_xlabel(ch, factorname);
-
-  if ( ! hist ) /* If this happens, probably all values are SYSMIS */
-    {
-      chart_submit(ch);
-      return ;
-    }
-  else
+  if (h->show_normal
+      && h->n != SYSMIS && h->mean != SYSMIS && h->stddev != SYSMIS)
     {
-      bins = gsl_histogram_bins(hist);
+      /* Draw the normal curve */
+      double d;
+      double x_min, x_max, not_used;
+      double abscissa_scale;
+      double ordinate_scale;
+      double range;
+
+      gsl_histogram_get_range (h->gsl_hist, 0, &x_min, &not_used);
+      range = not_used - x_min;
+      gsl_histogram_get_range (h->gsl_hist, bins - 1, &not_used, &x_max);
+
+      abscissa_scale = (geom->data_right - geom->data_left) / (x_max - x_min);
+      ordinate_scale = (geom->data_top - geom->data_bottom) /
+       gsl_histogram_max_val (h->gsl_hist);
+
+      cairo_move_to (cr, geom->data_left, geom->data_bottom);
+      for (d = geom->data_left;
+          d <= geom->data_right;
+          d += (geom->data_right - geom->data_left) / 100.0)
+       {
+         const double x = (d - geom->data_left) / abscissa_scale + x_min;
+         const double y = h->n * range *
+           gsl_ran_gaussian_pdf (x - h->mean, h->stddev);
+
+          cairo_line_to (cr, d, geom->data_bottom  + y * ordinate_scale);
+
+       }
+      cairo_stroke (cr);
     }
+}
 
-  chart_write_yscale(ch, 0, gsl_histogram_max_val(hist), 5);
-
-  for ( i = 0 ; i < bins ; ++i ) 
-      hist_draw_bar(ch, hist, i);
-
-  histogram_write_legend(ch, norm);
 
-  if ( show_normal  )
-  {
-    /* Draw the normal curve */    
-
-    double d ;
-    double x_min, x_max, not_used ;
-    double abscissa_scale ;
-    double ordinate_scale ;
-    double range ;
-
-    gsl_histogram_get_range(hist, 0, &x_min, &not_used);
-    range = not_used - x_min;
-    gsl_histogram_get_range(hist, bins - 1, &not_used, &x_max);
-    assert(range == x_max - not_used);
-
-    abscissa_scale = (ch->data_right - ch->data_left) / (x_max - x_min);
-    ordinate_scale = (ch->data_top - ch->data_bottom) / 
-      gsl_histogram_max_val(hist) ;
-
-    pl_move_r(ch->lp, ch->data_left, ch->data_bottom);    
-    for( d = ch->data_left; 
-        d <= ch->data_right ; 
-        d += (ch->data_right - ch->data_left) / 100.0)
-      {    
-       const double x = (d - ch->data_left) / abscissa_scale + x_min ; 
-       const double y = norm->N * range * 
-         gsl_ran_gaussian_pdf(x - norm->mean, norm->stddev);
-
-       pl_fcont_r(ch->lp,  d,  ch->data_bottom  + y * ordinate_scale);
-
-      }
-    pl_endpath_r(ch->lp);
-
-  }
-  chart_submit(ch);
+static void
+histogram_chart_destroy (struct chart *chart)
+{
+  struct histogram_chart *h = UP_CAST (chart, struct histogram_chart, chart);
+  if (h->gsl_hist != NULL)
+    gsl_histogram_free (h->gsl_hist);
+  free (h->label);
+  free (h);
 }
 
+static const struct chart_class histogram_chart_class =
+  {
+    histogram_chart_draw,
+    histogram_chart_destroy
+  };