+#include <math.h>
+
+#include "data/settings.h"
+#include "libpspp/message.h"
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
+#include "math/chart-geometry.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
+
+
+#include "gl/xalloc.h"
+
+void
+histogram_add (struct histogram *h, double y, double c)
+{
+ struct statistic *stat = &h->parent;
+ stat->accumulate (stat, NULL, c, 0, y);
+}
+
+static void
+acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
+{
+ struct histogram *hist = UP_CAST (s, struct histogram, parent);
+
+ gsl_histogram_accumulate (hist->gsl_hist, y, c);
+}
+
+static void
+destroy (struct statistic *s)
+{
+ struct histogram *h = UP_CAST (s, struct histogram, parent);
+ gsl_histogram_free (h->gsl_hist);
+ free (s);
+}
+
+
+/* Find a bin width which is adapted to the scaling of the x axis
+In the example here, the binwidth is half of the tick interval.
+
+ binwidth
+ > <
+ |....+....+....+. .+....|
+ LOWER 1 2 3 N_TICKS
+ ^LOWDBL ^HIGHDBL
+
+This only works, when the min and max value for the histogram are adapted
+such that (max-min) is a multiple of the binwidth. Then the location of the
+first bin has to be aligned to the ticks.
+*/
+static int
+hist_find_pretty_no_of_bins(double bin_width_in, double min, double max,
+ double *adjusted_min, double *adjusted_max)
+{
+ double lower, interval;
+ int n_ticks;
+ double binwidth;
+ int nbins;
+
+ chart_get_scale (max, min, &lower, &interval, &n_ticks);