use _NL_TIME_WEEK_1STDAY to determin the first day of the week.
[rrdtool.git] / src / rrd_graph.c
index d1f32dd..390a66e 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.4.2  Copyright by Tobi Oetiker, 1997-2009
  ****************************************************************************
  * rrd__graph.c  produce graphs from data in rrdfiles
  ****************************************************************************/
 #include <fcntl.h>
 #endif
 
-#ifdef HAVE_TIME_H
 #include <time.h>
-#endif
 
-#ifdef HAVE_LOCALE_H
 #include <locale.h>
+
+#ifdef HAVE_LANGINFO_H
+#include <langinfo.h>
 #endif
 
 #include "rrd_graph.h"
@@ -1252,7 +1252,7 @@ int data_proc(
                     } else
                         if (((long int) gr_time >=
                              (long int) im->gdes[vidx].start)
-                            && ((long int) gr_time <=
+                            && ((long int) gr_time <
                                 (long int) im->gdes[vidx].end)) {
                         value = im->gdes[vidx].data[(unsigned long)
                                                     floor((double)
@@ -1360,6 +1360,21 @@ int data_proc(
 }
 
 
+static int find_first_weekday(void){
+    static int first_weekday = -1;
+    if (first_weekday == -1){
+#if defined(HAVE_NL_LANGINFO)
+        /* according to http://sourceware.org/ml/libc-locales/2009-q1/msg00011.html */
+        long week_1stday_l = (long) nl_langinfo (_NL_TIME_WEEK_1STDAY);
+        if (week_1stday_l == 19971130) first_weekday = 0; /* Sun */
+        else if (week_1stday_l == 19971201) first_weekday = 1; /* Mon */
+        else first_weekday = 1; /* we go for a monday default */
+#else
+        first_weekday = 1;
+#endif
+    }
+    return first_weekday;
+}
 
 /* identify the point where the first gridline, label ... gets placed */
 
@@ -1401,10 +1416,10 @@ time_t find_first_time(
         tm.       tm_sec = 0;
         tm.       tm_min = 0;
         tm.       tm_hour = 0;
-        tm.       tm_mday -= tm.tm_wday - 1;    /* -1 because we want the monday */
+        tm.       tm_mday -= tm.tm_wday - find_first_weekday();
 
-        if (tm.tm_wday == 0)
-            tm.       tm_mday -= 7; /* we want the *previous* monday */
+        if (tm.tm_wday == 0 && find_first_weekday() > 0)
+            tm.       tm_mday -= 7; /* we want the *previous* week */
 
         break;
     case TMT_MONTH:
@@ -1734,6 +1749,7 @@ int leg_place(
                 prt_fctn != 'r' &&
                 prt_fctn != 'j' &&
                 prt_fctn != 'c' &&
+                prt_fctn != 'u' &&
                 prt_fctn != 's' && prt_fctn != '\0' && prt_fctn != 'g') {
                 free(legspace);
                 rrd_set_error
@@ -1822,7 +1838,7 @@ int leg_place(
                 if (prt_fctn == 'c')
                     leg_x = (double)(legendwidth - fill) / 2.0;
                 if (prt_fctn == 'r')
-                    leg_x = legendwidth - fill - border;
+                    leg_x = legendwidth - fill + border;
                 for (ii = mark; ii <= i; ii++) {
                     if (im->gdes[ii].legend[0] == '\0')
                         continue;   /* skip empty legends */
@@ -1843,6 +1859,8 @@ int leg_place(
                     leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
                 if (prt_fctn == 's')
                     leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
+                if (prt_fctn == 'u')
+                    leg_y -= im->text_prop[TEXT_PROP_LEGEND].size *1.8;
 
                 if(calc_width && (fill > legendwidth)){
                     legendwidth = fill;
@@ -2737,7 +2755,8 @@ void grid_paint(
                 boxV = boxH;
                 /* shift the box up a bit */
                 Y0 -= boxV * 0.4;
-               if (im->gdes[i].gf == GF_HRULE) { /* [-] */ 
+
+        if (im->dynamic_labels && im->gdes[i].gf == GF_HRULE) { /* [-] */ 
                        cairo_save(im->cr);
                        cairo_new_path(im->cr);
                        cairo_set_line_width(im->cr, 1.0);
@@ -2746,7 +2765,7 @@ void grid_paint(
                                X0 + boxH, Y0 - boxV / 2,
                                1.0, im->gdes[i].col);
                        gfx_close_path(im);
-               } else if (im->gdes[i].gf == GF_VRULE) { /* [|] */
+               } else if (im->dynamic_labels && im->gdes[i].gf == GF_VRULE) { /* [|] */
                        cairo_save(im->cr);
                        cairo_new_path(im->cr);
                        cairo_set_line_width(im->cr, 1.0);
@@ -2755,7 +2774,7 @@ void grid_paint(
                                X0 + boxH / 2, Y0 - boxV,
                                1.0, im->gdes[i].col);
                        gfx_close_path(im);
-               } else if (im->gdes[i].gf == GF_LINE) { /* [/] */
+               } else if (im->dynamic_labels && im->gdes[i].gf == GF_LINE) { /* [/] */
                        cairo_save(im->cr);
                        cairo_new_path(im->cr);
                        cairo_set_line_width(im->cr, im->gdes[i].linewidth);
@@ -3897,9 +3916,10 @@ rrd_info_t *rrd_graph_v(
 {
     image_desc_t im;
     rrd_info_t *grinfo;
+    char *old_locale;
     rrd_graph_init(&im);
     /* a dummy surface so that we can measure text sizes for placements */
-
+    old_locale = setlocale(LC_NUMERIC, "C");
     rrd_graph_options(argc, argv, &im);
     if (rrd_test_error()) {
         rrd_info_free(im.grinfo);
@@ -3929,6 +3949,8 @@ rrd_info_t *rrd_graph_v(
     }
 
     rrd_graph_script(argc, argv, &im, 1);
+    setlocale(LC_NUMERIC, old_locale); /* reenable locale for rendering the graph */
+
     if (rrd_test_error()) {
         rrd_info_free(im.grinfo);
         im_free(&im);
@@ -4010,6 +4032,7 @@ void rrd_graph_init(
     im->draw_x_grid = 1;
     im->draw_y_grid = 1;
     im->draw_3d_border = 2;
+    im->dynamic_labels = 0;
     im->extra_flags = 0;
     im->font_options = cairo_font_options_create();
     im->forceleftspace = 0;
@@ -4120,7 +4143,6 @@ void rrd_graph_options(
     long      long_tmp;
     rrd_time_value_t start_tv, end_tv;
     long unsigned int color;
-    char     *old_locale = "";
 
     /* defines for long options without a short equivalent. should be bytes,
        and may not collide with (the ASCII value of) short options */
@@ -4178,6 +4200,8 @@ void rrd_graph_options(
         { "legend-position",    required_argument, 0, 1005},
         { "legend-direction",   required_argument, 0, 1006},
         { "border",             required_argument, 0, 1007},
+        { "grid-dash",          required_argument, 0, 1008},
+        { "dynamic-labels",     no_argument,       0, 1009},
         {  0, 0, 0, 0}
 };
 /* *INDENT-ON* */
@@ -4251,7 +4275,6 @@ void rrd_graph_options(
         case LONGOPT_UNITS_SI:
             if (im->extra_flags & FORCE_UNITS) {
                 rrd_set_error("--units can only be used once!");
-                setlocale(LC_NUMERIC, old_locale);
                 return;
             }
             if (strcmp(optarg, "si") == 0)
@@ -4269,14 +4292,10 @@ void rrd_graph_options(
             im->forceleftspace = 1;
             break;
         case 'T':
-            old_locale = setlocale(LC_NUMERIC, "C");
             im->tabwidth = atof(optarg);
-            setlocale(LC_NUMERIC, old_locale);
             break;
         case 'S':
-            old_locale = setlocale(LC_NUMERIC, "C");
             im->step = atoi(optarg);
-            setlocale(LC_NUMERIC, old_locale);
             break;
         case 'N':
             im->gridfit = 0;
@@ -4340,9 +4359,7 @@ void rrd_graph_options(
                 im->draw_y_grid = 0;
                 break;
             };
-            old_locale = setlocale(LC_NUMERIC, "C");
             if (sscanf(optarg, "%lf:%d", &im->ygridstep, &im->ylabfact) == 2) {
-                setlocale(LC_NUMERIC, old_locale);
                 if (im->ygridstep <= 0) {
                     rrd_set_error("grid step must be > 0");
                     return;
@@ -4351,7 +4368,6 @@ void rrd_graph_options(
                     return;
                 }
             } else {
-                setlocale(LC_NUMERIC, old_locale);
                 rrd_set_error("invalid y-grid format");
                 return;
             }
@@ -4359,6 +4375,18 @@ void rrd_graph_options(
         case 1007:
             im->draw_3d_border = atoi(optarg);
             break;
+        case 1008: /* grid-dash */
+            if(sscanf(optarg,
+                      "%lf:%lf",
+                      &im->grid_dash_on,
+                      &im->grid_dash_off) != 2) {
+                rrd_set_error("expected grid-dash format float:float");
+                return;
+            }
+            break;   
+        case 1009: /* enable dynamic labels */
+            im->dynamic_labels = 1;
+            break;         
         case 1002: /* right y axis */
 
             if(sscanf(optarg,
@@ -4391,14 +4419,10 @@ void rrd_graph_options(
             im->ylegend[150] = '\0';
             break;
         case 'u':
-            old_locale = setlocale(LC_NUMERIC, "C");
             im->maxval = atof(optarg);
-            setlocale(LC_NUMERIC, old_locale);
             break;
         case 'l':
-            old_locale = setlocale(LC_NUMERIC, "C");
             im->minval = atof(optarg);
-            setlocale(LC_NUMERIC, old_locale);
             break;
         case 'b':
             im->base = atol(optarg);
@@ -4503,11 +4527,9 @@ void rrd_graph_options(
             double    size = 1;
             int       end;
 
-            old_locale = setlocale(LC_NUMERIC, "C");
             if (sscanf(optarg, "%10[A-Z]:%lf%n", prop, &size, &end) >= 2) {
                 int       sindex, propidx;
 
-                setlocale(LC_NUMERIC, old_locale);
                 if ((sindex = text_prop_conv(prop)) != -1) {
                     for (propidx = sindex;
                          propidx < TEXT_PROP_LAST; propidx++) {
@@ -4534,16 +4556,13 @@ void rrd_graph_options(
                     return;
                 }
             } else {
-                setlocale(LC_NUMERIC, old_locale);
                 rrd_set_error("invalid text property format");
                 return;
             }
             break;
         }
         case 'm':
-            old_locale = setlocale(LC_NUMERIC, "C");
             im->zoom = atof(optarg);
-            setlocale(LC_NUMERIC, old_locale);
             if (im->zoom <= 0.0) {
                 rrd_set_error("zoom factor must be > 0");
                 return;
@@ -4766,12 +4785,9 @@ int vdef_parse(
     double    param;
     char      func[30];
     int       n;
-    char     *old_locale;
 
     n = 0;
-    old_locale = setlocale(LC_NUMERIC, "C");
     sscanf(str, "%le,%29[A-Z]%n", &param, func, &n);
-    setlocale(LC_NUMERIC, old_locale);
     if (n == (int) strlen(str)) {   /* matched */
         ;
     } else {