[goffice] Regression line equation: improve for date axis (which has arbitrary 0).



commit b21cdb573e59fdc0c80bc471e221e55eacd7e1fd
Author: Morten Welinder <terra gnome org>
Date:   Mon Mar 22 19:16:25 2021 -0400

    Regression line equation: improve for date axis (which has arbitrary 0).

 ChangeLog                        |  8 ++++++++
 NEWS                             |  1 +
 plugins/reg_linear/gog-lin-reg.c | 22 ++++++++++++++++++++++
 plugins/reg_linear/gog-lin-reg.h |  2 ++
 4 files changed, 33 insertions(+)
---
diff --git a/ChangeLog b/ChangeLog
index 37e67306f..c8292a323 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2021-03-22  Morten Welinder  <terra gnome org>
+
+       * plugins/reg_linear/gog-lin-reg.c
+       (gog_lin_reg_curve_get_equation): If x-axis is a date axis, use an
+       equation that effectively says y=a * #days + b where #days is
+       counted from the left edge, not the arbitrary zero date in year
+       1900.
+
 2021-03-17  Morten Welinder  <terra gnome org>
 
        * goffice/gtk/go-format-sel.c: Get rid of empty categories.  (That
diff --git a/NEWS b/NEWS
index 639df41dc..8918c4f10 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ Morten:
        * Simplify regression equation code.
        * Use only 3 decimals for R^2 for regression lines.
        * Eliminate empty Special category from format selector.  [#29]
+       * Improve regression line equation for date axes.  [#41]
 
 --------------------------------------------------------------------------
 goffice 0.10.49:
diff --git a/plugins/reg_linear/gog-lin-reg.c b/plugins/reg_linear/gog-lin-reg.c
index 4c5d70df7..580e676ae 100644
--- a/plugins/reg_linear/gog-lin-reg.c
+++ b/plugins/reg_linear/gog-lin-reg.c
@@ -59,6 +59,18 @@ gog_lin_reg_curve_update (GogObject *obj)
        if (!gog_series_is_valid (series))
                return;
 
+       if (rc->affine) {
+               GogPlot *plot = gog_series_get_plot (series);
+               GogAxis *xaxis = plot ? gog_plot_get_axis (plot, GOG_AXIS_X) : NULL;
+               GOFormat *fmt = xaxis ? gog_axis_get_effective_format (xaxis) : NULL;
+               gboolean is_date = fmt && go_format_is_date (fmt) > 0;
+               double L, H;
+               gog_axis_get_bounds (xaxis, &L, &H);
+               rc->use_days_var = is_date;
+               rc->xbasis = L;
+       } else
+               rc->use_days_var = FALSE;
+
        nb = gog_series_get_xy_data (series, &x_vals, &y_vals);
        used = (y_vals)? (GOG_LIN_REG_CURVE_GET_CLASS(rc))->build_values (rc, x_vals, y_vals, nb): 0;
        if (used > 1) {
@@ -97,6 +109,14 @@ gog_lin_reg_curve_get_equation (GogRegCurve *curve)
                double b = curve->a[0];
                const char *var = "x";
                const char *times = "";
+
+               if (lin->use_days_var) {
+                       var = _("#days");
+                       // thin-space x thin-space
+                       times = "\xe2\x80\x89\xc3\x97\xe2\x80\x89";
+                       b += a * lin->xbasis;
+               }
+
                if (lin->affine)
                        curve->equation =
                                g_strdup_printf ("y = %s%g%s%s %s %g",
@@ -296,6 +316,8 @@ gog_lin_reg_curve_init (GogLinRegCurve *model)
        model->x_vals = NULL;
        model->y_vals = NULL;
        model->dims = 1;
+       model->use_days_var = FALSE;
+       model->xbasis = 0;
 }
 
 GSF_DYNAMIC_CLASS (GogLinRegCurve, gog_lin_reg_curve,
diff --git a/plugins/reg_linear/gog-lin-reg.h b/plugins/reg_linear/gog-lin-reg.h
index fa8b8ba28..6164b897b 100644
--- a/plugins/reg_linear/gog-lin-reg.h
+++ b/plugins/reg_linear/gog-lin-reg.h
@@ -32,6 +32,8 @@ typedef  struct {
        gboolean affine;
        double **x_vals, *y_vals;
        int dims;
+       gboolean use_days_var;
+       double xbasis;
 } GogLinRegCurve;
 
 typedef struct {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]