X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_graph.c;h=3ce39598c8b5d28e7d54ce3f87f4fc9e30d4916f;hp=2a13cb54e8ed62bf461b1914b3be57d31fcad833;hb=13b2b80a81d0fb22fc19ea95960b6ce71b8558b5;hpb=bba670307e10835e1dd3936c72ab987cb14b3071 diff --git a/src/rrd_graph.c b/src/rrd_graph.c index 2a13cb5..3ce3959 100644 --- a/src/rrd_graph.c +++ b/src/rrd_graph.c @@ -3406,6 +3406,9 @@ char *str; else if (!strcmp("TOTAL", func)) gdes->vf.op = VDEF_TOTAL; else if (!strcmp("FIRST", func)) gdes->vf.op = VDEF_FIRST; else if (!strcmp("LAST", func)) gdes->vf.op = VDEF_LAST; + else if (!strcmp("LSLSLOPE", func)) gdes->vf.op = VDEF_LSLSLOPE; + else if (!strcmp("LSLINT", func)) gdes->vf.op = VDEF_LSLINT; + else if (!strcmp("LSLCORREL",func)) gdes->vf.op = VDEF_LSLCORREL; else { rrd_set_error("Unknown function '%s' in VDEF '%s'\n" ,func @@ -3441,6 +3444,9 @@ char *str; case VDEF_TOTAL: case VDEF_FIRST: case VDEF_LAST: + case VDEF_LSLSLOPE: + case VDEF_LSLINT: + case VDEF_LSLCORREL: if (isnan(param)) { gdes->vf.param = DNAN; gdes->vf.val = DNAN; @@ -3599,6 +3605,48 @@ printf("DEBUG: %3li:%10.2f %c\n",step,array[step],step==field?'*':' '); dst->vf.when = src->start + (step+1)*src->step; } break; + case VDEF_LSLSLOPE: + case VDEF_LSLINT: + case VDEF_LSLCORREL:{ + /* Bestfit line by linear least squares method */ + + int cnt=0; + double SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl ; + SUMx = 0; SUMy = 0; SUMxy = 0; SUMxx = 0; SUMyy = 0; + + for (step=0;stepds_cnt])) { + cnt++; + SUMx += step; + SUMxx += step * step; + SUMxy += step * data[step*src->ds_cnt]; + SUMy += data[step*src->ds_cnt]; + SUMyy += data[step*src->ds_cnt]*data[step*src->ds_cnt]; + }; + } + + slope = ( SUMx*SUMy - cnt*SUMxy ) / ( SUMx*SUMx - cnt*SUMxx ); + y_intercept = ( SUMy - slope*SUMx ) / cnt; + correl = (SUMxy - (SUMx*SUMy)/cnt) / sqrt((SUMxx - (SUMx*SUMx)/cnt)*(SUMyy - (SUMy*SUMy)/cnt)); + + if (cnt) { + if (dst->vf.op == VDEF_LSLSLOPE) { + dst->vf.val = slope; + dst->vf.when = cnt*src->step; + } else if (dst->vf.op == VDEF_LSLINT) { + dst->vf.val = y_intercept; + dst->vf.when = cnt*src->step; + } else if (dst->vf.op == VDEF_LSLCORREL) { + dst->vf.val = correl; + dst->vf.when = cnt*src->step; + }; + + } else { + dst->vf.val = DNAN; + dst->vf.when = 0; + } + } + break; } return 0; }