]> granicus.if.org Git - sysstat/commitdiff
sadf: SVG: Add new "customcol" option
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 11 Jan 2019 15:31:30 +0000 (16:31 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 11 Jan 2019 15:31:30 +0000 (16:31 +0100)
Add a new option to sadf SVG output.
Entering something like "sadf -g -O customcol (...)" enables the user to
select a second distinct color palette that will be used to draw the
graphs.

This second palette is intended to be fully customizable by the user.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
sa.h
sadf.c
sadf_misc.c
svg_stats.c

diff --git a/sa.h b/sa.h
index 78107d24307317eae2fba6d83dfc85cdc139f12a..fd978cc787fa393b18a89f56322ae20bf6f86a8f 100644 (file)
--- a/sa.h
+++ b/sa.h
 #define K_HEIGHT       "height="
 #define K_PACKED       "packed"
 #define K_SHOWTOC      "showtoc"
+#define K_CUSTOMCOL    "customcol"
 
 /* Groups of activities */
 #define G_DEFAULT      0x00
@@ -1100,6 +1101,22 @@ struct report_format {
 /* Maximum number of horizontal lines for the background grid */
 #define MAX_HLINES_NR  10
 
+/* Color palette constants */
+#define SVG_COLORS_IDX_MASK    0x0f
+#define SVG_COL_PALETTE_SIZE   24
+#define SVG_COL_PALETTE_NR     2
+#define SVG_COL_BCKGRD_IDX     16
+#define SVG_COL_AXIS_IDX       17
+#define SVG_COL_GRID_IDX       18
+#define SVG_COL_TITLE_IDX      19
+#define SVG_COL_INFO_IDX       20
+#define SVG_COL_DEFAULT_IDX    21
+#define SVG_COL_HEADER_IDX     22
+#define SVG_COL_ERROR_IDX      23
+
+#define SVG_DEFAULT_COL_PALETTE        0
+#define SVG_CUSTOM_COL_PALETTE 1
+
 #define MAYBE  0x80
 
 /*
diff --git a/sadf.c b/sadf.c
index 114cb6aeb1e031053d9cd3e22c8677cba1ff11ee..dc0f46796eaf132aab832b94de7abcc9a04420e6 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -51,6 +51,8 @@ int endian_mismatch = FALSE;
 int arch_64 = FALSE;
 /* Number of decimal places */
 int dplaces_nr = -1;
+/* Color palette number */
+int palette = SVG_DEFAULT_COL_PALETTE;
 
 unsigned int flags = 0;
 unsigned int dm_major;         /* Device-mapper major number */
@@ -1566,6 +1568,9 @@ int main(int argc, char **argv)
                                else if (!strcmp(t, K_SHOWTOC)) {
                                        flags |= S_F_SVG_SHOW_TOC;
                                }
+                               else if (!strcmp(t, K_CUSTOMCOL)) {
+                                       palette = SVG_CUSTOM_COL_PALETTE;
+                               }
                                else {
                                        usage(argv[0]);
                                }
index bbb4c2ff6560ab4d022e8d099a4af175bc47df95..7510af09fe320798912f3235b4d4005f035d3d09 100644 (file)
@@ -35,6 +35,8 @@
 extern unsigned int flags;
 extern char *seps[];
 
+extern int palette;
+extern unsigned int svg_colors[][SVG_COL_PALETTE_SIZE];
 
 /*
  ***************************************************************************
@@ -954,9 +956,11 @@ __printf_funct_t print_svg_header(void *parm, int action, char *dfile,
                        height = 100;
                }
                printf(" width=\"%d\" height=\"%d\""
-                      " fill=\"black\" stroke=\"gray\" stroke-width=\"1\">\n",
-                      SVG_T_XSIZE * (hdr_parm->views_per_row), height);
-               printf("<text x=\"0\" y=\"30\" text-anchor=\"start\" stroke=\"brown\">");
+                      " fill=\"black\" stroke=\"#%06x\" stroke-width=\"1\">\n",
+                      SVG_T_XSIZE * (hdr_parm->views_per_row), height,
+                      svg_colors[palette][SVG_COL_DEFAULT_IDX]);
+               printf("<text x=\"0\" y=\"30\" text-anchor=\"start\" stroke=\"#%06x\">",
+                      svg_colors[palette][SVG_COL_HEADER_IDX]);
                print_gal_header(localtime_r((const time_t *) &(file_hdr->sa_ust_time), &rectime),
                                 file_hdr->sa_sysname, file_hdr->sa_release,
                                 file_hdr->sa_nodename, file_hdr->sa_machine,
@@ -985,9 +989,10 @@ __printf_funct_t print_svg_header(void *parm, int action, char *dfile,
                if (!(action & F_BEGIN)) {
                        if (!hdr_parm->graph_nr) {
                                /* No views displayed */
-                               printf("<text x= \"0\" y=\"%d\" text-anchor=\"start\" stroke=\"red\">",
+                               printf("<text x= \"0\" y=\"%d\" text-anchor=\"start\" stroke=\"#%06x\">",
                                       SVG_H_YSIZE +
-                                      SVG_C_YSIZE * (DISPLAY_TOC(flags) ? hdr_parm->nr_act_dispd : 0));
+                                      SVG_C_YSIZE * (DISPLAY_TOC(flags) ? hdr_parm->nr_act_dispd : 0),
+                                      svg_colors[palette][SVG_COL_ERROR_IDX]);
                                printf("No data!</text>\n");
                        }
                        /* Give actual SVG height */
index 2caecfe9e7d5c0981afc3c0ff9f64469ed3a493e..5e0363ee6de1829490744583bdbea74b6c50f57b 100644 (file)
 #endif
 
 extern unsigned int flags;
-
-unsigned int svg_colors[] = {0x00cc00, 0xff00bf, 0x00ffff, 0xff0000,
-                            0xe85f00, 0x0000ff, 0x006020, 0x7030a0,
-                            0xffff00, 0x666635, 0xd60093, 0x00bfbf,
-                            0xcc3300, 0x50040f, 0xffffbf, 0x193d55};
-#define SVG_COLORS_IDX_MASK    0x0f
+extern int palette;
+
+unsigned int svg_colors[SVG_COL_PALETTE_NR][SVG_COL_PALETTE_SIZE] =
+       {{0x00cc00, 0xff00bf, 0x00ffff, 0xff0000,       /* Default palette */
+         0xe85f00, 0x0000ff, 0x006020, 0x7030a0,
+         0xffff00, 0x666635, 0xd60093, 0x00bfbf,
+         0xcc3300, 0x50040f, 0xffffbf, 0x193d55,
+         0x000000, 0xffffff, 0x202020, 0xffff00,
+         0xffff00, 0x808080, 0xa52a2a, 0xff0000},
+
+        {0x915c83, 0x0000ff, 0x8a2be2, 0xde5d83,       /* Custom color palette */
+         0xff55a3, 0x993300, 0x800020, 0x008080,
+         0xdc143c, 0x006400, 0x483d8b, 0x2f4f4f,
+         0xcc3300, 0x50040f, 0xffffbf, 0x193d55,
+         0xf5f5dc, 0x000000, 0x536872, 0xff6347,
+         0xff6347, 0x808080, 0xa52a2a, 0xff0000}};
 
 /*
  ***************************************************************************
@@ -592,8 +602,10 @@ void display_hgrid(double ypos, double yfactor, double lmax, int dp)
                /* Display horizontal lines (except on X axis) */
                if (j > 0) {
                        printf("<polyline points=\"0,%.2f %d,%.2f\" style=\"vector-effect: non-scaling-stroke; "
-                              "stroke: #202020\" transform=\"scale(1,%f)\"/>\n",
-                              ypos * j, SVG_G_XSIZE, ypos * j, yfactor);
+                              "stroke: #%06x\" transform=\"scale(1,%f)\"/>\n",
+                              ypos * j, SVG_G_XSIZE, ypos * j,
+                              svg_colors[palette][SVG_COL_GRID_IDX],
+                              yfactor);
                }
 
                /*
@@ -602,9 +614,11 @@ void display_hgrid(double ypos, double yfactor, double lmax, int dp)
                 * to make sure they are properly aligned.
                 */
                sprintf(stmp, "%.2f", ypos * j);
-               printf("<text x=\"0\" y=\"%ld\" style=\"fill: white; stroke: none; font-size: 12px; "
+               printf("<text x=\"0\" y=\"%ld\" style=\"fill: #%06x; stroke: none; font-size: 12px; "
                       "text-anchor: end\">%.*f.</text>\n",
-                      (long) (atof(stmp) * yfactor), dp, ypos * j);
+                      (long) (atof(stmp) * yfactor),
+                      svg_colors[palette][SVG_COL_AXIS_IDX],
+                      dp, ypos * j);
                j++;
        }
        while ((ypos * j <= lmax) && (j < MAX_HLINES_NR));
@@ -642,29 +656,36 @@ void display_vgrid(long int xpos, double xfactor, int v_gridnr, struct svg_parm
                sa_get_record_timestamp_struct(flags, &stamp, &rectime, NULL);
                set_record_timestamp_string(flags, &stamp, NULL, cur_time, TIMESTAMP_LEN, &rectime);
                printf("<polyline points=\"%ld,0 %ld,%d\" style=\"vector-effect: non-scaling-stroke; "
-                      "stroke: #202020\" transform=\"scale(%f,1)\"/>\n",
-                      xpos * j, xpos * j, -SVG_G_YSIZE, xfactor);
+                      "stroke: #%06x\" transform=\"scale(%f,1)\"/>\n",
+                      xpos * j, xpos * j, -SVG_G_YSIZE,
+                      svg_colors[palette][SVG_COL_GRID_IDX],
+                      xfactor);
                /*
                 * Display graduations.
                 * NB: We may have tm_min != 0 if we have more than 24H worth of data in one datafile.
                 * In this case, we should rather display the exact time instead of only the hour.
                 */
                if (DISPLAY_ONE_DAY(flags) && (rectime.tm_min == 0)) {
-                       printf("<text x=\"%ld\" y=\"15\" style=\"fill: white; stroke: none; font-size: 14px; "
+                       printf("<text x=\"%ld\" y=\"15\" style=\"fill: #%06x; stroke: none; font-size: 14px; "
                               "text-anchor: start\">%2d:00</text>\n",
-                              (long) (xpos * j * xfactor) - 15, rectime.tm_hour);
+                              (long) (xpos * j * xfactor) - 15,
+                              svg_colors[palette][SVG_COL_AXIS_IDX],
+                              rectime.tm_hour);
                }
                else {
-                       printf("<text x=\"%ld\" y=\"10\" style=\"fill: white; stroke: none; font-size: 12px; "
+                       printf("<text x=\"%ld\" y=\"10\" style=\"fill: #%06x; stroke: none; font-size: 12px; "
                               "text-anchor: start\" transform=\"rotate(45,%ld,0)\">%s</text>\n",
-                              (long) (xpos * j * xfactor), (long) (xpos * j * xfactor), cur_time);
+                              (long) (xpos * j * xfactor),
+                              svg_colors[palette][SVG_COL_AXIS_IDX],
+                              (long) (xpos * j * xfactor), cur_time);
                }
                stamp.ust_time += xpos;
        }
 
        if (!PRINT_LOCAL_TIME(flags)) {
-               printf("<text x=\"-10\" y=\"30\" style=\"fill: yellow; stroke: none; font-size: 12px; "
-                      "text-anchor: end\">UTC</text>\n");
+               printf("<text x=\"-10\" y=\"30\" style=\"fill: #%06x; stroke: none; font-size: 12px; "
+                      "text-anchor: end\">UTC</text>\n",
+                      svg_colors[palette][SVG_COL_INFO_IDX]);
        }
 }
 
@@ -873,19 +894,23 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
                }
 
                /* Graph background */
-               printf("<rect x=\"%d\" y=\"%d\" height=\"%d\" width=\"%d\"/>\n",
-                      xv, yv, SVG_V_YSIZE, SVG_V_XSIZE);
+               printf("<rect x=\"%d\" y=\"%d\" height=\"%d\" width=\"%d\" fill=\"#%06x\"/>\n",
+                      xv, yv, SVG_V_YSIZE, SVG_V_XSIZE,
+                      svg_colors[palette][SVG_COL_BCKGRD_IDX]);
 
                /* Graph title */
-               printf("<text x=\"%d\" y=\"%d\" style=\"fill: yellow; stroke: none\">%s",
-                      xv, 20 + yv, title[i]);
+               printf("<text x=\"%d\" y=\"%d\" style=\"fill: #%06x; stroke: none\">%s",
+                      xv, 20 + yv,
+                      svg_colors[palette][SVG_COL_TITLE_IDX],
+                      title[i]);
                if (item_name) {
                        printf(" [%s]", item_name);
                }
                printf("\n");
-               printf("<tspan x=\"%d\" y=\"%d\" style=\"fill: yellow; stroke: none; font-size: 12px\">"
+               printf("<tspan x=\"%d\" y=\"%d\" style=\"fill: #%06x; stroke: none; font-size: 12px\">"
                       "(Min, Max values)</tspan>\n</text>\n",
-                      xv + 5 + SVG_M_XSIZE + SVG_G_XSIZE, yv + 25);
+                      xv + 5 + SVG_M_XSIZE + SVG_G_XSIZE, yv + 25,
+                      svg_colors[palette][SVG_COL_INFO_IDX]);
 
                /*
                 * At least two samples are needed.
@@ -894,17 +919,20 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
                if ((record_hdr->ust_time == svg_p->ust_time_first) ||
                    (*(spmin + pos) == DBL_MAX) || (*(spmax + pos) == -DBL_MIN)) {
                        /* No data found */
-                       printf("<text x=\"%d\" y=\"%d\" style=\"fill: red; stroke: none\">No data</text>\n",
-                              xv, yv + SVG_M_YSIZE);
+                       printf("<text x=\"%d\" y=\"%d\" style=\"fill: #%06x; stroke: none\">No data</text>\n",
+                              xv, yv + SVG_M_YSIZE,
+                              svg_colors[palette][SVG_COL_ERROR_IDX]);
                        skip_current_view(out, &pos, group[i]);
                        continue;
                }
 
                /* X and Y axis */
-               printf("<polyline points=\"%d,%d %d,%d %d,%d\" stroke=\"white\" stroke-width=\"2\"/>\n",
+               printf("<polyline points=\"%d,%d %d,%d %d,%d\" style=\"fill: #%06x; stroke: #%06x; stroke-width: 2\"/>\n",
                       xv + SVG_M_XSIZE, yv + SVG_M_YSIZE,
                       xv + SVG_M_XSIZE, yv + SVG_M_YSIZE + SVG_G_YSIZE,
-                      xv + SVG_M_XSIZE + SVG_G_XSIZE, yv + SVG_M_YSIZE + SVG_G_YSIZE);
+                      xv + SVG_M_XSIZE + SVG_G_XSIZE, yv + SVG_M_YSIZE + SVG_G_YSIZE,
+                      svg_colors[palette][SVG_COL_BCKGRD_IDX],
+                      svg_colors[palette][SVG_COL_AXIS_IDX]);
 
                /* Autoscaling graphs if needed */
                gr_autoscaling(asfactor, 16, group[i], g_type[i], pos, gmax, spmax);
@@ -917,7 +945,7 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
                        printf("<text x=\"%d\" y=\"%d\" style=\"fill: #%06x; stroke: none; font-size: 12px\">"
                               "%s %s(%.*f, %.*f)</text>\n",
                               xv + 5 + SVG_M_XSIZE + SVG_G_XSIZE, yv + SVG_M_YSIZE + j * 15,
-                              svg_colors[(pos + j) & SVG_COLORS_IDX_MASK], g_title[pos + j] + dp,
+                              svg_colors[palette][(pos + j) & SVG_COLORS_IDX_MASK], g_title[pos + j] + dp,
                               asfactor[j] == 1 ? "" : val,
                               !dp * 2, *(spmin + pos + j) * asfactor[j],
                               !dp * 2, *(spmax + pos + j) * asfactor[j]);
@@ -926,18 +954,20 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
                if (DISPLAY_INFO(flags)) {
                        /* Display additional info (hostname, date) */
                        printf("<text x=\"%d\" y=\"%d\" "
-                              "style=\"fill: yellow; text-anchor: end; stroke: none; font-size: 14px\">"
+                              "style=\"fill: #%06x; text-anchor: end; stroke: none; font-size: 14px\">"
                               "%s\n",
                               xv + SVG_V_XSIZE - 5, yv + SVG_M_YSIZE + SVG_G_YSIZE,
+                              svg_colors[palette][SVG_COL_INFO_IDX],
                               svg_p->file_hdr->sa_nodename);
 
                        /* Get report date */
                        set_report_date(localtime_r((const time_t *) &(svg_p->file_hdr->sa_ust_time), &rectime),
                                        cur_date, sizeof(cur_date));
                        printf("<tspan x=\"%d\" y=\"%d\" "
-                              "style=\"fill: yellow; text-anchor: end; stroke: none; font-size: 14px\">"
+                              "style=\"fill: #%06x; text-anchor: end; stroke: none; font-size: 14px\">"
                               "%s</tspan>\n</text>\n",
                               xv + SVG_V_XSIZE - 5, yv + SVG_M_YSIZE + SVG_G_YSIZE + 14,
+                              svg_colors[palette][SVG_COL_INFO_IDX],
                               cur_date);
                }
 
@@ -998,14 +1028,14 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
                                       "stroke: #%06x; stroke-width: 1; fill-opacity: 0\" "
                                       "transform=\"scale(%f,%f)\"/>\n",
                                       out_p,
-                                      svg_colors[(pos + j) & SVG_COLORS_IDX_MASK],
+                                      svg_colors[palette][(pos + j) & SVG_COLORS_IDX_MASK],
                                       xfactor,
                                       yfactor * asfactor[j]);
                        }
                        else if (*out_p) {      /* Ignore flat bars */
                                /* Bar graphs */
                                printf("<g style=\"fill: #%06x; stroke: none\" transform=\"scale(%f,%f)\">\n",
-                                      svg_colors[(pos + j) & SVG_COLORS_IDX_MASK], xfactor, yfactor);
+                                      svg_colors[palette][(pos + j) & SVG_COLORS_IDX_MASK], xfactor, yfactor);
                                printf("%s\n", out_p);
                                printf("</g>\n");
                        }