X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fcharts%2Fnp-plot.c;h=eeffbc1fa1d5f0cf3beae266974f1f95dd89bd25;hb=75a514035ccbe2df4058b486a48162b301dd51d0;hp=dba20bac56a7dfdd40c377ae389e7d5a0fc1db7c;hpb=e0c37920bb2cc46ee559e3992470572d4b4d27e6;p=pspp diff --git a/src/output/charts/np-plot.c b/src/output/charts/np-plot.c index dba20bac56..eeffbc1fa1 100644 --- a/src/output/charts/np-plot.c +++ b/src/output/charts/np-plot.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2004, 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 2004, 2008, 2009, 2011 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,62 +16,32 @@ #include -#include +#include "output/charts/np-plot.h" #include -#include -#include -#include -#include -#include -#include -#include +#include "data/casereader.h" +#include "libpspp/cast.h" +#include "math/np.h" +#include "output/chart-provider.h" #include "gl/minmax.h" -#include "gettext.h" -#define _(msgid) gettext (msgid) - -/* An NP or DNP plot. */ -struct np_plot_chart - { - struct chart chart; - char *label; - struct casereader *data; - - /* Copied directly from struct np. */ - double y_min, y_max; - double dns_min, dns_max; - - /* Calculated. */ - double slope, intercept; - double y_first, y_last; - double x_lower, x_upper; - double slack; - }; - -static const struct chart_class np_plot_chart_class; -static const struct chart_class dnp_plot_chart_class; - static struct chart * -make_np_plot (const struct chart_class *class, - const struct np *np, const struct casereader *reader, - const char *label) +make_np_plot (const struct np *np, const struct casereader *reader, + const char *label, bool detrended) { - struct np_plot_chart *npp; - - if (np->n < 1.0) + if (np->n <= 1.0) return NULL; - npp = xmalloc (sizeof *npp); - chart_init (&npp->chart, class); - npp->label = xstrdup (label); + struct np_plot_chart *npp = XZALLOC (struct np_plot_chart); + chart_init (&npp->chart, &np_plot_chart_class, label); npp->data = casereader_clone (reader); npp->y_min = np->y_min; npp->y_max = np->y_max; npp->dns_min = np->dns_min; npp->dns_max = np->dns_max; + npp->detrended = detrended; /* Slope and intercept of the ideal normal probability line. */ npp->slope = 1.0 / np->stddev; @@ -101,7 +71,7 @@ struct chart * np_plot_create (const struct np *np, const struct casereader *reader, const char *label) { - return make_np_plot (&np_plot_chart_class, np, reader, label); + return make_np_plot (np, reader, label, false); } /* Creates and returns a detrended normal probability plot @@ -117,78 +87,18 @@ struct chart * dnp_plot_create (const struct np *np, const struct casereader *reader, const char *label) { - return make_np_plot (&dnp_plot_chart_class, np, reader, label); -} - -static void -np_plot_chart_draw (const struct chart *chart, plPlotter *lp, - struct chart_geometry *geom) -{ - const struct np_plot_chart *npp = (struct np_plot_chart *) chart; - struct casereader *data; - struct ccase *c; - - chart_write_title (lp, geom, _("Normal Q-Q Plot of %s"), npp->label); - chart_write_xlabel (lp, geom, _("Observed Value")); - chart_write_ylabel (lp, geom, _("Expected Normal")); - chart_write_xscale (lp, geom, - npp->x_lower - npp->slack, - npp->x_upper + npp->slack, 5); - chart_write_yscale (lp, geom, npp->y_first, npp->y_last, 5); - - data = casereader_clone (npp->data); - for (; (c = casereader_read (data)) != NULL; case_unref (c)) - chart_datum (lp, geom, 0, - case_data_idx (c, NP_IDX_Y)->f, - case_data_idx (c, NP_IDX_NS)->f); - casereader_destroy (data); - - chart_line (lp, geom, npp->slope, npp->intercept, - npp->y_first, npp->y_last, CHART_DIM_Y); -} - -static void -dnp_plot_chart_draw (const struct chart *chart, plPlotter *lp, - struct chart_geometry *geom) -{ - const struct np_plot_chart *dnpp = (struct np_plot_chart *) chart; - struct casereader *data; - struct ccase *c; - - chart_write_title (lp, geom, _("Detrended Normal Q-Q Plot of %s"), - dnpp->label); - chart_write_xlabel (lp, geom, _("Observed Value")); - chart_write_ylabel (lp, geom, _("Dev from Normal")); - chart_write_xscale (lp, geom, dnpp->y_min, dnpp->y_max, 5); - chart_write_yscale (lp, geom, dnpp->dns_min, dnpp->dns_max, 5); - - data = casereader_clone (dnpp->data); - for (; (c = casereader_read (data)) != NULL; case_unref (c)) - chart_datum (lp, geom, 0, case_data_idx (c, NP_IDX_Y)->f, - case_data_idx (c, NP_IDX_DNS)->f); - casereader_destroy (data); - - chart_line (lp, geom, 0, 0, dnpp->y_min, dnpp->y_max, CHART_DIM_X); + return make_np_plot (np, reader, label, true); } static void np_plot_chart_destroy (struct chart *chart) { - struct np_plot_chart *npp = (struct np_plot_chart *) chart; - + struct np_plot_chart *npp = to_np_plot_chart (chart); casereader_destroy (npp->data); - free (npp->label); free (npp); } -static const struct chart_class np_plot_chart_class = - { - np_plot_chart_draw, - np_plot_chart_destroy - }; - -static const struct chart_class dnp_plot_chart_class = +const struct chart_class np_plot_chart_class = { - dnp_plot_chart_draw, np_plot_chart_destroy };