From: Mark Brugnoli-Vinten Date: Wed, 1 Aug 2018 14:50:49 +0000 (+0100) Subject: Allow multiple lines in graph titles (#897) X-Git-Tag: v1.7.1~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd4a7285600e233490c2faaf798858b8050358df;p=thirdparty%2Frrdtool-1.x.git Allow multiple lines in graph titles (#897) --- diff --git a/doc/rrdgraph.pod b/doc/rrdgraph.pod index f4cf4def..4086d249 100644 --- a/doc/rrdgraph.pod +++ b/doc/rrdgraph.pod @@ -85,10 +85,13 @@ one pixel will silently be ignored. =head2 Labels [B<-t>|B<--title> I] + +A horizontal string placed at the top of the graph which may be +separated into multiple lines using
or \n + [B<-v>|B<--vertical-label> I] -A horizontal string at the top of the graph and/or a vertically -placed string at the left hand side of the graph. +A vertical string placed at the left hand of the graph. =head2 Size diff --git a/src/rrd_graph.c b/src/rrd_graph.c index 8d07c604..bb049004 100644 --- a/src/rrd_graph.c +++ b/src/rrd_graph.c @@ -3099,9 +3099,11 @@ int grid_paint( { long i; int res = 0; - double X0, Y0; /* points for filled graph and more */ - struct gfx_color_t water_color; + int j = 0; int legend_cnt = 0; + double X0, Y0; /* points for filled graph and more */ + struct image_title_t image_title; + struct gfx_color_t water_color; if (im->draw_3d_border > 0) { /* draw 3d border */ @@ -3173,13 +3175,18 @@ int grid_paint( } /* graph title */ - gfx_text(im, - im->xOriginTitle, im->yOriginTitle+6, + image_title = graph_title_split(im->title?im->title:""); + while(image_title.lines[j] != NULL) { + gfx_text(im, + im->ximg / 2, (im->text_prop[TEXT_PROP_TITLE].size * 1.3) + (im->text_prop[TEXT_PROP_TITLE].size * 1.6 * j), im->graph_col[GRC_FONT], im-> text_prop[TEXT_PROP_TITLE]. font_desc, - im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP, im->title?im->title:""); + im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP, image_title.lines[j]?image_title.lines[j]:""); + j++; + } + /* rrdtool 'logo' */ if (!(im->extra_flags & NO_RRDTOOL_TAG)){ water_color = im->graph_col[GRC_FONT]; @@ -3490,7 +3497,8 @@ int graph_size_location( ** spacing is added here, on each side. */ /* if necessary, reduce the font size of the title until it fits the image width */ - Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10; + image_title_t image_title = graph_title_split(im->title); + Ytitle = im->text_prop[TEXT_PROP_TITLE].size * (image_title.count + 1) * 1.6; } else{ // we have no title; get a little clearing from the top @@ -6147,3 +6155,75 @@ void time_clean( } result[jj] = '\0'; /* We must force the end of the string */ } + +char *substring(const char *string, int position, int length) +{ + char *pointer; + + pointer = malloc(length+1); + + if (pointer == NULL) + { + printf("Unable to allocate memory.\n"); + exit(1); + } + + strncpy(pointer, (string+position), length); + *(pointer+length+1) = '\0'; + + return pointer; +} + +image_title_t graph_title_split( + const char *title) +{ + image_title_t retval; + int start = 0; + int length = 0; + int pos = 0; + int count = 0; + int delim = 1; + + retval.lines = malloc((MAX_IMAGE_TITLE_LINES + 1 ) * sizeof(char *)); + length = strlen(title); + + char *delims[6] = { "\n", "\\n", "
", "
" }; + printf("title: %s\n", title); + do + { + pos = 0; + delim = 0; + + while (delim == 0 && start + pos < length) { + for(int i=0; i<6;i++) { + int delim_size = strlen(delims[i]); + int delim_match = strncasecmp(title+start+pos, delims[i], delim_size); + + //printf("Comparing '%s' with '%s' (%d=%d from %d) = %d\n", + // title+start+pos, delims[i], i, delim_size, start+pos, delim_match); + if (delim_match == 0) { + delim = delim_size; + break; + } + } + + pos++; + } + + if (pos != 0) + { + char *str = substring(title, start, pos - 1); + //printf("str: %s (%d - %d)\n", str, start, pos); + retval.lines[count] = str; + } + + start = start + pos + delim - 1; + count++; + } + while (pos != 0 && retval.lines[count] != NULL && count < MAX_IMAGE_TITLE_LINES); + + retval.lines[count] = NULL; + retval.count = count; + + return retval; +} diff --git a/src/rrd_graph.h b/src/rrd_graph.h index 51e627cc..a2f4faa4 100644 --- a/src/rrd_graph.h +++ b/src/rrd_graph.h @@ -203,6 +203,7 @@ enum value_formatter_en { #endif # define MAX_AXIS 4 +# define MAX_IMAGE_TITLE_LINES 3 typedef struct graph_desc_t { enum gf_en gf; /* graphing function */ @@ -357,6 +358,12 @@ typedef struct image_desc_t { double last_tabwidth; /* (see gfx_prep_text() for reference) */ } image_desc_t; +typedef struct image_title_t +{ + char **lines; + int count; +} image_title_t; + /* Prototypes */ int xtr( image_desc_t *, @@ -438,6 +445,8 @@ int graph_paint_timestring( image_desc_t *,int,int); int graph_paint_xy( image_desc_t *,int,int); +image_title_t graph_title_split( + const char *); int rrd_graph_xport( image_desc_t *); @@ -608,4 +617,3 @@ void grinfo_push( char *key, rrd_info_type_t type, rrd_infoval_t value); -