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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file chart.c
*
* This file is part of the XForms library package.
* Copyright (c) 1996-2002 T.C. Zhao and Mark Overmars
* All rights reserved.
*
* All chart objects. Absolutely needs prototypes for all
* drawing functions.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "include/forms.h"
#include "flinternal.h"
#ifndef M_PI
#define M_PI 3.14159265359
#endif
#define ARCINC ( M_PI / 1800 )
#define MAX_CHART_LABEL_LEN 16
/* Object specific information */
typedef struct
{
float val; /* Value of the entry */
FL_COLOR col; /* Color of the entry */
FL_COLOR lcol; /* Label color of the entry */
char str[ MAX_CHART_LABEL_LEN ]; /* Label of the entry */
} ENTRY;
typedef struct
{
float min, /* the boundaries */
max;
int numb; /* number of entries */
int maxnumb; /* maximal number of entries to display */
int autosize; /* whether the x-axis should be scaled */
int lstyle, /* item label font style & size */
lsize;
int x, /* drawing area */
y,
w,
h;
FL_COLOR lcol; /* default label color */
ENTRY * entries; /* the entries */
int no_baseline;
} FLI_CHART_SPEC;
/***************************************
* Draws a bar chart. x,y,w,h is the bounding box, entries the array of
* numb entries and min and max the boundaries.
***************************************/
static void
draw_barchart( FL_OBJECT * ob,
float min,
float max )
{
FLI_CHART_SPEC *sp = ob->spec;
int x = sp->x,
y = sp->y,
w = sp->w,
h = sp->h;
int numb = sp->numb;
int i,
j,
n;
float bwidth; /* Width of a bar */
FL_Coord zeroh; /* Height of zero value */
FL_Coord val,
xx,
dx;
float incr, /* Increment per unit value */
xfuzzy;
float lh = fl_get_char_height( sp->lstyle, sp->lsize, &i, &j );
ENTRY *e,
*es,
*entries = sp->entries;
int lbox;
float fx;
incr = h / ( max - min );
zeroh = y + h + min * incr;
if ( -min * incr < lh )
{
incr = ( h - lh ) / max;
zeroh = y + h - lh;
}
bwidth = ( double ) w / ( sp->autosize ? numb : sp->maxnumb );
/* base line */
if( ! sp->no_baseline )
fl_line( x, zeroh + 0.5, x + w, zeroh + 0.5, ob->col2 );
if ( min == 0.0 && max == 0.0 )
return; /* Nothing else to draw */
/* Draw the bars */
n = 1;
if ( ( xfuzzy = bwidth - ( FL_Coord ) bwidth ) != 0.0 )
n = 1.0 / xfuzzy + 2;
for ( e = entries, xx = x, i = 0, es = e + numb; e < es; e++, i++ )
{
dx = bwidth + ( i % n ) * xfuzzy;
if ( e->val != 0.0 )
{
val = e->val * incr;
fl_rectbound( xx, zeroh - val, dx, val, e->col );
}
xx += dx;
}
/* Draw the labels */
lbox = 0.8 * bwidth;
for ( e = entries, es = e + numb, fx = x; e < es; fx += bwidth, e++ )
fl_drw_text_beside( FL_ALIGN_BOTTOM, fx + 0.5 * ( bwidth - lbox ),
zeroh - lbox, lbox, lbox, e->lcol,
sp->lstyle, sp->lsize, e->str );
}
/***************************************
* Draws a horizontal bar chart. x,y,w,h is the bounding box, entries the
* array of numb entries and min and max the boundaries.
***************************************/
static void
draw_horbarchart( FL_OBJECT * ob,
float min,
float max )
{
FLI_CHART_SPEC *sp = ob->spec;
int x = sp->x,
y = sp->y,
w = sp->w,
h = sp->h;
int numb = sp->numb;
float bwidth; /* Width of a bar */
float incr; /* Increment per unit value */
FL_Coord lw, /* Label width & Position of zero value */
zeroh,
dy,
yy;
int i,
l,
n;
float yfuzzy;
char *s;
ENTRY *e,
*entries = sp->entries;
int lbox;
/* Compute maximal label width */
for ( lw = 0, i = 0; i < numb; i++ )
{
s = entries[ i ].str;
l = fl_get_string_width( sp->lstyle, sp->lsize, s, strlen( s ) );
if ( l > lw )
lw = l;
}
if ( lw > 0.0 )
lw = lw + 4.0;
incr = ( float ) w / ( max - min );
zeroh = x - min * incr + 0.1;
if ( -min * incr < lw )
{
zeroh = x + lw;
incr = ( w - lw ) / max;
}
bwidth = ( float ) h / ( sp->autosize ? numb : sp->maxnumb );
/* Draw base line */
if ( ! sp->no_baseline )
fl_line( zeroh + 0.5, y, zeroh + 0.5, y + h, ob->col2 );
if ( min == 0.0 && max == 0.0 )
return; /* Nothing else to draw */
/* Draw the bars. Need to take care of round-off errors */
yy = y;
dy = bwidth;
n = 2;
if ( ( yfuzzy = bwidth - dy ) != 0 )
n = 1.0 / yfuzzy + 2;
for ( e = entries + numb - 1, i = 0; i < numb; i++, e-- )
{
dy = bwidth + ( i % n ) * yfuzzy;
if ( e->val != 0.0 )
fl_rectbound( zeroh, yy, e->val * incr, dy, e->col );
yy += dy;
}
/* Draw the labels */
lbox = 0.8 * bwidth;
for ( e = entries + numb - 1, i = 0; i < numb; i++, e-- )
fl_drw_text_beside( FL_ALIGN_LEFT, zeroh,
y + i * bwidth + 0.5 * ( bwidth - lbox ),
lbox, lbox, e->lcol, sp->lstyle,
sp->lsize, e->str );
}
/***************************************
* Draws a line chart
***************************************/
static void
draw_linechart( FL_OBJECT * ob,
float min,
float max )
{
FLI_CHART_SPEC *sp = ob->spec;
int type = ob->type;
int x = sp->x,
y = sp->y,
w = sp->w,
h = sp->h;
int i,
numb = sp->numb;
float ttt;
float bwidth; /* distance between points */
float zeroh; /* Height of zero value */
float incr; /* Increment per unit value */
float lh = fl_get_char_height( sp->lstyle, sp->lsize, 0, 0 );
ENTRY *e,
*es,
*entries = sp->entries;
float xx, /* tmp vars */
val1,
val2,
val3;
int lbox;
incr = ( h - 2 * lh ) / ( max - min );
zeroh = ( y + h ) - ( lh - min * incr );
bwidth = ( float ) w / ( sp->autosize ? numb : sp->maxnumb );
/* Draw the values */
for ( i = 0; i < numb; i++ )
{
val3 = entries[ i ].val * incr;
if ( type == FL_SPIKE_CHART )
{
val1 = ( i + 0.5 ) * bwidth;
fli_reset_vertex( );
fl_color( entries[ i ].col );
fli_add_float_vertex( x + val1, zeroh );
fli_add_float_vertex( x + val1, zeroh - val3 );
fli_endline( );
}
else if ( type == FL_LINE_CHART && i != 0 )
{
e = entries + i - 1;
fli_reset_vertex( );
fl_color( e->col );
fli_add_float_vertex( x + ( i - 0.5 ) * bwidth,
zeroh - e->val * incr );
fli_add_float_vertex( x + ( i + 0.5 ) * bwidth, zeroh - val3 );
fli_endline( );
}
else if ( type == FL_FILLED_CHART && i != 0 )
{
e = entries + i - 1;
val1 = ( i - 0.5 ) * bwidth;
val2 = ( i + 0.5 ) * bwidth;
fli_reset_vertex( );
fl_color( e->col );
fli_add_float_vertex( x + val1, zeroh );
fli_add_float_vertex( x + val1, zeroh - e->val * incr );
if ( ( e->val > 0.0 && entries[ i ].val < 0.0 )
|| ( e->val < 0.0 && entries[ i ].val > 0.0 ) )
{
ttt = e->val / ( e->val - entries[ i ].val );
fli_add_float_vertex( x + ( i - 0.5 + ttt ) * bwidth, zeroh );
fli_add_float_vertex( x + ( i - 0.5 + ttt ) * bwidth, zeroh );
}
fli_add_float_vertex( x + val2, zeroh - val3 );
fli_add_float_vertex( x + val2, zeroh );
fli_endpolygon( );
fli_reset_vertex( );
fl_color( FL_BLACK );
fli_add_float_vertex( x + val1, zeroh - e->val * incr );
fli_add_float_vertex( x + val2, zeroh - val3 );
fli_endline( );
}
}
/* Draw base line */
if ( ! sp->no_baseline )
fl_line( x, zeroh + 0.5, x + w, zeroh + 0.5, ob->col2 );
/* Draw the labels */
lbox = 0.8 * bwidth;
xx = x + 0.5 * ( bwidth - lbox );
for ( e = entries, es = e + numb; e < es; e++, xx += bwidth )
{
if ( e->val < 0.0 )
fl_drw_text_beside( FL_ALIGN_TOP, xx, zeroh - e->val * incr + 12,
lbox, lbox, e->lcol, sp->lstyle,
sp->lsize, e->str );
else
fl_drw_text_beside( FL_ALIGN_BOTTOM, xx,
zeroh - e->val * incr - 12 - lbox,
lbox, lbox, e->lcol, sp->lstyle,
sp->lsize, e->str );
}
}
/***************************************
* Draws a pie chart. x,y,w,h is the bounding box, entries the array of
* numb entries
***************************************/
static void
draw_piechart( FL_OBJECT * ob,
int special )
{
FLI_CHART_SPEC *sp = ob->spec;
int x = sp->x,
y = sp->y,
w = sp->w,
h = sp->h;
int i, numb = sp->numb;
float xc, /* center and radius */
yc,
rad;
float tot; /* sum of values */
float incr; /* increment in angle */
float curang; /* current angle we are drawing */
float xl, /* label position */
yl;
float txc, /* temporary center */
tyc;
float lh = fl_get_char_height( sp->lstyle, sp->lsize, 0, 0 );
int lbox;
ENTRY *e,
*entries = sp->entries;
/* compute center and radius */
xc = x + w / 2;
yc = y + h / 2;
rad = h / 2 - lh;
if ( special )
{
yc += 0.1 * rad;
rad = 0.9 * rad;
}
/* compute sum of values */
for ( tot = 0.0f, i = 0; i < numb; i++ )
if ( entries[ i ].val > 0.0 )
tot += entries[ i ].val;
if ( tot == 0.0 )
return;
incr = 3600.0 / tot;
/* Draw the pie */
curang = 0.0;
for ( e = entries, i = 0; i < numb; i++, e++ )
if ( e->val > 0.0 )
{
float tt = incr * e->val;
txc = xc;
tyc = yc;
/* Correct for special pies */
if ( special && i == 0 )
{
txc += 0.2 * rad * cos( ARCINC * ( curang + tt * 0.5 ) );
tyc -= 0.2 * rad * sin( ARCINC * ( curang + tt * 0.5 ) );
}
tt += curang;
fl_arcf( txc, tyc, rad, curang, tt, e->col );
fl_arc( txc, tyc, rad, curang, tt, FL_BLACK );
fli_reset_vertex( );
fli_add_float_vertex( txc, tyc );
fli_add_float_vertex( txc + rad * cos( ARCINC * curang ),
tyc - rad * sin( ARCINC * curang ) );
fli_endline( );
curang += 0.5 * incr * e->val;
lbox = 16;
/* draw the label */
xl = txc + 1.1 * rad * cos( ARCINC * curang );
yl = tyc - 1.1 * rad * sin( ARCINC * curang );
if ( xl < txc )
fl_drw_text_beside( FL_ALIGN_LEFT, xl, yl - 0.5 * lbox,
lbox, lbox, e->lcol, sp->lstyle,
sp->lsize, e->str );
else
fl_drw_text_beside( FL_ALIGN_RIGHT, xl - lbox, yl - 0.5 * lbox,
lbox, lbox, e->lcol, sp->lstyle,
sp->lsize, e->str );
curang += 0.5 * incr * e->val;
fli_reset_vertex( );
fli_add_float_vertex( txc, tyc );
fli_add_float_vertex( txc + rad * cos( ARCINC * curang ),
tyc - rad * sin( ARCINC * curang ) );
fli_endline( );
}
}
/***************************************
* Draws a chart object
***************************************/
static void
draw_chart( FL_OBJECT * ob )
{
FLI_CHART_SPEC *sp = ob->spec;
FL_Coord absbw = FL_abs( ob->bw );
float min = sp->min,
max = sp->max;
int i;
/* Find bounding box */
sp->x = ob->x + 3 + 2 * absbw;
sp->y = ob->y + 3 + 2 * absbw;
sp->w = ob->w - 6 - 4 * absbw;
sp->h = ob->h - 6 - 4 * absbw;
/* Find bounds */
if ( min == max )
{
min = max = sp->numb ? sp->entries[ 0 ].val : 0.0;
for ( i = 0; i < sp->numb; i++ )
{
if ( sp->entries[ i ].val < min )
min = sp->entries[ i ].val;
if ( sp->entries[ i ].val > max )
max = sp->entries[ i ].val;
}
}
/* min can equal to max if only one entry */
if ( min == max )
{
min -= 1.0;
max += 1.0;
}
/* Do the drawing */
fl_drw_box( ob->boxtype, ob->x, ob->y, ob->w, ob->h, ob->col1, ob->bw );
if ( sp->numb == 0 )
{
fl_drw_text_beside( ob->align, ob->x, ob->y, ob->w, ob->h,
ob->lcol, ob->lstyle, ob->lsize, ob->label );
return;
}
fl_set_clipping( sp->x - 1, sp->y - 1, sp->w + 2, sp->h + 2 );
switch ( ob->type )
{
case FL_BAR_CHART:
draw_barchart( ob, min, max );
break;
case FL_HORBAR_CHART:
draw_horbarchart( ob, min, max );
break;
case FL_PIE_CHART:
draw_piechart( ob, 0 );
break;
case FL_SPECIALPIE_CHART:
draw_piechart( ob, 1 );
break;
default:
draw_linechart( ob, min, max );
break;
}
fl_unset_clipping( );
}
/***************************************
* Handles an event, returns whether value has changed
***************************************/
static int
handle_chart( FL_OBJECT * ob,
int event,
FL_Coord mx FL_UNUSED_ARG,
FL_Coord my FL_UNUSED_ARG,
int key FL_UNUSED_ARG,
void * ev FL_UNUSED_ARG )
{
#if FL_DEBUG >= ML_DEBUG
M_info( "handle_chart", fli_event_name( event ) );
#endif
switch ( event )
{
case FL_DRAW:
draw_chart( ob );
/* fall through */
case FL_DRAWLABEL:
fl_draw_object_label( ob );
break;
case FL_FREEMEM:
if ( ( ( FLI_CHART_SPEC * ) ob->spec )->entries )
fl_free( ( ( FLI_CHART_SPEC * ) ob->spec )->entries );
fl_free( ob->spec );
break;
}
return FL_RETURN_NONE;
}
/***************************************
* creates a chart object
***************************************/
FL_OBJECT *
fl_create_chart( int type,
FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
const char * label )
{
FL_OBJECT *obj;
FLI_CHART_SPEC *sp;
int i;
obj = fl_make_object( FL_CHART, type, x, y, w, h, label, handle_chart );
obj->boxtype = FL_CHART_BOXTYPE;
obj->col1 = FL_CHART_COL1;
obj->col2 = FL_BLACK;
obj->align = FL_CHART_ALIGN;
obj->lcol = FL_CHART_LCOL;
obj->active = 0;
sp = obj->spec = fl_calloc( 1, sizeof *sp );
sp->maxnumb = 512;
sp->entries = fl_calloc( sp->maxnumb + 1, sizeof *sp->entries );
for ( i = 0; i <= sp->maxnumb; i++ )
sp->entries[ i ].val = 0.0;
sp->autosize = 1;
sp->min = sp->max = 0.0;
sp->lsize = FL_TINY_SIZE;
sp->lstyle = FL_NORMAL_STYLE;
sp->lcol = FL_BLACK;
return obj;
}
/***************************************
* Adds a chart object
***************************************/
FL_OBJECT *
fl_add_chart( int type,
FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
const char * label )
{
FL_OBJECT *obj = fl_create_chart( type, x, y, w, h, label );
fl_add_object( fl_current_form, obj );
return obj;
}
/***************************************
***************************************/
void
fl_set_chart_lsize( FL_OBJECT * ob,
int lsize )
{
FLI_CHART_SPEC *sp = ob->spec;
if ( sp->lsize != lsize )
{
sp->lsize = lsize;
fl_redraw_object( ob );
}
}
/***************************************
***************************************/
void
fl_set_chart_lstyle( FL_OBJECT * ob,
int lstyle )
{
FLI_CHART_SPEC *sp = ob->spec;
if ( sp->lstyle != lstyle )
{
sp->lstyle = lstyle;
fl_redraw_object( ob );
}
}
/***************************************
***************************************/
void
fl_set_chart_lcolor( FL_OBJECT * ob,
FL_COLOR lcol )
{
FLI_CHART_SPEC *sp = ob->spec;
if ( sp->lcol != lcol )
sp->lcol = lcol;
}
/***************************************
* Clears the contents of a chart
***************************************/
void
fl_clear_chart( FL_OBJECT * ob )
{
( ( FLI_CHART_SPEC * ) ob->spec )->numb = 0;
fl_redraw_object( ob );
}
/***************************************
* Add an item to the chart.
***************************************/
void
fl_add_chart_value( FL_OBJECT * ob,
double val,
const char * str,
FL_COLOR col )
{
FLI_CHART_SPEC *sp = ob->spec;
#if FL_DEBUG >= ML_ERR
if ( ! IsValidClass( ob, FL_CHART ) )
{
M_err( "fl_add_chart_value", "%s not a chart", ob ? ob->label : "" );
return;
}
#endif
/* Shift entries if required */
if ( sp->numb == sp->maxnumb )
{
int i;
for ( i = 0; i < sp->numb - 1; i++ )
sp->entries[ i ] = sp->entries[ i + 1 ];
sp->numb--;
}
/* Fill in the new entry */
sp->entries[ sp->numb ].val = val;
sp->entries[ sp->numb ].col = col;
sp->entries[ sp->numb ].lcol = sp->lcol;
if ( str )
fli_sstrcpy( sp->entries[sp->numb ].str, str, MAX_CHART_LABEL_LEN );
else
*sp->entries[sp->numb ].str = '\0';
sp->numb++;
fl_redraw_object( ob );
}
/***************************************
* Inserts an item before indx to the chart.
***************************************/
void
fl_insert_chart_value( FL_OBJECT * ob,
int indx,
double val,
const char * str,
FL_COLOR col )
{
FLI_CHART_SPEC *sp = ob->spec;
int i;
#if FL_DEBUG >= ML_ERR
if ( ! IsValidClass( ob, FL_CHART ) )
{
M_err( "fl_insert_chart_value", "%s not a chart", ob ? ob->label : "" );
return;
}
#endif
if ( indx < 1 || indx > sp->numb + 1 )
return;
/* Shift entries */
for ( i = sp->numb; i >= indx; i-- )
sp->entries[ i ] = sp->entries[ i - 1 ];
if ( sp->numb < sp->maxnumb )
sp->numb++;
/* Fill in the new entry */
sp->entries[ indx - 1 ].val = val;
sp->entries[ indx - 1 ].col = col;
if ( str != NULL )
fli_sstrcpy( sp->entries[ indx - 1 ].str, str, MAX_CHART_LABEL_LEN );
else
*sp->entries[ indx - 1 ].str = '\0';
fl_redraw_object( ob );
}
/***************************************
* Replaces an item in the chart.
***************************************/
void
fl_replace_chart_value( FL_OBJECT * ob,
int indx,
double val,
const char * str,
FL_COLOR col )
{
FLI_CHART_SPEC *sp = ob->spec;
if ( indx < 1 || indx > sp->numb )
return;
sp->entries[ indx - 1 ].val = val;
sp->entries[ indx - 1 ].col = col;
if ( str )
fli_sstrcpy( sp->entries[ indx - 1 ].str, str, MAX_CHART_LABEL_LEN );
else
*sp->entries[ indx - 1 ].str = '\0';
fl_redraw_object( ob );
}
/***************************************
* Sets the boundaries in the value for the object
***************************************/
void
fl_set_chart_bounds( FL_OBJECT * ob,
double min,
double max )
{
FLI_CHART_SPEC *sp;
#if FL_DEBUG >= ML_ERR
if ( ! IsValidClass( ob, FL_CHART ) )
{
M_err( "fl_set_chart_bounds", "%s not a chart", ob ? ob->label : "" );
return;
}
#endif
sp = ob->spec;
if ( sp->min != min || sp->max != max )
{
sp->min = min;
sp->max = max;
fl_redraw_object( ob );
}
}
/***************************************
***************************************/
void
fl_get_chart_bounds( FL_OBJECT * ob,
double * min,
double * max )
{
FLI_CHART_SPEC *sp = ob->spec;
*min = sp->min;
*max = sp->max;
}
/***************************************
* Sets the maximal number of values displayed in the chart
***************************************/
void
fl_set_chart_maxnumb( FL_OBJECT * ob,
int maxnumb )
{
FLI_CHART_SPEC *sp = ob->spec;
int i, curmax;
/* Fill in the new number */
if ( maxnumb < 0 )
{
M_err( "fl_set_chart_maxnum", "Invalid maxnumb value" );
return;
}
if ( maxnumb == sp->maxnumb )
return;
curmax = sp->maxnumb;
if ( maxnumb > FL_CHART_MAX )
sp->maxnumb = FL_CHART_MAX;
else
sp->maxnumb = maxnumb;
if ( sp->maxnumb > curmax )
{
sp->entries = fl_realloc( sp->entries,
( sp->maxnumb + 1 ) * sizeof *sp->entries );
for ( i = curmax; i <= sp->maxnumb; i++ )
sp->entries[ i ].val = 0.0;
}
if ( ! sp->entries )
{
sp->maxnumb = curmax;
sp->entries = fl_calloc( curmax + 1, sizeof *sp->entries );
for ( i = 0; i <= curmax; i++ )
sp->entries[ i ].val = 0.0;
return;
}
/* Shift entries if required */
if ( sp->numb > sp->maxnumb )
{
for ( i = 0; i < maxnumb; i++ )
sp->entries[ i ] = sp->entries[ i + sp->numb - maxnumb ];
sp->numb = sp->maxnumb;
fl_redraw_object( ob );
}
}
/***************************************
* Sets whether the chart should autosize along the x-axis
***************************************/
void
fl_set_chart_autosize( FL_OBJECT * ob,
int autosize )
{
if ( ( ( FLI_CHART_SPEC * ) ob->spec )->autosize != autosize )
{
( ( FLI_CHART_SPEC * ) ob->spec )->autosize = autosize;
fl_redraw_object( ob );
}
}
/***************************************
***************************************/
void
fl_set_chart_baseline( FL_OBJECT * ob,
int iYesNo )
{
if ( ( ( FLI_CHART_SPEC * ) ob->spec )->no_baseline != !iYesNo )
{
( ( FLI_CHART_SPEC * ) ob->spec )->no_baseline = !iYesNo;
fl_redraw_object( ob );
}
}
/***************************************
***************************************/
#if 0
void
fl_get_chart_area( FL_OBJECT * ob,
int * x,
int * y,
int * w,
int * h )
{
FLI_CHART_SPEC *sp = ob->spec;
*x = sp->x;
*y = sp->y;
*w = sp->w;
*h = sp->h;
}
#endif
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|