GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PlotTest2.C
Go to the documentation of this file.
1 /* Copyright 2000-2012 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #include "gtkInterface.H"
18 #include "Plot.H"
19 
20 // setup the possible plot colours ...
21 int ColourLineSpec::colourCnt=9; // this number matches with the number of colours below
22 const char *ColourLineSpec::shortColours[]={(char *)"y",(char *)"m",(char *)"c",(char *)"r",(char *)"g",(char *)"b",(char *)"w",(char *)"k",(char *)"o"}; // standard colours
23 const char *ColourLineSpec::X11Colours[]={(char *)"yellow", (char *)"magenta", (char *)"cyan", (char *)"red", (char *)"green", (char *)"blue", (char *)"white", (char *)"black", (char *)"orange"};
24 
25 // Quit button callback
26 static void quit(void *wid, gpointer data) {
27  gtk_main_quit();
28 }
29 
30 // some definitions
31 #define cnt 101
32 float x[cnt], y[cnt];
33 
34 #include <time.h>
35 #include <math.h>
36 
37 #define TYPE float
38 
39 // timeout function - shifts the sinusoid along and replots - shows how simple it is to plot dynamically changing data.
40 gboolean sinusoid(gpointer user_data){
41  // shift the sinusoid on.
42  int s=time(NULL);
43  for (int i=0;i<cnt;i++)
44  y[i]=100.0*sin((TYPE)i/((TYPE)cnt-1)*2*M_PI+s);
45 
46 
47  // replot the data
48  Plot **p = (Plot**) user_data;
49  p[0]->replot();
50  p[1]->replot();
51  return 1;
52 }
53 
54 
55 int main(int argc, char *argv[]) {
56 
57  gtk_init( &argc, &argv );
58 
59  GtkInterface topWindow;
60 
61  gtk_widget_set_size_request (topWindow.win, 1000, 500);
62  //hBox<< figure.getWidget();
63 
64  TYPE x1[cnt], y1[cnt]; // some more data for a second plot
65  for (int i=1;i<cnt;i++)
66  x[i]=y[i]=(TYPE)i;
67  for (int i=1;i<cnt;i++){
68  x1[i]=(TYPE)i;
69  y1[i]=(TYPE)i+100.0;
70  }
71 
72  /* ////////////////////////////////////////////////////////*/
73  // The actual plotting
74 
75  Plot figure; // create and show a Figure
76  figure.show();
77  figure.plot(x,y,cnt); // Plot the first line with default colours
78  figure.hold(true);
79  figure.plot(x1,y1,cnt, "r2");
80  figure.limits(1.0,101.0,1.0,201.0);
81  figure.set(figure.gca(), "XLabelFormat", "%%.1f", NULL);
82  figure.grid(true);
83  figure.title("Figure 1");
84  figure.xLabel("first plot x label");
85  figure.yLabel("just an example y");
86 
87  Plot figure2; // create a second figure for plotting a region
88  figure2.show();
89  figure2.plot(x,y,y1,cnt); // Plot the first line with default colours
90  figure2.limits(1.0,101.0,1.0,201.0);
91  figure2.grid(true);
92 
93  figure2.title("Figure 2");
94  figure2.xLabel("second plot x label");
95  /* /////////////////////////////////////////////////////////*/
96 
97  HBox hBox;
98  hBox << BoxIS(TRUE, TRUE)<<figure.getWidget() // show the figures
99  << BoxIS(TRUE, TRUE)<<figure2.getWidget();
100  hBox.show(); // show the horizontal box
101  topWindow<< hBox; // add it to the top window
102 
103  // setup a 1 second timer to change the plot
104  Plot *plots[2]; plots[0]=&figure; plots[1]=&figure2;
105  g_timeout_add_seconds(1, sinusoid, (gpointer)plots);
106 
107  gtk_main();
108 }