基于GTK +的图表?

问题描述 投票:13回答:3

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

c++ gtk gtk2
3个回答
4
投票

看看gnuplot有一些与gnuplot接口的C ++ libraries,它可以做你正在寻找的东西,然后是一些。如果你曾经使用过gnuplot,它很容易使用。


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;
}

9
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.