gfio: encapsulate x- and y-offsets into graph library
authorStephen M. Cameron <stephenmcameron@gmail.com>
Sun, 11 Mar 2012 10:36:51 +0000 (11:36 +0100)
committerJens Axboe <axboe@kernel.dk>
Sun, 11 Mar 2012 10:36:51 +0000 (11:36 +0100)
Don't need to carry around drawing area dimensions alongside
the graphs, the size and xy-offsets of the graphs can be set
directly in the drawing area configure callback.

Signed-off-by: Stephen M. Cameron <stephenmcameron@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
gfio.c
graph.c
graph.h

diff --git a/gfio.c b/gfio.c
index d2fd94aa0dd5bdd3d1afcbef9699efaa18743e67..936e41db2117c22d16a68ea09eb1770b7ddd74f9 100644 (file)
--- a/gfio.c
+++ b/gfio.c
@@ -89,9 +89,6 @@ struct gfio_graphs {
 #define DRAWING_AREA_XDIM 1000
 #define DRAWING_AREA_YDIM 400
        GtkWidget *drawing_area;
 #define DRAWING_AREA_XDIM 1000
 #define DRAWING_AREA_YDIM 400
        GtkWidget *drawing_area;
-       int drawing_area_xdim;
-       int drawing_area_ydim;
-
        struct graph *iops_graph;
        struct graph *bandwidth_graph;
 };
        struct graph *iops_graph;
        struct graph *bandwidth_graph;
 };
@@ -1130,35 +1127,28 @@ static gint on_config_drawing_area(GtkWidget *w, GdkEventConfigure *event,
 {
        struct gfio_graphs *g = data;
 
 {
        struct gfio_graphs *g = data;
 
-       g->drawing_area_xdim = w->allocation.width;
-       g->drawing_area_ydim = w->allocation.height;
+       graph_set_size(g->iops_graph, w->allocation.width / 2.0, w->allocation.height);
+       graph_set_position(g->iops_graph, w->allocation.width / 2.0, 0.0);
+       graph_set_size(g->bandwidth_graph, w->allocation.width / 2.0, w->allocation.height);
+       graph_set_position(g->bandwidth_graph, 0, 0);
        return TRUE;
 }
 
        return TRUE;
 }
 
+static void draw_graph(struct graph *g, cairo_t *cr)
+{
+       line_graph_draw(g, cr);
+       cairo_stroke(cr);
+}
+
 static int on_expose_drawing_area(GtkWidget *w, GdkEvent *event, gpointer p)
 {
        struct gfio_graphs *g = p;
        cairo_t *cr;
 
 static int on_expose_drawing_area(GtkWidget *w, GdkEvent *event, gpointer p)
 {
        struct gfio_graphs *g = p;
        cairo_t *cr;
 
-       graph_set_size(g->iops_graph, g->drawing_area_xdim / 2.0,
-                                       g->drawing_area_ydim);
-       graph_set_size(g->bandwidth_graph, g->drawing_area_xdim / 2.0,
-                                       g->drawing_area_ydim);
        cr = gdk_cairo_create(w->window);
        cr = gdk_cairo_create(w->window);
-
        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_set_source_rgb(cr, 0, 0, 0);
-
-       cairo_save(cr);
-       cairo_translate(cr, 0, 0);
-       line_graph_draw(g->bandwidth_graph, cr);
-       cairo_stroke(cr);
-       cairo_restore(cr);
-
-       cairo_save(cr);
-       cairo_translate(cr, g->drawing_area_xdim / 2.0, 0);
-       line_graph_draw(g->iops_graph, cr);
-       cairo_stroke(cr);
-       cairo_restore(cr);
+       draw_graph(g->iops_graph, cr);
+       draw_graph(g->bandwidth_graph, cr);
        cairo_destroy(cr);
 
        return FALSE;
        cairo_destroy(cr);
 
        return FALSE;
@@ -2324,10 +2314,8 @@ static GtkWidget *new_client_page(struct gui_entry *ge)
         */
        gdk_color_parse("white", &white);
        ge->graphs.drawing_area = gtk_drawing_area_new();
         */
        gdk_color_parse("white", &white);
        ge->graphs.drawing_area = gtk_drawing_area_new();
-       ge->graphs.drawing_area_xdim = DRAWING_AREA_XDIM;
-       ge->graphs.drawing_area_ydim = DRAWING_AREA_YDIM;
        gtk_widget_set_size_request(GTK_WIDGET(ge->graphs.drawing_area),
        gtk_widget_set_size_request(GTK_WIDGET(ge->graphs.drawing_area),
-               ge->graphs.drawing_area_xdim, ge->graphs.drawing_area_ydim);
+               DRAWING_AREA_XDIM, DRAWING_AREA_YDIM);
        gtk_widget_modify_bg(ge->graphs.drawing_area, GTK_STATE_NORMAL, &white);
        g_signal_connect(G_OBJECT(ge->graphs.drawing_area), "expose_event",
                                G_CALLBACK(on_expose_drawing_area), &ge->graphs);
        gtk_widget_modify_bg(ge->graphs.drawing_area, GTK_STATE_NORMAL, &white);
        g_signal_connect(G_OBJECT(ge->graphs.drawing_area), "expose_event",
                                G_CALLBACK(on_expose_drawing_area), &ge->graphs);
@@ -2415,10 +2403,8 @@ static GtkWidget *new_main_page(struct gui *ui)
         */
        gdk_color_parse("white", &white);
        ui->graphs.drawing_area = gtk_drawing_area_new();
         */
        gdk_color_parse("white", &white);
        ui->graphs.drawing_area = gtk_drawing_area_new();
-       ui->graphs.drawing_area_xdim = DRAWING_AREA_XDIM;
-       ui->graphs.drawing_area_ydim = DRAWING_AREA_YDIM;
        gtk_widget_set_size_request(GTK_WIDGET(ui->graphs.drawing_area),
        gtk_widget_set_size_request(GTK_WIDGET(ui->graphs.drawing_area),
-               ui->graphs.drawing_area_xdim, ui->graphs.drawing_area_ydim);
+               DRAWING_AREA_XDIM, DRAWING_AREA_YDIM);
        gtk_widget_modify_bg(ui->graphs.drawing_area, GTK_STATE_NORMAL, &white);
        g_signal_connect(G_OBJECT(ui->graphs.drawing_area), "expose_event",
                        G_CALLBACK(on_expose_drawing_area), &ui->graphs);
        gtk_widget_modify_bg(ui->graphs.drawing_area, GTK_STATE_NORMAL, &white);
        g_signal_connect(G_OBJECT(ui->graphs.drawing_area), "expose_event",
                        G_CALLBACK(on_expose_drawing_area), &ui->graphs);
diff --git a/graph.c b/graph.c
index de65055b99c1ffbb0dc00c88b805629c9bfd5bc9..111a7203b4a44ab5975cc43c730e8c808c13f429 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -55,6 +55,7 @@ struct graph {
        char *xtitle;
        char *ytitle;
        unsigned int xdim, ydim;
        char *xtitle;
        char *ytitle;
        unsigned int xdim, ydim;
+       double xoffset, yoffset;
        struct graph_label *labels;
        struct graph_label *tail;
        int per_label_limit;
        struct graph_label *labels;
        struct graph_label *tail;
        int per_label_limit;
@@ -69,6 +70,12 @@ void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
        g->ydim = ydim;
 }
 
        g->ydim = ydim;
 }
 
+void graph_set_position(struct graph *g, double xoffset, double yoffset)
+{
+       g->xoffset = xoffset;
+       g->yoffset = yoffset;
+}
+
 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
 {
        struct graph *g;
 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
 {
        struct graph *g;
@@ -397,6 +404,7 @@ void bar_graph_draw(struct graph *bg, cairo_t *cr)
        struct graph_label *lb;
 
        cairo_save(cr);
        struct graph_label *lb;
 
        cairo_save(cr);
+       cairo_translate(cr, bg->xoffset, bg->yoffset);
        graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
 
        nlabels = count_labels(bg->labels);
        graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
 
        nlabels = count_labels(bg->labels);
@@ -473,6 +481,7 @@ void line_graph_draw(struct graph *g, cairo_t *cr)
        int good_data = 1, first = 1;
 
        cairo_save(cr);
        int good_data = 1, first = 1;
 
        cairo_save(cr);
+       cairo_translate(cr, g->xoffset, g->yoffset);
        graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
 
        minx = find_xy_value(g, getx, mindouble);
        graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
 
        minx = find_xy_value(g, getx, mindouble);
diff --git a/graph.h b/graph.h
index 9eb33d7fe121b7e0062cb8e16e5794e728f931f1..0f286498931a5ce6060adc357de8edc21abfe1ce 100644 (file)
--- a/graph.h
+++ b/graph.h
@@ -8,6 +8,8 @@ struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font);
 /* graph_new() Returns a new graph structure of the given dimensions and font */
 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim);
 /* graph_set_size() Changes the size of a graph to the given dimensions. */ 
 /* graph_new() Returns a new graph structure of the given dimensions and font */
 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim);
 /* graph_set_size() Changes the size of a graph to the given dimensions. */ 
+void graph_set_position(struct graph *g, double xoffset, double yoffset);
+/* graph_set_position() sets the x- and y-offset to translate the graph */
 void bar_graph_draw(struct graph *g, cairo_t *cr);
 /* bar_graph_draw() draws the given graph as a bar graph */
 void line_graph_draw(struct graph *g, cairo_t *cr);
 void bar_graph_draw(struct graph *g, cairo_t *cr);
 /* bar_graph_draw() draws the given graph as a bar graph */
 void line_graph_draw(struct graph *g, cairo_t *cr);