There are two popular variants of the Holt-Winters forecasting method; RRDtool
[rrdtool.git] / src / rrd_graph.c
index 542c3b8..3022227 100644 (file)
@@ -717,6 +717,7 @@ void reduce_data(
                 else {
                     switch (cf) {
                     case CF_HWPREDICT:
+                    case CF_MHWPREDICT:
                     case CF_DEVSEASONAL:
                     case CF_DEVPREDICT:
                     case CF_SEASONAL:
@@ -742,6 +743,7 @@ void reduce_data(
             } else {
                 switch (cf) {
                 case CF_HWPREDICT:
+                case CF_MHWPREDICT:
                 case CF_DEVSEASONAL:
                 case CF_DEVPREDICT:
                 case CF_SEASONAL:
@@ -1438,6 +1440,7 @@ int print_calc(
 
                     switch (im->gdes[i].cf) {
                     case CF_HWPREDICT:
+                    case CF_MHWPREDICT:
                     case CF_DEVPREDICT:
                     case CF_DEVSEASONAL:
                     case CF_SEASONAL:
@@ -1580,7 +1583,7 @@ int leg_place(
     int       glue = 0;
     int       i, ii, mark = 0;
     char      prt_fctn; /*special printfunctions */
-    char      default_txtalign = TXA_JUSTIFIED; /*default line orientation*/
+    char      default_txtalign = TXA_JUSTIFIED; /*default line orientation */
     int      *legspace;
 
     if (!(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH)) {
@@ -1598,7 +1601,7 @@ int leg_place(
 
             /* hide legends for rules which are not displayed */
 
-            if (im->gdes[i].gf == GF_TEXTALIGN){
+            if (im->gdes[i].gf == GF_TEXTALIGN) {
                 default_txtalign = im->gdes[i].txtalign;
             }
 
@@ -1644,7 +1647,7 @@ int leg_place(
                 prt_fctn = 'l';
             }
 
-            /* remove exess space from the end of the legend for \g*/            
+            /* remove exess space from the end of the legend for \g */
             while (prt_fctn == 'g' &&
                    leg_cc > 0 && im->gdes[i].legend[leg_cc - 1] == ' ') {
                 leg_cc--;
@@ -1677,19 +1680,19 @@ int leg_place(
             if (prt_fctn == '\0') {
                 if (i == im->gdes_c - 1 || fill > im->ximg - 2 * border) {
                     /* just one legend item is left right or center */
-                    switch (default_txtalign){
+                    switch (default_txtalign) {
                     case TXA_RIGHT:
-                         prt_fctn = 'r';
-                         break;
+                        prt_fctn = 'r';
+                        break;
                     case TXA_CENTER:
-                         prt_fctn = 'c';
-                         break;
+                        prt_fctn = 'c';
+                        break;
                     case TXA_JUSTIFIED:
-                         prt_fctn = 'j';
-                         break;
+                        prt_fctn = 'j';
+                        break;
                     default:
-                         prt_fctn = 'l';
-                         break;
+                        prt_fctn = 'l';
+                        break;
                     }
                 }
                 /* is it time to place the legends ? */
@@ -1701,7 +1704,7 @@ int leg_place(
                         leg_c--;
                     }
                 }
-                if (leg_c == 1 && prt_fctn == 'j'){
+                if (leg_c == 1 && prt_fctn == 'j') {
                     prt_fctn = 'l';
                 }
             }
@@ -2873,16 +2876,35 @@ int graph_size_location(
 
 
 
-static cairo_status_t cairo_write_func_file(
-       void *closure,
-       const unsigned char *data,
-       unsigned int length)
+static cairo_status_t cairo_write_func_filehandle(
+    void *closure,
+    const unsigned char *data,
+    unsigned int length)
 {
-       if (fwrite(data, length, 1, closure) != 1)
-               return CAIRO_STATUS_WRITE_ERROR;
-       return CAIRO_STATUS_SUCCESS;
+    if (fwrite(data, length, 1, closure) != 1)
+        return CAIRO_STATUS_WRITE_ERROR;
+    return CAIRO_STATUS_SUCCESS;
 }
 
+static cairo_status_t cairo_copy_to_buffer(
+    void *closure,
+    const unsigned char *data,
+    unsigned int length)
+{
+    image_desc_t *im = closure;
+
+    im->rendered_image =
+        realloc(im->rendered_image, im->rendered_image_size + length);
+    if (im->rendered_image == NULL) {
+        return CAIRO_STATUS_WRITE_ERROR;
+    }
+
+    memcpy(im->rendered_image + im->rendered_image_size, data, length);
+
+    im->rendered_image_size += length;
+
+    return CAIRO_STATUS_SUCCESS;
+}
 
 /* draw that picture thing ... */
 int graph_paint(
@@ -2965,21 +2987,30 @@ int graph_paint(
         break;
     case IF_PDF:
         im->gridfit = 0;
-        im->surface =
-            cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
-                                     im->yimg * im->zoom);
+        im->surface = strlen(im->graphfile)
+            ? cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
+                                       im->yimg * im->zoom)
+            : cairo_pdf_surface_create_for_stream(&cairo_copy_to_buffer, im,
+                                                  im->ximg * im->zoom,
+                                                  im->yimg * im->zoom);
         break;
     case IF_EPS:
         im->gridfit = 0;
-        im->surface =
-            cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
-                                    im->yimg * im->zoom);
+        im->surface = strlen(im->graphfile)
+            ? cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
+                                      im->yimg * im->zoom)
+            : cairo_ps_surface_create_for_stream(&cairo_copy_to_buffer, im,
+                                                 im->ximg * im->zoom,
+                                                 im->yimg * im->zoom);
         break;
     case IF_SVG:
         im->gridfit = 0;
-        im->surface =
-            cairo_svg_surface_create(im->graphfile, im->ximg * im->zoom,
-                                     im->yimg * im->zoom);
+        im->surface = strlen(im->graphfile)
+            ? cairo_svg_surface_create(im->graphfile, im->ximg * im->zoom,
+                                       im->yimg * im->zoom)
+            : cairo_svg_surface_create_for_stream(&cairo_copy_to_buffer, im,
+                                                  im->ximg * im->zoom,
+                                                  im->yimg * im->zoom);
         cairo_svg_surface_restrict_to_version(im->surface,
                                               CAIRO_SVG_VERSION_1_1);
         break;
@@ -3312,23 +3343,34 @@ int graph_paint(
 
     switch (im->imgformat) {
     case IF_PNG:
-               {
-                       cairo_status_t status;
-               
-                       if (strcmp(im->graphfile, "-") == 0) {
-               status = cairo_surface_write_to_png_stream(im->surface, &cairo_write_func_file, (void*)stdout);
-                       } else {
-               status = cairo_surface_write_to_png(im->surface, im->graphfile);
-                       }
-               
-               if (status != CAIRO_STATUS_SUCCESS) {
-               rrd_set_error("Could not save png to '%s'", im->graphfile);
-                   return 1;
-               }
-               }
+    {
+        cairo_status_t status;
+
+        if (strlen(im->graphfile) == 0) {
+            status =
+                cairo_surface_write_to_png_stream(im->surface,
+                                                  &cairo_copy_to_buffer, im);
+        } else if (strcmp(im->graphfile, "-") == 0) {
+            status =
+                cairo_surface_write_to_png_stream(im->surface,
+                                                  &cairo_write_func_filehandle,
+                                                  (void *) stdout);
+        } else {
+            status = cairo_surface_write_to_png(im->surface, im->graphfile);
+        }
+
+        if (status != CAIRO_STATUS_SUCCESS) {
+            rrd_set_error("Could not save png to '%s'", im->graphfile);
+            return 1;
+        }
+    }
         break;
     default:
-        cairo_show_page(im->cr);
+        if (strlen(im->graphfile)) {
+            cairo_show_page(im->cr);
+        } else {
+            cairo_surface_finish(im->surface);
+        }
         break;
     }
     return 0;
@@ -3430,9 +3472,6 @@ int rrd_graph(
     /* a dummy surface so that we can measure text sizes for placements */
     im.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
     im.cr = cairo_create(im.surface);
-
-
-    /* not currently using this ... */
     im.graphhandle = stream;
 
     rrd_graph_options(argc, argv, &im);
@@ -3441,11 +3480,17 @@ int rrd_graph(
         return -1;
     }
 
+    if (optind >= argc) {
+        rrd_set_error("missing filename");
+        return -1;
+    }
+
     if (strlen(argv[optind]) >= MAXPATH) {
         rrd_set_error("filename (including path) too long");
         im_free(&im);
         return -1;
     }
+
     strncpy(im.graphfile, argv[optind], MAXPATH - 1);
     im.graphfile[MAXPATH - 1] = '\0';
 
@@ -3502,6 +3547,60 @@ int rrd_graph(
     return 0;
 }
 
+/* a simplified version of the above that just creates the graph in memory 
+   and returns a pointer to it. */
+
+unsigned char *rrd_graph_in_memory(
+    int argc,
+    char **argv,
+    char ***prdata,
+    int *xsize,
+    int *ysize,
+    double *ymin,
+    double *ymax,
+    size_t * img_size)
+{
+    image_desc_t im;
+
+    rrd_graph_init(&im);
+
+    /* a dummy surface so that we can measure text sizes for placements */
+    im.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
+    im.cr = cairo_create(im.surface);
+
+    rrd_graph_options(argc, argv, &im);
+    if (rrd_test_error()) {
+        im_free(&im);
+        return NULL;
+    }
+
+    rrd_graph_script(argc, argv, &im, 1);
+    if (rrd_test_error()) {
+        im_free(&im);
+        return NULL;
+    }
+
+    /* Everything is now read and the actual work can start */
+
+    /* by not assigning a name to im.graphfile data will be written to
+       newly allocated memory on im.rendered_image ... */
+
+    (*prdata) = NULL;
+    if (graph_paint(&im, prdata) == -1) {
+        im_free(&im);
+        return NULL;
+    }
+
+    *xsize = im.ximg;
+    *ysize = im.yimg;
+    *ymin = im.minval;
+    *ymax = im.maxval;
+    *img_size = im.rendered_image_size;
+    im_free(&im);
+
+    return im.rendered_image;
+}
+
 void rrd_graph_init(
     image_desc_t *im)
 {
@@ -3524,6 +3623,8 @@ void rrd_graph_init(
     im->yimg = 0;
     im->xsize = 400;
     im->ysize = 100;
+    im->rendered_image_size = 0;
+    im->rendered_image = NULL;
     im->step = 0;
     im->ylegend[0] = '\0';
     im->title[0] = '\0';
@@ -3536,6 +3637,7 @@ void rrd_graph_init(
     im->symbol = ' ';
     im->viewfactor = 1.0;
     im->imgformat = IF_PNG;
+    im->graphfile[0] = '\0';
     im->cr = NULL;
     im->surface = NULL;
     im->extra_flags = 0;
@@ -4001,11 +4103,6 @@ void rrd_graph_options(
         }
     }
 
-    if (optind >= argc) {
-        rrd_set_error("missing filename");
-        return;
-    }
-
     if (im->logarithmic == 1 && im->minval <= 0) {
         rrd_set_error
             ("for a logarithmic yaxis you must specify a lower-limit > 0");