Trouble with GtkPlot

Dan H <[email protected]> Thu, 6 Dec 2007 10:43:04 +0100
Newsgroups gmane.comp.scigraphica.gtkextra
Message-ID <[email protected]>
Hello,

currently I'm stumped by GtkPlot. Essentially I stripped down the
"testgtkplot" example and embedded it into my application, but under
most circumstances I can't get any graph to show up (the axes come
up, just no data). What's funny is that if I pack all the canvas,
dataset, plot creation etc. in a callback connected to the "realize"
signal of the containing widget (a GtkFrame), everything shows up
fine. But if the plot is created or changed after the gtk_frame is
fully operational (from within the main event loop), I only get the
white background and the axes, but no plot data. I tried calling
gtk_widget_queue_draw() on the GtkPlot but that didn't change
anything. Below is a code snippet. The whole thing is contained in a
user interface created with Glade.

The whole code is here:

http://www.nanoscience.de/group_r/members/dhaude/tmp/test-0.1.tar.gz

Thanks, --D.


-------------------------------------------

/* everything is static so I don't accidentally lose references */
static struct {
    GtkPlotData *dataset;
    GtkWidget *plot;
    GtkWidget *canvas;
    GtkWidget *container;
    GtkPlotCanvasChild *child;
} P = {0};

void test(void)
{
  g_assert(P.container);
  build_plot();
  build_example1();
}

void on_b_test_clicked(GtkButton *button, gpointer user_data)
{
  /* here the test() call doesn't work -- after clicking on the button to
     which this callback is connected, only the axes show up but not
     the plot */
  test();
}

void on_f_plot_realize(GtkWidget *widget, gpointer user_data)
{
  P.container = lookup_widget(button, "f_plot"); /* or some other method to find the containing frame */
  (void) user_data;
  /* if test() is called here, the plot shows up correctly. */
}

/* This is just more or less copied out of the GtkExtra example code */

gboolean build_example1()
{
    GdkColor color;

    static gdouble px1[] = { 0., 0.2, 0.4, 0.6, 0.8, 1.0 };
    static gdouble py1[] = { .2, .4, .5, .35, .30, 1.40 };
    static gdouble dx1[] = { .4, .6, .7, .2, .2, .2 };
    static gdouble dy1[] = { .1, .1, .1, .1, .1, .4 };

    gtk_plot_add_data(GTK_PLOT(P.plot), P.dataset);
    gtk_widget_show(GTK_WIDGET(P.dataset));

    gtk_plot_data_set_points(P.dataset, px1, py1, dx1, dy1, 6);

    gdk_color_parse("red", &color);
    gdk_color_alloc(gdk_colormap_get_system(), &color);

    gtk_plot_data_set_symbol(P.dataset,
        GTK_PLOT_SYMBOL_DIAMOND,
        GTK_PLOT_SYMBOL_EMPTY, 10, 2, &color, &color);
    gtk_plot_data_set_line_attributes(P.dataset,
        GTK_PLOT_LINE_SOLID, 0, 0, 1, &color);

    gtk_plot_data_set_connector(P.dataset, GTK_PLOT_CONNECT_SPLINE);

    gtk_plot_data_show_yerrbars(P.dataset);
    gtk_plot_data_set_legend(P.dataset, "Spline + EY");
    gtk_widget_show(GTK_WIDGET(P.dataset));
    gtk_widget_show(GTK_WIDGET(P.plot)); 
    return FALSE;
}

static void build_plot(void)
{
    P.canvas =
        gtk_plot_canvas_new(P.container->allocation.width,
        P.container->allocation.height, 1);
    GTK_PLOT_CANVAS_SET_FLAGS(GTK_PLOT_CANVAS(P.canvas), 0);
    gtk_container_add(GTK_CONTAINER(P.container), P.canvas);
    gtk_widget_show(P.canvas); 

    P.plot = gtk_plot_new(NULL);
    gtk_plot_set_range(GTK_PLOT(P.plot), -1., 1., -1., 1.4);
    gtk_plot_set_legends_border(GTK_PLOT(P.plot), 0, 0);
    gtk_plot_axis_hide_title(gtk_plot_get_axis(GTK_PLOT(P.plot),
            GTK_PLOT_AXIS_TOP));
    gtk_plot_axis_show_ticks(gtk_plot_get_axis(GTK_PLOT(P.plot),
            GTK_PLOT_AXIS_BOTTOM), 15, 3); 
    gtk_plot_set_ticks(GTK_PLOT(P.plot), GTK_PLOT_AXIS_X, 0.2, 5);
    gtk_plot_set_ticks(GTK_PLOT(P.plot), GTK_PLOT_AXIS_Y, 1., 3);
    gtk_widget_show(GTK_WIDGET(P.plot));
    
    P.child = gtk_plot_canvas_plot_new(GTK_PLOT(P.plot));
    gtk_plot_canvas_put_child(GTK_PLOT_CANVAS(P.canvas), P.child, 0, 0, 1, 1);
    GTK_PLOT_CANVAS_PLOT(P.child)->flags |= GTK_PLOT_CANVAS_PLOT_SELECT_POINT;
    GTK_PLOT_CANVAS_PLOT(P.child)->flags |= GTK_PLOT_CANVAS_PLOT_DND_POINT;  
    P.dataset = GTK_PLOT_DATA(gtk_plot_data_new()); 
    gtk_widget_show(GTK_WIDGET(P.dataset));
}

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4