1 /*$Id: plot.cc,v 1.2 2010-09-07 07:46:24 felix Exp $
2 * (this file is a mess. it should be redone.)
4 //testing=script 2006.07.17
5 #include "declare.h" /* self */
9 /*--------------------------------------------------------------------------*/
10 void plottr(double,const PROBELIST
&);
11 int plopen(double,double,const PROBELIST
&);
14 static void plborder(void);
15 static void calibrate(const PROBE
&);
16 static int round_to_int(double);
17 static void plhead(const PROBELIST
&);
18 static int point(double,double,double,int,int,int);
19 static void plotarg(double,double,double,double,double,double,
20 double,double,double);
21 /*--------------------------------------------------------------------------*/
23 #define OUTWIDTH (std::min(static_cast<int>(OPT::outwidth), MAXWIDTH))
24 #define INDENT 8 /* beware OMSTERAM::form! */
25 #define CONSSCALE (OUTWIDTH - INDENT - 2) /*console scale size in chr */
26 static bool active
; /* flag: plotting has opened */
27 static double xstart
, xstop
;
28 static char border
[MAXWIDTH
+1]; /* border string (keep, repeat at end) */
29 static char emptydata
[MAXWIDTH
+1]; /* empty data, to copy then insert data */
30 /*--------------------------------------------------------------------------*/
31 void plottr(double xx
, const PROBELIST
& plotlist
) /* plot a data point, */
39 for (PROBELIST::const_iterator
43 val
[ii
] = (*i
)->value();
44 if ((*i
)->range() != 0.) {
59 plotarg(xx
, val
[0], val
[1],
64 /*--------------------------------------------------------------------------*/
65 /* plopen: begin the plot. any type
67 int plopen(double start
, double stop
, const PROBELIST
& plotlist
)
70 IO::plotout
= OMSTREAM();
72 if (!IO::plotout
.any()) {
79 assert(active
== false);
83 /*--------------------------------------------------------------------------*/
84 /* plclose: finish up the plot (any type)
93 IO::plotout
= OMSTREAM();
95 /*--------------------------------------------------------------------------*/
96 /* plclear: clear graphics mode
105 /*--------------------------------------------------------------------------*/
106 /* plborder: draw the border -- Ascii graphics
108 static void plborder(void)
110 IO::plotout
.tab(INDENT
) << border
<< '\n';
112 /*--------------------------------------------------------------------------*/
113 /* calibrate: calibrate the y axis. ascii plot.
115 static void calibrate(const PROBE
& prb
)
117 static char nums
[20]; /* this label string */
118 static char highs
[20]; /* the last label string */
119 int cal
; /* char position within line */
120 int stop
; /* location of last label, stop printing */
121 int filled
; /* how far (characters) have been printed */
122 int numsize
; /* number of characters in this label */
123 int start
; /* starting position of this label */
124 double markno
; /* loop counter */
127 if (prb
.range() == 0) {
134 double range
= hi
- lo
;
136 strcpy(highs
, ftos(hi
, 0, 5, IO::formaat
));
137 highs
[8] = '\0'; /* trim to 8 chrs */
138 /* *strchr(&highs[2],' ') = '\0'; */ /* make the top label, and save */
139 stop
= OUTWIDTH
- static_cast<int>(strlen(highs
)) - 1; /* space for it. */
141 IO::plotout
<< prb
.label();
144 for (markno
= 0.; markno
< OPT::ydivisions
; markno
++) {
145 double number
= lo
+ range
* markno
/OPT::ydivisions
;
146 if (std::abs(number
) < std::abs(range
)/(10.*CONSSCALE
)) {
148 } /* label to put on this div. */
149 strcpy(nums
, ftos(number
, 0, 5, IO::formaat
));
150 nums
[8] = '\0'; /* trim to 8 chrs */
151 numsize
= static_cast<int>(strlen(nums
)); /* center it over the mark */
152 cal
= round_to_int(INDENT
+ CONSSCALE
* (markno
/OPT::ydivisions
));
153 start
= cal
- (numsize
+1)/2;
154 if (start
> filled
&& start
+numsize
< stop
) {
155 IO::plotout
.tab(start
) << nums
; /* if it fits, print it */
156 filled
= start
+ numsize
;
160 IO::plotout
.tab(stop
) << highs
<< '\n'; /* print the last calibration */
162 /*--------------------------------------------------------------------------*/
163 static int round_to_int(double x
)
165 return static_cast<int>(floor(x
+.5));
167 /*--------------------------------------------------------------------------*/
168 /* plhead: begin ascii graphics
169 * print opening border, calibrations, etc.
171 static void plhead(const PROBELIST
& plotlist
)
173 for (PROBELIST::const_iterator
174 i
= plotlist
.begin();
179 for (int ii
= 0; ii
< CONSSCALE
; ii
++) { /* build strings */
183 double incr
= static_cast<double>(CONSSCALE
) / OPT::ydivisions
;
186 place
< static_cast<double>(CONSSCALE
);
188 border
[round_to_int(place
)] = '+';
189 emptydata
[round_to_int(place
)] = '.'; /* tics in emptydata */
191 border
[CONSSCALE
] = '+'; /* fix ends of the strings */
192 border
[CONSSCALE
+1] = '\0';
193 emptydata
[CONSSCALE
] = emptydata
[0] = '|';
194 emptydata
[CONSSCALE
+1] = '\0';
196 plborder(); /* print the border */
198 /*--------------------------------------------------------------------------*/
199 /* point: return coordinate to plot in pixel #
202 double yy
, /* raw data */
204 double hi
, /* limits: both ends of the plot */
205 int scale
, /* length of scale in pixels */
206 int offset
, /* pixel offset of start of plot area */
207 int linswp
) /* flag: linear scale (else log scale) */
212 place
= round_to_int( scale
*(yy
-lo
)/(hi
-lo
));
214 place
= round_to_int( scale
*(log(yy
/lo
))/(log(hi
/lo
)));
220 if (place
> scale
) {itested();
223 return place
+ offset
;
225 /*--------------------------------------------------------------------------*/
226 /* plotarg: plot all 2 selected probes at one time, freq, etc. point.
230 double xx
, /* values */
233 double , /* lower limits */
236 double , /* upper limits */
240 char adata
[MAXWIDTH
+1]; /* actual data. copy emptydata, insert */
241 char *xxs
; /* string representation of xx */
242 memcpy(adata
, emptydata
, MAXWIDTH
); /* copy prototype */
243 xxs
= ftos( xx
, 11, 5, IO::formaat
);
244 if (zz
!= NOT_VALID
) {
245 adata
[point(zz
,zlo
,zhi
,CONSSCALE
,0,1)] = '+';/* zap data into string */
247 adata
[point(yy
,ylo
,yhi
,CONSSCALE
,0,1)] = '*';
248 IO::plotout
.form( "%-8.8s%s", xxs
, adata
);
251 /*--------------------------------------------------------------------------*/
252 /*--------------------------------------------------------------------------*/
253 // vim:ts=8:sw=2:noet: