Attached below is a simple example. Something that you should keep in mind
is that gtkplot expects you to keep the data around as long as the plot is
displayed, i.e. it accepts a pointer to the data, but it does not copy it.
Presumably for perfomance reasons.

Regards,
Dov

#include <gtk/gtk.h>

#include <gtkextra/gtkextra.h>

#include <math.h>

 GtkWidget *w_top;

GtkPlotData *dataset;

 void

build_example1(GtkWidget *plot)

{

    GdkColor color;

     static gdouble px1[]={0., 0.2, 0.4, 0.6, 0.8, 1.0};

    static gdouble py1[]={.2, .4, .5, .35, .10, .40};

     dataset = GTK_PLOT_DATA(gtk_plot_data_new());

    gtk_plot_data_set_points(dataset, px1, py1, NULL, NULL, 6);

     gdk_color_parse("red", &color);

     gtk_plot_data_set_symbol(dataset,

                             GTK_PLOT_SYMBOL_DIAMOND,

                             GTK_PLOT_SYMBOL_EMPTY,

                             10, 2, &color, &color);

    gtk_plot_data_set_line_attributes(dataset,

                                      GTK_PLOT_LINE_SOLID,

                                      (GdkCapStyle)0,

                                      (GdkJoinStyle)0,

                                      2, &color);

     gtk_plot_data_set_connector(dataset, GTK_PLOT_CONNECT_STRAIGHT);

     gtk_plot_data_hide_legend(dataset);

    gtk_plot_add_data(GTK_PLOT(plot), dataset);

    gtk_widget_show(GTK_WIDGET(dataset));

}

 // This signal handler resizes the graph if its allocation was changed

int plot_expose_event(GtkWidget *canvas,

                      GdkEventConfigure *event,

                      gpointer user_data)

{

    int width = canvas->allocation.width;

    int height = canvas->allocation.height;

     if (width != GTK_PLOT_CANVAS(canvas)->width

        || height != GTK_PLOT_CANVAS(canvas)->height) {

        gtk_plot_canvas_set_size(GTK_PLOT_CANVAS(canvas),

                                 width, height);

        gtk_plot_canvas_paint(GTK_PLOT_CANVAS(canvas));

    }

     return 0;

}

 GtkWidget *create_plot()

{

    GtkWidget *w_plot_canvas, *w_plot;

    GtkPlotCanvasChild *child;

    GtkPlotAxis *x_axis, *y_axis;

     w_plot_canvas = gtk_plot_canvas_new(500,300,1.0);

     // Do this in order to have auto resize of plot with window.

    GTK_PLOT_CANVAS_UNSET_FLAGS (GTK_PLOT_CANVAS (w_plot_canvas),

                                 GTK_PLOT_CANVAS_DND_FLAGS);

    GTK_PLOT_CANVAS_SET_FLAGS (GTK_PLOT_CANVAS (w_plot_canvas),

                               GTK_PLOT_CANVAS_CAN_RESIZE);

     w_plot = gtk_plot_new(NULL);

     gtk_plot_set_range(GTK_PLOT(w_plot), 0, 1., -1., 1.4);

    gtk_plot_axis_set_ticks(gtk_plot_get_axis(GTK_PLOT(w_plot),

                                              GTK_PLOT_AXIS_LEFT),

                            0.5,5);

    child = gtk_plot_canvas_plot_new(GTK_PLOT(w_plot));

    gtk_plot_canvas_put_child(GTK_PLOT_CANVAS(w_plot_canvas), child,

                              .10, .1, .9, .85);

     // Setup the axis

    gtk_plot_set_legends_border(GTK_PLOT(w_plot), (GtkPlotBorderStyle)0, 0);

    gtk_plot_axis_set_visible(gtk_plot_get_axis(GTK_PLOT(w_plot),
GTK_PLOT_AXIS_TOP), FALSE);

    gtk_plot_axis_set_visible(gtk_plot_get_axis(GTK_PLOT(w_plot),
GTK_PLOT_AXIS_RIGHT), FALSE);

    x_axis = gtk_plot_get_axis(GTK_PLOT(w_plot), GTK_PLOT_AXIS_BOTTOM);

    y_axis = gtk_plot_get_axis(GTK_PLOT(w_plot), GTK_PLOT_AXIS_LEFT);

     gtk_plot_axis_set_title(x_axis, "Time [t]");

    gtk_plot_axis_hide_title(y_axis);

    gtk_plot_x0_set_visible(GTK_PLOT(w_plot), TRUE);

    gtk_plot_y0_set_visible(GTK_PLOT(w_plot), TRUE);

    gtk_plot_canvas_put_child(GTK_PLOT_CANVAS(w_plot_canvas),

                              gtk_plot_canvas_text_new("Helvetica",

                                                       12, 0,

                                                       NULL, NULL,

                                                       FALSE,

                                                       GTK_JUSTIFY_CENTER,

                                                       "Intensity"

                                                       ),

                              .10, .1, .20, .20);

     // Build data and put it in plot

    build_example1(w_plot);

    gtk_widget_show(GTK_WIDGET(w_plot));

     g_signal_connect(G_OBJECT(w_plot_canvas), "expose-event",

                     G_CALLBACK(&plot_expose_event), NULL);

     return w_plot_canvas;

}

 // This is an example of how to update the data displayed in the

// widget. For more examples, see testrealtime.c.

void cb_change_dataset(GtkWidget *button,

                       gpointer user_data

                       )

{

    GtkWidget *canvas = GTK_WIDGET(user_data);

    static gdouble px1[]={0., 0.2, 0.4, 0.6, 0.8, 1.0};

    static gdouble py1[]={.35, .30, .40, .2, .4, .5 };

     gtk_plot_data_set_points(dataset, px1, py1, NULL, NULL, 6);

    gtk_plot_canvas_paint(GTK_PLOT_CANVAS(canvas));

    gtk_widget_queue_draw(canvas);

}

 void create_widgets()

{

    GtkWidget *w_vbox, *w_plot_canvas, *w_button;

     w_top = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_signal_connect(GTK_OBJECT(w_top),

                       "delete_event",

                       gtk_main_quit,

                       NULL);

     w_vbox = gtk_vbox_new(FALSE, 0);

    gtk_container_add(GTK_CONTAINER(w_top), w_vbox);

     w_plot_canvas = create_plot();

    gtk_box_pack_start(GTK_BOX(w_vbox), w_plot_canvas, TRUE, TRUE, 0);

     w_button = gtk_button_new_with_label("ok");

    gtk_box_pack_start(GTK_BOX(w_vbox), w_button, FALSE, FALSE, 0);

    gtk_signal_connect(GTK_OBJECT(w_button),

                       "clicked",

                       gtk_main_quit,

                       NULL);

     gtk_widget_show_all(w_top);

}

 int main(int argc, char *argv[])

{

    gtk_init(&argc, &argv);

     create_widgets();

     gtk_main();

}



On Wed, Mar 24, 2010 at 07:04, Arsen Mamikonyan <mamikony...@yahoo.com>wrote:

> Hello everyone,
>
> I'm trying to write an application with Gtk+/GtkPlot that will periodically
> plot the data received over network/internet.
> I have some problem with figuring out how to make graphs and show them
> using GtkPlot, mainly because I can't find any documentation or simple
> ploting program examples. I'm new to Gtk so I have hard time figuring out by
> myself.
> I would appraciate very much if someone can give me an example how to plot
> and  show graph out of sample data points.
>
> Thanks and Best Regards,
>
> Arsen Mamikonyan
> MIT Class of 2012
>
>
>
>
> _______________________________________________
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
  • GtkPlot Arsen Mamikonyan
    • Re: GtkPlot Dov Grobgeld

Reply via email to