Skip to content

Instantly share code, notes, and snippets.

@petski
Created March 8, 2017 12:16
Show Gist options
  • Save petski/6240b64e527c821fc537e0d866cbeec1 to your computer and use it in GitHub Desktop.
Save petski/6240b64e527c821fc537e0d866cbeec1 to your computer and use it in GitHub Desktop.
Patch to support newline(s) in rrdtool title. Separation char is "?"
--- rrdtool-1.6.0-orig/src/rrd_graph.c 2017-03-07 10:20:47.154603542 +0100
+++ rrdtool-1.6.0/src/rrd_graph.c 2017-03-07 11:57:28.174767236 +0100
@@ -3084,6 +3084,8 @@
long i;
int res = 0;
double X0, Y0; /* points for filled graph and more */
+ int j = 0;
+ imgtitle_t myimgtitle_t;
struct gfx_color_t water_color;
if (im->draw_3d_border > 0) {
@@ -3156,13 +3158,18 @@
}
/* graph title */
+ myimgtitle_t = split_graphtitle(im->title?im->title:"");
+ while(myimgtitle_t.pieces[j] != NULL) {
gfx_text(im,
- im->xOriginTitle, im->yOriginTitle+6,
+ 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_CENTER, myimgtitle_t.pieces[j]);
+ j++;
+ }
+
/* rrdtool 'logo' */
if (!(im->extra_flags & NO_RRDTOOL_TAG)){
water_color = im->graph_col[GRC_FONT];
@@ -3413,6 +3420,8 @@
0, Xylabel = 0, Xmain = 0, Ymain =
0, Yxlabel = 0, Xspacing = 15, Yspacing = 15, Ywatermark = 4;
+ imgtitle_t myimgtitle_t;
+
// no legends and no the shall be plotted it's easy
if (im->extra_flags & ONLY_GRAPH) {
im->xorigin = 0;
@@ -3447,7 +3456,8 @@
** 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;
+ myimgtitle_t = split_graphtitle(im->title);
+ Ytitle = im->text_prop[TEXT_PROP_TITLE].size * (myimgtitle_t.count + 1) * 1.6;
}
else{
// we have no title; get a little clearing from the top
@@ -6092,3 +6102,27 @@
}
result[jj] = '\0'; /* We must force the end of the string */
}
+
+imgtitle_t split_graphtitle(
+ char *input)
+{
+ imgtitle_t retval;
+ char *str;
+ int count = 0;
+ const char delim[2] = "?";
+
+ str = strdup(input);
+
+ retval.pieces = malloc((MAX_IMGTITLE_NEWLINES + 1 ) * sizeof(char *));
+
+ retval.pieces[count] = strtok (str, delim);
+ while ( retval.pieces[count] != NULL && count++ < MAX_IMGTITLE_NEWLINES)
+ {
+ retval.pieces[count] = strtok( NULL, delim);
+ }
+ retval.pieces[count] = NULL;
+ retval.count = count;
+
+ return retval;
+}
+
--- rrdtool-1.6.0-orig/src/rrd_graph.h 2017-03-07 10:20:47.154603542 +0100
+++ rrdtool-1.6.0/src/rrd_graph.h 2017-03-07 11:08:41.394684648 +0100
@@ -48,6 +48,8 @@
#define gdes_fetch_key(x) sprintf_alloc("%s:%d:%d:%d:%d",x.rrd,x.cf,x.cf_reduce,x.start_orig,x.end_orig,x.step_orig)
+#define MAX_IMGTITLE_NEWLINES 5
+
enum tmt_en { TMT_SECOND = 0, TMT_MINUTE, TMT_HOUR, TMT_DAY,
TMT_WEEK, TMT_MONTH, TMT_YEAR
};
@@ -350,6 +352,12 @@
mutex_t *fontmap_mutex; /* Mutex for locking the global fontmap */
} image_desc_t;
+typedef struct imgtitle_t
+{
+ char **pieces;
+ int count;
+} imgtitle_t;
+
/* Prototypes */
int xtr(
image_desc_t *,
@@ -592,4 +600,4 @@
char *key,
rrd_info_type_t type, rrd_infoval_t value);
-
+imgtitle_t split_graphtitle(char *);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment