TransWikia.com

how to embed plot in gtk and glade

Stack Overflow Asked by user169808 on December 23, 2021

I’m trying to create a simple gui using glade and gtk in C. In the GUI I want to add a widget that plots data (a line plot and a histogram are all I want) (constantly updating) and I dont know what to use. I’ve seen that gnuplot is popular, but I have no idea how to use it in an application. I would like some suggestions and some instructions because I cannot find it.

here is some sample code that is what I am using as a base, just to give you an idea

update:
I built gnuplot on my pc and now when I run the code it doesnt open a new window and seems to plot inside my window. the only problem is that the widget in my gtkbox container doesnt resize so it looks like a tiny white bar. Im assuming that it is working because i removed

gui.graph = gnuplot_init() ;
    gnuplot_setstyle(gui.graph, "lines") ;
    gnuplot_cmd(gui.graph, "set terminal x11 window "%x"", (int)gtk_socket_get_id (gui.socket));
    gnuplot_plot_slope(gui.graph, 1.0, 0.0, "unity slope") ;

and ran the commands on a seperate terminal and the white bar i mentioned pops up.

#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include "variables.h"
#include "gnuplot_i.h"

struct test tv;
struct GUI gui;

// Functions
void GUISetup();

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    gui.builder = gtk_builder_new_from_file("glade/window_main.glade");

    gui.window = GTK_WIDGET(gtk_builder_get_object(gui.builder, "window_main"));
    gtk_builder_connect_signals(gui.builder, NULL);
    
    GUISetup();
    
    g_object_unref(gui.builder);
    // start fullbuilderscreen
    gtk_window_fullscreen(GTK_WINDOW(gui.window));
    gtk_widget_show(gui.window);                
    gtk_main();

    return 0;
}

void GUISetup(){
    gui.socket = gtk_socket_new ();
    gtk_container_add (GTK_CONTAINER (gui.MainContainer), gui.socket);
    //gtk_widget_show (gui.socket);
    //gtk_widget_realize (gui.socket);
    
    gui.graph = gnuplot_init() ;
    gnuplot_setstyle(gui.graph, "lines") ;
    gnuplot_cmd(gui.graph, "set terminal x11 window "%x"", (int)gtk_socket_get_id (gui.socket));
    gnuplot_plot_slope(gui.graph, 1.0, 0.0, "unity slope") ;
}

when I make I get this:

gcc -c -g -O0 -Wall -pthread -pipe src/main.c `pkg-config --cflags --libs gtk+-3.0` -o main.o 
src/main.c: In function ‘GUISetup’:
src/main.c:66:86: warning: passing argument 1 of ‘gtk_socket_get_id’ from incompatible pointer type [-Wincompatible-pointer-types]
 ui.graph, "set terminal x11 window "%x"", (int)gtk_socket_get_id (gui.socket));
                                                                     ^~~
In file included from /usr/include/gtk-3.0/gtk/gtkx.h:29:0,
                 from src/main.c:25:
/usr/include/gtk-3.0/gtk/gtksocket.h:81:12: note: expected ‘GtkSocket * {aka struct _GtkSocket *}’ but argument is of type ‘GtkWidget * {aka struct _GtkWidget *}’
 Window     gtk_socket_get_id          (GtkSocket *socket_);
            ^~~~~~~~~~~~~~~~~
gcc -o interface main.o gnuplot_i.o -pthread `pkg-config --cflags --libs gtk+-3.0` -export-dynamic

i tested the code manually and got the same results as found here, so I am going to try building gnuplot and altering the config files. Is there any alternative to the solution found in that post?

One Answer

For gnuplot:

http://ndevilla.free.fr/gnuplot/gnuplot_i/index.html

Or you can use Gtk::Socket, start by create an Gtk::Socket and add it to your widget. Then pass the socket ID (as hex) to gnuplot 'set terminal x11 window "ID"'. And that's it!

Matplot can be better:

https://github.com/lava/matplotlib-cpp

Answered by Ôrel on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP