基于GTK +的图表?

pau*_*op6 13 c++ gtk gtk2

我希望能够使用GTK +制作折线图,但我不确定如何处理这个问题.有没有人得到任何提示或提示?

lib*_*rce 12

编辑:

这是该程序的GTK + 2和GTK + 3版本:

原始答案:

这是一个使用cairo绘制简单数学函数的GTK2应用程序:

#include <gtk/gtk.h>
#include <math.h>
#include <cairo.h>

#define WIDTH   640
#define HEIGHT  480

#define ZOOM_X  100.0
#define ZOOM_Y  100.0


gfloat f (gfloat x)
{
    return 0.03 * pow (x, 3);
}

static gboolean
on_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
    cairo_t *cr = gdk_cairo_create (widget->window);
    GdkRectangle da;            /* GtkDrawingArea size */
    gdouble dx = 5.0, dy = 5.0; /* Pixels between each point */
    gdouble i, clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
    gint unused = 0;

    /* Define a clipping zone to improve performance */
    cairo_rectangle (cr, 
            event->area.x, 
            event->area.y, 
            event->area.width, 
            event->area.height);
    cairo_clip (cr);

    /* Determine GtkDrawingArea dimensions */
    gdk_window_get_geometry (widget->window, 
            &da.x, 
            &da.y, 
            &da.width, 
            &da.height, 
            &unused);

    /* Draw on a black background */
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
    cairo_paint (cr);

    /* Change the transformation matrix */
    cairo_translate (cr, da.width / 2, da.height / 2);
    cairo_scale (cr, ZOOM_X, -ZOOM_Y);  

    /* Determine the data points to calculate (ie. those in the clipping zone */
    cairo_device_to_user_distance (cr, &dx, &dy);
    cairo_clip_extents (cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
    cairo_set_line_width (cr, dx);

    /* Draws x and y axis */
    cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
    cairo_move_to (cr, clip_x1, 0.0);
    cairo_line_to (cr, clip_x2, 0.0);
    cairo_move_to (cr, 0.0, clip_y1);
    cairo_line_to (cr, 0.0, clip_y2);
    cairo_stroke (cr);

    /* Link each data point */
    for (i = clip_x1; i < clip_x2; i += dx)
        cairo_line_to (cr, i, f (i));

    /* Draw the curve */
    cairo_set_source_rgba (cr, 1, 0.2, 0.2, 0.6);
    cairo_stroke (cr);

    cairo_destroy (cr);
    return FALSE;
}


int
main (int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *da;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size (GTK_WINDOW (window), WIDTH, HEIGHT);
    gtk_window_set_title (GTK_WINDOW (window), "Graph drawing");
    g_signal_connect (G_OBJECT (window), "destroy", gtk_main_quit, NULL);

    da = gtk_drawing_area_new ();
    gtk_container_add (GTK_CONTAINER (window), da);

    g_signal_connect (G_OBJECT (da), 
            "expose-event", 
            G_CALLBACK (on_expose_event), 
            NULL);

    gtk_widget_show_all (window);
    gtk_main ();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)


ntd*_*ntd 9

我只想为这个常见请求添加更多替代方案.

  1. libgoffice
    这是Gnumeric和AbiWord使用的库,因此它被积极维护并且相当稳定:目前可用的最佳替代方案之一.不幸的是,没有官方主页,它缺乏初学者文档.
  2. GtkDatabox
    它最近更改了维护者,因此将来会有一些不确定性.它曾经是一个很好的解决方案,用于在线图中渲染大量数据.
  3. GtkExtra2
    这是在GTK +中绘制图表的旧事实标准.跳到GTK + 2似乎对这个项目来说是致命的.
  4. GTK +仪器小工具和GLineGraph
    有点简单,但对简单的东西很好.

除此之外,很多项目都在内部实现某种GTK +图表.除了引用的Gnuplot之外,还有Gwyddion和gretl.而且我很确定我缺少其他人.

总之,在GTK +世界中没有普遍的共识或事实上的图表标准......

  • 我目前正在研究gnome-system-monitor如何绘制图形,对我来说,使用cairo实现它的方式似乎是一种很好的绘图方式,而且我还试图实时绘制.谢谢你的信息ntd,它全部采取了. (2认同)

hha*_*fez 2

看一下gnuplot,有一些与 gnuplot 交互的 C++库,它们可以完成您正在寻找的功能,然后还有一些。如果您曾经使用过 gnuplot,那么它会非常容易使用。

  • 面对5.5年前OP同样的问题,我想知道为什么这个答案被接受?它没有回答问题!至少,缺少一句话提示如何在 Gtk 小部件中显示绘图(使用 gnuplot 创建)。 (6认同)