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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/*
* barchart.c - procedures to build and maintain a barchart
* data structure
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "CNplot.h"
/*
* BARCHART DATA STRUCTURE
* A barchart is used to store data for a bar chart.
* The data consists of points (x only) collected in bins and bars.
* bins then plotted.
*/
/*
* Bar Chart data has the following format:
* "Bar_A" "Bar_B" "Bar_C"
* "value1" xa1 xb1 xc1
* "value2" xa2 xb2 xc2
* "value3" ...
*
* The resulting chart is obtained:
*
* | C
* | A |=| C
* | |=|B| | A B C B|=|
* | | |=| | |=|=|=| A|=| |
* | | | | | | | | | |=| | |
* | | | | | | | | | | | | |
* ----------|---------|---------|
* value1 value2 value3
*
* Each of the intervals "value1", "value2" etc are defined as bins,
* and each bin contains 1 or more bars.
*
* Thus the data-structure contains a linked list of bins
* Each bin has a name (e.g. "value1" and a linked list of bars
* Each bar in turn contains a point and a pointer to a name (e.g. "A")
*/
static int CNcount_bars();
static int CNcount_bins();
/*
* Given a barchart dataset, sort the data and create curves
*/
void CNsort_barchart(dptr, verbose)
CNdatasetptr dptr;
int verbose;
{
CNbarptr Barptr;
CNbinptr Binptr;
CNcurveptr Cptr;
int nbars, bar_count;
int nbins, bin_count;
double bar_width, bar_offset;
double bin_width;
double xmin, xmax;
double x1, y1, x2, y2;
int curveID=0;
/* Error-check */
if (dptr == NULL) {
(void) fprintf(stderr,"CNsort_barchart: Error! NULL dataset!\n");
return;
}
if (dptr->datatype != CN_BARCHART) {
(void) fprintf(stderr,
"CNsort_barchart: Error! Need a BARCHART dataset!\n");
return;
}
if (dptr->barchart == NULL) {
(void) fprintf(stderr,
"CNsort_barchart: Error! NULL BARCHART structure!\n");
return;
}
if (dptr->barchart->binhead == NULL) {
(void) fprintf(stderr,
"CNsort_barchart: Error! No bars in BARCHART!\n");
return;
}
/* Get rid of the curvelist in the dataset first */
CNdelete_curve_list(&(dptr->curvehead), &(dptr->curvetail));
/* Initialize */
nbars = CNcount_bars(dptr->barchart->binhead->barhead,
dptr->barchart->binhead->bartail);
nbins = CNcount_bins(dptr->barchart->binhead,
dptr->barchart->bintail);
bar_offset = 0.2;
bin_width = 1.0;
bar_width = (bin_width - 2.0*bar_offset)/(double)nbars;
/*
bar_width = 0.6;
bin_width = nbars*bar_width + 2.0*bar_offset;
*/
xmin = 0.0;
xmax = nbins*bin_width;
/* Create the various bars */
bin_count = 0;
for (Binptr=dptr->barchart->binhead; Binptr!=NULL; Binptr=Binptr->next) {
/* Set the min/max */
Binptr->xmin = xmin + (bin_count )*bin_width;
Binptr->xmax = xmin + (bin_count+1)*bin_width;
bin_count++;
bar_count = 0;
for (Barptr=Binptr->barhead; Barptr!=NULL; Barptr=Barptr->next) {
x1 = Binptr->xmin + (bar_count )*bar_width + bar_offset;
x2 = Binptr->xmin + (bar_count+1)*bar_width + bar_offset;
bar_count++;
y1 = dptr->data_pr.barmin; /* Defaults to 0.0 */
y2 = Barptr->value;
/* Allocate a curve data-structure */
Cptr = CNinsert_curve(&(dptr->curvehead), &(dptr->curvetail),
curveID++);
if (Cptr!=NULL) {
(void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
x1,y1,0.0,0);
(void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
x1,y2,0.0,0);
(void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
x2,y2,0.0,0);
(void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
x2,y1,0.0,0);
(void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
x1,y1,0.0,0);
/* Set the properties */
Cptr->curv_pr.filltype = Barptr->filltype;
Cptr->curv_pr.flag = Cptr->curv_pr.flag & CNfilltype;
Cptr->curv_pr.fillcolor= Barptr->fillcolor;
Cptr->curv_pr.flag = Cptr->curv_pr.flag & CNfillcolor;
if (Binptr==dptr->barchart->binhead && Barptr->name) {
(void) CNparse_curve_property(&(Cptr->curv_pr),
"linelabel",Barptr->name,0);
}
}
}
}
/* Reset the dataset boundaries */
dptr->bxmin = xmin;
dptr->bxmax = xmax;
dptr->plot_pr.vxmin = xmin;
dptr->plot_pr.vxmax = xmax;
/* Print info */
if (verbose) {
(void) fprintf(stdout, "Generated %d new curves\n",
CNcount_curves(dptr->curvehead, dptr->curvetail));
}
}
/*
* BAR DATA_STRUCT ALLOCATION
*/
/*
* make a bar
*/
CNbarptr CNmake_bar(name, value)
char *name;
double value;
{
CNbarptr newptr;
unsigned int size = sizeof(CNbar);
if ((newptr = (CNbarptr)malloc(size))!=NULL) {
newptr->name = CNcreate_string(name);
newptr->value = value;
newptr->filltype = CN_FILL_SOLID;
newptr->fillcolor = 2;
newptr->next = NULL;
newptr->prev = NULL;
}
return(newptr);
}
/*
* Insert a bar at the tail of the current bar list
*/
CNbarptr CNinsert_bar(bar_listhead,bar_listtail,name,value)
CNbarptr *bar_listhead, *bar_listtail;
char *name;
double value;
{
CNbarptr next,A,B;
A = *bar_listtail;
if ((B=CNmake_bar(name,value))!=NULL) {
if (A==NULL) {
*bar_listhead = B;
*bar_listtail = B;
} else {
next = A->next;
B->next = next;
B->prev = A;
A->next = B;
if (next != NULL) next->prev = B;
if (B->next == NULL) *bar_listtail = B;
}
}
return(B);
}
/*
* Store the linked list
*/
void CNstore_bar(bar_listhead, bar_listtail, Cptr)
CNbarptr *bar_listhead, *bar_listtail, Cptr;
{
CNbarptr A, B, next;
A = *bar_listtail;
if ((B=Cptr)!=NULL) {
if (A==NULL) {
*bar_listhead = B;
*bar_listtail = B;
} else {
next = A->next;
B->next = next;
B->prev = A;
A->next = B;
if (next != NULL) next->prev = B;
if (B->next == NULL) *bar_listtail = B;
}
}
}
/*
* Delete bar
*/
void CNdelete_bar(bar_listhead, bar_listtail, B)
CNbarptr *bar_listhead, *bar_listtail;
CNbarptr B;
{
CNbarptr prev,next;
/* Destroy the name string */
if (B->name) CNdestroy_string(B->name);
prev = B->prev;
next = B->next;
if (prev!=NULL) prev->next = next;
if (next!=NULL) next->prev = prev;
if (B==*bar_listhead) *bar_listhead = next;
if (B==*bar_listtail) *bar_listtail = prev;
/* Now delete B */
free ((char*)B);
}
/*
* Delete all the bars in the list
*/
void CNdelete_bar_list(bar_listhead, bar_listtail)
CNbarptr *bar_listhead, *bar_listtail;
{
CNbarptr B;
while ((B = *bar_listhead) != NULL)
CNdelete_bar(bar_listhead, bar_listtail, B);
}
/*
* Count the number of bars in the list
*/
/*ARGSUSED*/
static int CNcount_bars(bar_listhead, bar_listtail)
CNbarptr bar_listhead, bar_listtail;
{
CNbarptr B;
int count = 0;
for (B=bar_listhead; B!=NULL; B=B->next) count++;
return(count);
}
/*
* BIN DATA_STRUCT ALLOCATION
*/
/*
* make a bin
*/
CNbinptr CNmake_bin(name)
char *name;
{
CNbinptr newptr;
unsigned int size = sizeof(CNbin);
if ((newptr = (CNbinptr)malloc(size))!=NULL) {
newptr->name = CNcreate_string(name);
newptr->xmin = 0.0;
newptr->xmax = 0.0;
newptr->barhead = NULL;
newptr->bartail = NULL;
newptr->next = NULL;
newptr->prev = NULL;
}
return(newptr);
}
/*
* Insert a bin at the tail of the current bin list
*/
CNbinptr CNinsert_bin(bin_listhead,bin_listtail,name)
CNbinptr *bin_listhead, *bin_listtail;
char *name;
{
CNbinptr next,A,B;
A = *bin_listtail;
if ((B=CNmake_bin(name))!=NULL) {
if (A==NULL) {
*bin_listhead = B;
*bin_listtail = B;
} else {
next = A->next;
B->next = next;
B->prev = A;
A->next = B;
if (next != NULL) next->prev = B;
if (B->next == NULL) *bin_listtail = B;
}
}
return(B);
}
/*
* Store the linked list
*/
void CNstore_bin(bin_listhead, bin_listtail, Cptr)
CNbinptr *bin_listhead, *bin_listtail, Cptr;
{
CNbinptr A, B, next;
A = *bin_listtail;
if ((B=Cptr)!=NULL) {
if (A==NULL) {
*bin_listhead = B;
*bin_listtail = B;
} else {
next = A->next;
B->next = next;
B->prev = A;
A->next = B;
if (next != NULL) next->prev = B;
if (B->next == NULL) *bin_listtail = B;
}
}
}
/*
* Delete bin
*/
void CNdelete_bin(bin_listhead, bin_listtail, B)
CNbinptr *bin_listhead, *bin_listtail;
CNbinptr B;
{
CNbinptr prev,next;
/* Destroy the name string */
if (B->name) CNdestroy_string(B->name);
/* Delete all the bars in the bin */
CNdelete_bar_list(&(B->barhead), &(B->bartail));
prev = B->prev;
next = B->next;
if (prev!=NULL) prev->next = next;
if (next!=NULL) next->prev = prev;
if (B==*bin_listhead) *bin_listhead = next;
if (B==*bin_listtail) *bin_listtail = prev;
/* Now delete B */
free ((char*)B);
}
/*
* Delete all the bins in the list
*/
void CNdelete_bin_list(bin_listhead, bin_listtail)
CNbinptr *bin_listhead, *bin_listtail;
{
CNbinptr B;
while ((B = *bin_listhead) != NULL)
CNdelete_bin(bin_listhead, bin_listtail, B);
}
/*
* Count the number of bins in the list
*/
/*ARGSUSED*/
static int CNcount_bins(bin_listhead, bin_listtail)
CNbinptr bin_listhead, bin_listtail;
{
CNbinptr B;
int count = 0;
for (B=bin_listhead; B!=NULL; B=B->next) count++;
return(count);
}
/*
* BAR-CHART DATA_STRUCT ALLOCATION
*/
/*
* make a barchart
*/
CNbarchartptr CNmake_barchart(binhead, bintail)
CNbinptr binhead, bintail;
{
CNbarchartptr newptr;
unsigned int size = sizeof(CNbarchart);
if ((newptr = (CNbarchartptr)malloc(size))!=NULL) {
newptr->binhead = binhead;
newptr->bintail = bintail;
}
return(newptr);
}
/*
* Delete barchart
*/
void CNdelete_barchart(B)
CNbarchartptr B;
{
/* Delete all the bins in the barchart */
CNdelete_bin_list(&(B->binhead), &(B->bintail));
/* Now delete B */
free ((char*)B);
}
|