1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// Plots a simple stripchart.
//
// Copyright (C) 2004-2014 Alan W. Irwin
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//
// ToDo: better way of clearing plot. search for `plvsta'.
//
#include "plplotP.h"
// Data declarations for stripcharts.
#define PEN 4
typedef struct
{
PLFLT xmin, xmax, ymin, ymax, xjump, xlen;
PLFLT wxmin, wxmax, wymin, wymax; // FIXME - some redundancy might exist
char *xspec, *yspec, *labx, *laby, *labtop;
PLINT y_ascl, acc, colbox, collab;
PLFLT xlpos, ylpos;
PLFLT *x[PEN], *y[PEN];
PLINT npts[PEN], nptsmax[PEN];
PLINT colline[PEN], styline[PEN];
char *legline[PEN];
} PLStrip;
static int sid; // strip id number
#define MAX_STRIPC 1000 // Max allowed
static PLStrip *strip[MAX_STRIPC]; // Array of pointers
static PLStrip *stripc; // current strip chart
// Generates a complete stripchart plot.
static void
plstrip_gen( PLStrip *strip );
// draw legend
static void
plstrip_legend( PLStrip *strip, int flag );
//--------------------------------------------------------------------------
// plstripc
//
// Create 1d stripchart.
//--------------------------------------------------------------------------
void
c_plstripc( PLINT *id, PLCHAR_VECTOR xspec, PLCHAR_VECTOR yspec,
PLFLT xmin, PLFLT xmax, PLFLT xjump, PLFLT ymin, PLFLT ymax,
PLFLT xlpos, PLFLT ylpos,
PLINT y_ascl, PLINT acc,
PLINT colbox, PLINT collab,
PLINT_VECTOR colline, PLINT_VECTOR styline, PLCHAR_MATRIX legline,
PLCHAR_VECTOR labx, PLCHAR_VECTOR laby, PLCHAR_VECTOR labtop )
{
int i;
// Get a free strip id and allocate it
for ( i = 0; i < MAX_STRIPC; i++ )
if ( strip[i] == NULL )
break;
if ( i == MAX_STRIPC )
{
plabort( "plstripc: Cannot create new strip chart" );
*id = -1;
return;
}
else
{
sid = *id = i;
strip[sid] = (PLStrip *) calloc( 1, (size_t) sizeof ( PLStrip ) );
if ( strip[sid] == NULL )
{
plabort( "plstripc: Out of memory." );
*id = -1;
return;
}
}
// Fill up the struct with all relevant info
stripc = strip[sid];
for ( i = 0; i < PEN; i++ )
{
stripc->npts[i] = 0;
stripc->nptsmax[i] = 100;
stripc->colline[i] = colline[i];
stripc->styline[i] = styline[i];
stripc->legline[i] = plstrdup( legline[i] );
stripc->x[i] = (PLFLT *) malloc( (size_t) sizeof ( PLFLT ) * (size_t) ( stripc->nptsmax[i] ) );
stripc->y[i] = (PLFLT *) malloc( (size_t) sizeof ( PLFLT ) * (size_t) ( stripc->nptsmax[i] ) );
if ( stripc->x[i] == NULL || stripc->y[i] == NULL )
{
plabort( "plstripc: Out of memory." );
plstripd( sid );
*id = -1;
return;
}
}
stripc->xlpos = xlpos; // legend position [0..1]
stripc->ylpos = ylpos;
stripc->xmin = xmin; // initial bounding box
stripc->xmax = xmax;
stripc->ymin = ymin;
stripc->ymax = ymax;
stripc->xjump = xjump; // jump x step(%) when x attains xmax (xmax is then set to xmax+xjump)
stripc->xlen = xmax - xmin; // length of x scale
stripc->y_ascl = y_ascl; // autoscale y between x jump scale
stripc->acc = acc; // accumulate plot (not really stripchart)
stripc->xspec = plstrdup( xspec ); // x axis specification
stripc->yspec = plstrdup( yspec );
stripc->labx = plstrdup( labx ); // x label
stripc->laby = plstrdup( laby );
stripc->labtop = plstrdup( labtop ); // title
stripc->colbox = colbox; // box color
stripc->collab = collab; // label color
// Generate the plot
plstrip_gen( stripc );
plstrip_legend( stripc, 1 );
}
static void plstrip_legend( PLStrip *stripcloc, int first )
{
int i;
PLFLT sc, dy;
// draw legend
plgchr( &sc, &dy );
sc = dy = dy / 100;
plwind( -0.01, 1.01, -0.01, 1.01 );
for ( i = 0; i < PEN; i++ )
{
if ( stripcloc->npts[i] || first )
{
plcol0( stripcloc->colline[i] ); pllsty( stripcloc->styline[i] );
pljoin( stripcloc->xlpos, stripcloc->ylpos - sc, stripcloc->xlpos + 0.1, stripcloc->ylpos - sc );
plcol0( stripcloc->collab );
plptex( stripcloc->xlpos + 0.11, stripcloc->ylpos - sc, 0., 0., 0, stripcloc->legline[i] ); sc += dy;
}
}
plwind( stripcloc->xmin, stripcloc->xmax, stripcloc->ymin, stripcloc->ymax );
plflush();
}
//--------------------------------------------------------------------------
// plstrip_gen
//
// Generates a complete stripchart plot. Used either initially or
// during rescaling.
//--------------------------------------------------------------------------
static void plstrip_gen( PLStrip *striploc )
{
int i;
// Set up window
plvpor( 0, 1, 0, 1 );
plwind( 0, 1, 0, 1 );
plcol0( 0 ); plpsty( 0 );
plclear();
plvsta();
// Draw box and same window dimensions
striploc->wxmin = striploc->xmin; striploc->wxmax = striploc->xmax;
striploc->wymin = striploc->ymin; striploc->wymax = striploc->ymax; // FIXME - can exist some redundancy here
plwind( striploc->xmin, striploc->xmax, striploc->ymin, striploc->ymax );
pllsty( 1 );
plcol0( striploc->colbox );
plbox( striploc->xspec, 0.0, 0, striploc->yspec, 0.0, 0 );
plcol0( striploc->collab );
pllab( striploc->labx, striploc->laby, striploc->labtop );
for ( i = 0; i < PEN; i++ )
{
if ( striploc->npts[i] > 0 )
{
plcol0( striploc->colline[i] ); pllsty( striploc->styline[i] );
plline( striploc->npts[i], striploc->x[i], striploc->y[i] );
}
}
plstrip_legend( striploc, 0 );
}
//--------------------------------------------------------------------------
// plstripa
//
// Add a point to a stripchart.
// Allocates memory and rescales as necessary.
//--------------------------------------------------------------------------
void c_plstripa( PLINT id, PLINT p, PLFLT x, PLFLT y )
{
int j, yasc = 0, istart;
if ( p >= PEN )
{
plabort( "Non existent pen" );
return;
}
if ( ( id < 0 ) || ( id >= MAX_STRIPC ) ||
( ( stripc = strip[id] ) == NULL ) )
{
plabort( "Non existent stripchart" );
return;
}
// Add new point, allocating memory if necessary
if ( ++stripc->npts[p] > stripc->nptsmax[p] )
{
stripc->nptsmax[p] += 32;
stripc->x[p] = (PLFLT *) realloc( (void *) stripc->x[p], sizeof ( PLFLT ) * (size_t) ( stripc->nptsmax[p] ) );
stripc->y[p] = (PLFLT *) realloc( (void *) stripc->y[p], sizeof ( PLFLT ) * (size_t) ( stripc->nptsmax[p] ) );
if ( stripc->x[p] == NULL || stripc->y[p] == NULL )
{
plabort( "plstripc: Out of memory." );
plstripd( id );
return;
}
}
stripc->x[p][stripc->npts[p] - 1] = x;
stripc->y[p][stripc->npts[p] - 1] = y;
stripc->xmax = x;
if ( stripc->y_ascl == 1 && ( y > stripc->ymax || y < stripc->ymin ) )
yasc = 1;
if ( y > stripc->ymax )
stripc->ymax = stripc->ymin + 1.1 * ( y - stripc->ymin );
if ( y < stripc->ymin )
stripc->ymin = stripc->ymax - 1.1 * ( stripc->ymax - y );
// Now either plot new point or regenerate plot
if ( stripc->xmax - stripc->xmin < stripc->xlen )
{
if ( yasc == 0 )
{
// If user has changed subwindow, make shure we have the correct one
plvsta();
plwind( stripc->wxmin, stripc->wxmax, stripc->wymin, stripc->wymax ); // FIXME - can exist some redundancy here
plcol0( stripc->colline[p] ); pllsty( stripc->styline[p] );
if ( ( stripc->npts[p] - 2 ) < 0 )
plP_movwor( stripc->x[p][stripc->npts[p] - 1], stripc->y[p][stripc->npts[p] - 1] );
else
plP_movwor( stripc->x[p][stripc->npts[p] - 2], stripc->y[p][stripc->npts[p] - 2] );
plP_drawor( stripc->x[p][stripc->npts[p] - 1], stripc->y[p][stripc->npts[p] - 1] );
plflush();
}
else
{
stripc->xmax = stripc->xmin + stripc->xlen;
plstrip_gen( stripc );
}
}
else
{
// Regenerating plot
if ( stripc->acc == 0 )
{
for ( j = 0; j < PEN; j++ )
{
if ( stripc->npts[j] > 0 )
{
istart = 0;
while ( stripc->x[j][istart] < stripc->xmin + stripc->xlen * stripc->xjump )
istart++;
stripc->npts[j] = stripc->npts[j] - istart;
memcpy( &stripc->x[j][0], &stripc->x[j][istart], (size_t) ( stripc->npts[j] ) * sizeof ( PLFLT ) );
memcpy( &stripc->y[j][0], &stripc->y[j][istart], (size_t) ( stripc->npts[j] ) * sizeof ( PLFLT ) );
}
}
}
else
stripc->xlen = stripc->xlen * ( 1 + stripc->xjump );
if ( stripc->acc == 0 )
stripc->xmin = stripc->xmin + stripc->xlen * stripc->xjump;
else
stripc->xmin = stripc->x[p][0];
stripc->xmax = stripc->xmax + stripc->xlen * stripc->xjump;
plstrip_gen( stripc );
}
}
//--------------------------------------------------------------------------
// plstripd
//
// Deletes and releases memory used by a stripchart.
//--------------------------------------------------------------------------
void c_plstripd( PLINT id )
{
int i;
if ( ( id < 0 ) || ( id >= MAX_STRIPC ) ||
( ( stripc = strip[id] ) == NULL ) )
{
plabort( "Non existent stripchart" );
return;
}
for ( i = 0; i < PEN; i++ )
{
if ( stripc->npts[i] )
{
free( (void *) stripc->x[i] );
free( (void *) stripc->y[i] );
free( stripc->legline[i] );
}
}
free( stripc->xspec );
free( stripc->yspec );
free( stripc->labx );
free( stripc->laby );
free( stripc->labtop );
free( (void *) stripc );
strip[id] = NULL;
}
|