1 // Plots a simple stripchart with four pens.
2 //
3
4 #include "plcdemos.h"
5 #include <stdlib.h>
6 #ifdef PL_HAVE_NANOSLEEP
7 # include <time.h>
8 #endif
9 #ifdef PL_HAVE_UNISTD_H
10 # include <unistd.h>
11 #else
12 # ifdef PL_HAVE_POLL
13 # include <poll.h>
14 # endif
15 #endif
16
17
18
19 // Variables for holding error return info from PLplot
20
21 static PLINT pl_errcode;
22 static char errmsg[160];
23
24 //--------------------------------------------------------------------------
25 // main program
26 //--------------------------------------------------------------------------
27
28 int
main(int argc,char * argv[])29 main( int argc, char *argv[] )
30 {
31 PLINT id1, n, autoy, acc, nsteps = 1000;
32 PLFLT y1, y2, y3, y4, ymin, ymax, xlab, ylab;
33 PLFLT t, tmin, tmax, tjump, dt, noise;
34 PLINT colbox, collab, colline[4], styline[4];
35 PLCHAR_VECTOR legline[4];
36 #ifdef PL_HAVE_NANOSLEEP
37 struct timespec ts;
38 #endif
39
40 // plplot initialization
41 // Parse and process command line arguments
42
43 (void) plparseopts( &argc, argv, PL_PARSE_FULL );
44
45 // If db is used the plot is much more smooth. However, because of the
46 // async X behaviour, one does not have a real-time scripcharter.
47 // This is now disabled since it does not significantly improve the
48 // performance on new machines and makes it difficult to use this
49 // example non-interactively since it requires an extra pleop call after
50 // each call to plstripa.
51 //
52 //plsetopt("db", "");
53 //plsetopt("np", "");
54
55 // User sets up plot completely except for window and data
56 // Eventually settings in place when strip chart is created will be
57 // remembered so that multiple strip charts can be used simultaneously.
58 //
59
60 // Specify some reasonable defaults for ymin and ymax
61 // The plot will grow automatically if needed (but not shrink)
62
63 ymin = -0.1;
64 ymax = 0.1;
65
66 // Specify initial tmin and tmax -- this determines length of window.
67 // Also specify maximum jump in t
68 // This can accomodate adaptive timesteps
69
70 tmin = 0.;
71 tmax = 10.;
72 tjump = 0.3; // percentage of plot to jump
73
74 // Axes options same as plbox.
75 // Only automatic tick generation and label placement allowed
76 // Eventually I'll make this fancier
77
78 colbox = 1;
79 collab = 3;
80 styline[0] = colline[0] = 2; // pens color and line style
81 styline[1] = colline[1] = 3;
82 styline[2] = colline[2] = 4;
83 styline[3] = colline[3] = 5;
84
85 legline[0] = "sum"; // pens legend
86 legline[1] = "sin";
87 legline[2] = "sin*noi";
88 legline[3] = "sin+noi";
89
90 xlab = 0.; ylab = 0.25; // legend position
91
92 autoy = 1; // autoscale y
93 acc = 1; // don't scrip, accumulate
94
95 // Initialize plplot
96
97 plinit();
98
99 pladv( 0 );
100 plvsta();
101
102 // Register our error variables with PLplot
103 // From here on, we're handling all errors here
104
105 plsError( &pl_errcode, errmsg );
106
107 plstripc( &id1, "bcnst", "bcnstv",
108 tmin, tmax, tjump, ymin, ymax,
109 xlab, ylab,
110 autoy, acc,
111 colbox, collab,
112 colline, styline, legline,
113 "t", "", "Strip chart demo" );
114
115 if ( pl_errcode )
116 {
117 fprintf( stderr, "%s\n", errmsg );
118 exit( 1 );
119 }
120
121 // Let plplot handle errors from here on
122
123 plsError( NULL, NULL );
124
125 autoy = 0; // autoscale y
126 acc = 1; // accumulate
127
128 // This is to represent a loop over time
129 // Let's try a random walk process
130
131 y1 = y2 = y3 = y4 = 0.0;
132 dt = 0.1;
133
134 #ifdef PL_HAVE_NANOSLEEP
135 ts.tv_sec = 0;
136 ts.tv_nsec = 10000000;
137 #endif
138 for ( n = 0; n < nsteps; n++ )
139 {
140 #ifdef PL_HAVE_NANOSLEEP
141 nanosleep( &ts, NULL ); // wait a little (10 ms) to simulate time elapsing
142 #else
143 # ifdef PL_HAVE_POLL
144 poll( 0, 0, 10 );
145 # else
146 { int i; for ( i = 0; i < 1000000; i++ )
147 ;
148 }
149 # endif
150 #endif
151 t = (PLFLT) n * dt;
152 noise = plrandd() - 0.5;
153 y1 = y1 + noise;
154 y2 = sin( t * M_PI / 18. );
155 y3 = y2 * noise;
156 y4 = y2 + noise / 3.;
157
158 // There is no need for all pens to have the same number of
159 // points or beeing equally time spaced.
160
161 if ( n % 2 )
162 plstripa( id1, 0, t, y1 );
163 if ( n % 3 )
164 plstripa( id1, 1, t, y2 );
165 if ( n % 4 )
166 plstripa( id1, 2, t, y3 );
167 if ( n % 5 )
168 plstripa( id1, 3, t, y4 );
169
170 // needed if using double buffering (-db on command line)
171 //pleop();
172 }
173
174 // Destroy strip chart and it's memory
175
176 plstripd( id1 );
177 plend();
178 exit( 0 );
179 }
180
181