"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/examples/logarithmic.c" (31 Mar 2021, 7046 Bytes) of package /linux/privat/gtkdatabox-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "logarithmic.c": 0.9.3.1_vs_1.0.0.

    1 /* $Id: logarithmic.c 4 2008-06-22 09:19:11Z rbock $ */
    2 /* GtkDatabox - An extension to the gtk+ library
    3  * Copyright (C) 1998 - 2008  Dr. Roland Bock
    4  *
    5  * This program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU Lesser General Public License
    7  * as published by the Free Software Foundation; either version 2.1
    8  * of the License, or (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU Lesser General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program; if not, write to the Free Software
   17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   18  */
   19 #include <stdio.h>
   20 
   21 #include <gtk/gtk.h>
   22 #include <gtkdatabox.h>
   23 #include <gtkdatabox_points.h>
   24 #include <gtkdatabox_ruler.h>
   25 #include <math.h>
   26 
   27 #define POINTS 100000
   28 /*
   29 #define STEPS 50
   30 #define BARS 25
   31 #define MARKER 10
   32 */
   33 #define NO_BOXES 4
   34 
   35 /*----------------------------------------------------------------
   36  *  databox logarithmic
   37  *----------------------------------------------------------------*/
   38 
   39 static void
   40 create_logarithmic (void)
   41 {
   42    GtkWidget *window = NULL;
   43    GtkWidget *hbox;
   44    GtkWidget *vbox;
   45    GtkWidget *close_button;
   46    GtkWidget *box[NO_BOXES];
   47    GtkWidget *label;
   48    GtkWidget *separator;
   49    GtkWidget *table;
   50    GtkDataboxGraph *graph;
   51    GdkCursor *cursor;
   52    gfloat min_x, max_x;
   53    gfloat min_y, max_y;
   54    gfloat *X;
   55    gfloat *Y;
   56    GdkRGBA color;
   57    gint i;
   58 
   59    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   60    gtk_widget_set_size_request (window, 1000, 500);
   61 
   62    g_signal_connect (G_OBJECT (window), "destroy",
   63              G_CALLBACK (gtk_main_quit), NULL);
   64 
   65    gtk_window_set_title (GTK_WINDOW (window),
   66              "GtkDatabox: Logarithmic Plots");
   67    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
   68 
   69    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
   70    gtk_container_add (GTK_CONTAINER (window), vbox);
   71 
   72    label = gtk_label_new
   73       ("The following types of curves are shown (with some adjustments to make them fit nicely into the window):\n Green: sin^2, Red: exp, Magenta: 1/x, Yellow: x^2\n(100K Points per each curve in each graph)\n");
   74    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
   75    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
   76    gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
   77 
   78    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   79    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
   80 
   81    label = gtk_label_new ("Linear Plot:");
   82    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   83 
   84    label = gtk_label_new ("Logarithmic Plot:");
   85    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   86 
   87    label = gtk_label_new ("Double Logarithmic Plot:");
   88    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   89 
   90    label = gtk_label_new ("Log base 2 Plot:");
   91    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   92 
   93    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
   94    gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
   95 
   96    /* Create the databoxes */
   97    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   98    gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
   99 
  100    for (i = 0; i < NO_BOXES; ++i)
  101    {
  102       gtk_databox_create_box_with_scrollbars_and_rulers (&box[i], &table,
  103                              TRUE, TRUE, TRUE,
  104                              TRUE);
  105       gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, TRUE, 0);
  106    }
  107 
  108    /* set the background */
  109    for (i = 0; i < NO_BOXES; ++i)
  110       gtk_databox_set_bg_color (GTK_DATABOX (box[i]), "#545454");
  111 
  112    /* add a sinus^2 */
  113    X = g_new0 (gfloat, POINTS);
  114    Y = g_new0 (gfloat, POINTS);
  115 
  116    for (i = 0; i < POINTS; i++)
  117    {
  118       X[i] = i / 100.;
  119       Y[i] = 0.01 + 1000 * pow (sin (i * G_PI / POINTS), 2);
  120    }
  121 
  122    color.red = 0;
  123    color.green = 1;
  124    color.blue = 0;
  125    color.alpha = 1;
  126 
  127    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  128    for (i = 0; i < NO_BOXES; ++i)
  129       gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  130 
  131    /* add an exp function */
  132    X = g_new0 (gfloat, POINTS);
  133    Y = g_new0 (gfloat, POINTS);
  134 
  135    for (i = 0; i < POINTS; i++)
  136    {
  137       X[i] = i / 100.;
  138       Y[i] = exp (log (1000) * i / ((gfloat) POINTS));
  139    }
  140 
  141    color.red = 1;
  142    color.green = 0;
  143    color.blue = 0;
  144    color.alpha = 1;
  145 
  146    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  147    for (i = 0; i < NO_BOXES; ++i)
  148       gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  149 
  150    /* add an 1/x function */
  151    X = g_new0 (gfloat, POINTS);
  152    Y = g_new0 (gfloat, POINTS);
  153 
  154    for (i = 0; i < POINTS; i++)
  155    {
  156       X[i] = i / 100.;
  157       Y[i] = 1000. / (100. * (gfloat) i / POINTS);
  158    }
  159 
  160    color.red = 1;
  161    color.green = 0;
  162    color.blue = 1;
  163    color.alpha = 1;
  164 
  165    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  166    for (i = 0; i < NO_BOXES; ++i)
  167       gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  168 
  169    /* add an x^2 function */
  170    X = g_new0 (gfloat, POINTS);
  171    Y = g_new0 (gfloat, POINTS);
  172 
  173    for (i = 0; i < POINTS; i++)
  174    {
  175       X[i] = i / 100.;
  176       Y[i] = 1000. * (gfloat) i / POINTS * (gfloat) i / POINTS;
  177    }
  178 
  179    color.red = 1;
  180    color.green = 1;
  181    color.blue = 0;
  182    color.alpha = 1;
  183 
  184    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  185    for (i = 0; i < NO_BOXES; ++i)
  186       gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  187 
  188    /* set the extrema */
  189    min_x = 0.01;
  190    max_x = 1000.;
  191    min_y = 1000.;
  192    max_y = 0.01;
  193 
  194    for (i = 0; i < NO_BOXES-1; ++i)
  195    {
  196       gtk_databox_set_total_limits (GTK_DATABOX (box[i]), min_x, max_x, min_y,
  197                     max_y);
  198    }
  199    gtk_databox_set_total_limits (GTK_DATABOX (box[i]), 8, max_x, min_y,
  200                     max_y);
  201 
  202    gtk_databox_set_scale_type_y (GTK_DATABOX (box[1]), GTK_DATABOX_SCALE_LOG);
  203    gtk_databox_set_scale_type_x (GTK_DATABOX (box[2]), GTK_DATABOX_SCALE_LOG);
  204    gtk_databox_set_scale_type_y (GTK_DATABOX (box[2]), GTK_DATABOX_SCALE_LOG);
  205    gtk_databox_set_scale_type_x (GTK_DATABOX (box[3]), GTK_DATABOX_SCALE_LOG2);
  206 
  207    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  208    gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);
  209 
  210    close_button = gtk_button_new_with_label ("close");
  211    g_signal_connect (G_OBJECT (close_button), "clicked",
  212              G_CALLBACK (gtk_main_quit), NULL);
  213    gtk_box_pack_start (GTK_BOX (vbox), close_button, FALSE, FALSE, 0);
  214    gtk_widget_set_can_default(close_button, TRUE);
  215    gtk_widget_grab_default (close_button);
  216    gtk_widget_grab_focus (close_button);
  217 
  218    gtk_widget_show_all (window);
  219    cursor = gdk_cursor_new_for_display (gdk_display_get_default (), GDK_CROSS);
  220    for (i = 0; i < NO_BOXES; ++i)
  221       gdk_window_set_cursor (gtk_widget_get_window(box[i]), cursor);
  222 }
  223 
  224 gint
  225 main (gint argc, char *argv[])
  226 {
  227    gtk_init (&argc, &argv);
  228 
  229    create_logarithmic ();
  230    gtk_main ();
  231 
  232    return 0;
  233 }