File: plot_line.c

package info (click to toggle)
xspread 3.1.1c-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,836 kB
  • ctags: 1,426
  • sloc: ansic: 19,342; yacc: 499; lisp: 231; makefile: 198; sh: 28; sed: 4
file content (1022 lines) | stat: -rw-r--r-- 30,461 bytes parent folder | download | duplicates (2)
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
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
 * Copyright (C) 1992  Board of Regents of the University of Wisconsin
 * on behalf of the Department of Electrical Engineering and Computer
 * Science, University of Wisconsin-Milwaukee, Milwaukee, WI 53201.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * a copy of which is included here in file "GNU_GENERAL"
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * The programs in this directory were developed by software engineering 
 * teams as part of the course "Introduction to Software Engineering" 
 * under the supervision of Professor G. Davida.
 * This is a modification of a program written or modified by
 * others.  The original copyrights, as per GNU General Public License,
 * may still be applicable.  The UWM copyright is applicable only
 * the those parts generated at UWM.
 *
 * Please send all changes, enhancements, and other comments about this
 * software to
 *     		soft-eng@cs.uwm.edu
 *
 * No Warranty, expressed or implied, comes with this software.
 * This software is intended to be used by not-for-profit
 * organizations or by individuals for personal HOME use. 
 * This software, or any of its parts, may not be used by for-profit
 * organization, regardless of application or intended product or
 * customer, without the permission of the Board of Regents of the 
 * University  of Wisconsin. 
 *
 * Contact:	soft-eng@cs.uwm.edu
 *			or
 *		
 *		Software Engineering Coordinator
 *		Computer Science
 *    		Department of EECS
 *		University of Wisconsin - Milwaukee
 *		Milwaukee, WI  53201
 *		414-229-4677
 *
 *		HISTORY,CLAIMS and CONTRIBUTIONS
 */
/*
 ******************************************************************************
 * This procedure will display the line graph given the values provided by    *
 * the user.  It evenly spaces out the columns depending on the number of     *
 * X-Values which are used.
 * Dan Gruber - October 1991                                                  *
 *   Modified 5/94 by Dan Coppersmith to improve user friendliness of
 *   error messages.
 **************************************************************************** 
 */
#include <config.h>

#ifdef HAVE_X11_X_H	/* this code for now is X specific */

#include <math.h>
#include <curses.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "plot.h" 
#include "graphic_gvar.h"
#include "sc.h"
#include "scXstuff.h"
#define TITLE "XSPREAD-Grapher (Line Plot)"
#define PIX_SEP 3

#ifndef irint
#define irint(x) floor((x)+0.5)
#endif

/*
 * This structure forms the WM_HINTS property of the window,
 * letting the window manager know how to handle this window.
 * See Section 9.1 of the Xlib manual.
 */

   XWMHints     XWMH = {
	  (InputHint|StateHint),   /* flags         */
          False,                   /* input         */
          NormalState,             /* initial_state */
	  0,                       /* icon pixmap   */
	  0,                       /* icon window   */
          0, 0,                    /* icon location */
          0,                       /* icon mask     */
          0,                       /* Window group  */
   };


void
plot_line()
{   double max_y, min_y; 
    double Y_Range;
    double y_power /*, x_power */; 
    double grid_w;
   
    int  i, j /*,k*/, len;
    int  first_y_datum=1;
    int  grid_l_x, grid_l_y, grid_h;
    int  /* font_w, */ text_w;
    int  legend_n;
    int  x, y, x1, y1;
    int  looping = 1;  /* true if we shouldn't exit yet                    */
    int  draw = 0;     /* true if we should paint the window               */
    char format;       /* graph format specifier, (L)ine, (S)ymbol, (B)oth */
    int  first_point;  /* true when getting coords for 1st point in curve  */
    struct ent *p;
    struct ent *yent;  /* table entries y values                           */
    int cells=0; 
    int items=0;
    int number_of_items=0;
    int width;         /* used to calculate the Labelsize */
    int labelsize;     /* The size of the Label to be drawn */
    
    char str[100];
    /* register int c; */

    int argc = 0;
    char **argv = 0;

/*---- Colors  and fonts added by Rama Devi ---------*/
Colormap cmap;
XColor exactBlack, exactOrange;
XColor exactGreen, exactGreenYellow, exactOrchid;
XColor exactYellow, exactAquamarine, exactGoldenrod;

XColor Black, Orange;
XColor Green, GreenYellow, Orchid, Yellow, Aquamarine, Goldenrod;

extern XFontStruct *curfont;       /* Font descriptor struct for current font */
extern Font       curfontid;       /* resource id of current font */
extern int        curfontwidth,
           curfontheight;   /* pixel dimensions of current font */

/* check that we have enough valid x values.  While we are looping,
 * determine the minimum and maximum x values, in case we need them
 */

   for (i=graphic_range[0].r1; i <= graphic_range[0].r2; i++) { 
     p = lookat( i, graphic_range[0].c);
     if (!(p->label)) continue;
       cells++;
   } /* for i */

   if (cells < 2) {
     fprintf(stderr,"\007");
     if (graphic_range[0].r2 - graphic_range[0].r1 > 1)
	message("x values must be strings");
     else message("Not enough valid X values");
     return;
   }

   number_of_items = cells;

/* 
 * The plotting job now
 */

    /*
     *  Decide the scale for x and y axes
     */

     /* calculate y limits automatically or manually */
     if (g_auto_man[1] == 'M'){
        max_y = graphic_scale[1][1];
        min_y = graphic_scale[1][0];
     } else {
        first_y_datum = 1;
	for (j = 1; j < GRAPHRANGES; j++) { 

           if (*(graphic_range[j].col) == '\0') continue;

           for (i=graphic_range[j].r1; i <= graphic_range[j].r2; i++) { 
	     p = lookat(i, graphic_range[j].c);
	     if ( !(p->flags & is_valid)) continue; /* ignore invalid */
             if (first_y_datum) {
               max_y = p->v;
               min_y = p->v;
               first_y_datum = 0;
             } else {
               max_y = Max(max_y, p->v); 
               min_y = Min(min_y, p->v);
	     }
           }
        }
     } 
   /* protection for the case max == min */
   if (min_y == max_y) {
       max_y = 1.01 * max_y;
       min_y = 0.99 * min_y;
    }
   
    y_power = - floor(log10((max_y - min_y)));
    y_power = pow10(y_power);

    min_y = floor(min_y * y_power) / y_power;
    max_y = ceil(max_y * y_power) / y_power;
    Y_Range = max_y - min_y;
    
    /* 
     * Open a new display window
     */

    /*
     * Select colors for the border,  the window background,  and the
     * foreground.
     */


    /*---- Colors fonts  added by Rama Devi --*/
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor(dpy, cmap, "Black", &exactBlack,
             &Black) != 0) && (XAllocNamedColor (dpy, cmap,
             "Orange", &exactOrange, &Orange) != 0))
      {
      bd = Orange.pixel;
      bg = Black.pixel;
      fg = Orange.pixel;
      }
    else 
      {
      bd = WhitePixel(dpy, DefaultScreen(dpy));
      bg = BlackPixel(dpy, DefaultScreen(dpy));
      fg = WhitePixel(dpy, DefaultScreen(dpy));
      }

  if (userfont == NULL)
     curfont = XLoadQueryFont(dpy, SC_FONT);
  else
     curfont = XLoadQueryFont(dpy, userfont);
  if (curfont == NULL)
  {
    fprintf(stderr, "%s: Display %s doesn't know font \"%s\" \n",
          progname, DisplayString(dpy), userfont == NULL ? SC_FONT : userfont);
    exit(1);
  }
  /* initialize the font-related globals */
  /* usefont(curfont); */
  curfontid = curfont->fid;
  curfontheight = curfont->max_bounds.ascent + curfont->max_bounds.descent;
  curfontwidth = curfont->max_bounds.width;

    /*
     * Set the border width of the window,  and the gap between the text
     * and the edge of the window, "pad".
     */

    pad = BORDER;
    bw = 1;

    /*
     * Deal with providing the window with an initial position & size.
     * Fill out the XSizeHints struct to inform the window manager.
     */

    xsh.width = XTextWidth(curfont, TITLE, 80 + pad * 2);

    xsh.flags = (PPosition | PSize);
    xsh.height = WIN_H; 
    xsh.width = WIN_W; 
    xsh.x = (DisplayWidth(dpy, DefaultScreen(dpy)) - xsh.width) / 2;
    xsh.y = (DisplayHeight(dpy, DefaultScreen(dpy)) - xsh.height) / 2;

    /*
     * Create the Window with the information in the XSizeHints, the
     * border width,  and the border & background pixels.
     */

    win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
                  xsh.x, xsh.y, xsh.width, xsh.height,
                            /*---  bw, bd, bg); ---*/
                              bw, fg, bg);

    /*
     * Set the standard properties for the window managers. 
     */

    XSetStandardProperties(dpy, win, TITLE, TITLE, 
			   None, argv, argc, &xsh);
    XSetWMHints(dpy, win, &XWMH);

    /*
     * Ensure that the window's colormap field points to the default
     * colormap,  so that the window manager knows the correct colormap to
     * use for the window.   Also,  set the window's Bit Gravity to reduce 
     * Expose events.
     */

    xswa.colormap = DefaultColormap(dpy, DefaultScreen(dpy));
    xswa.bit_gravity = CenterGravity;
    XChangeWindowAttributes(dpy, win, (CWColormap | CWBitGravity), &xswa);


/*--- The following statement is added by Rama Devi ---*/
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Orange", &exactOrange, &Orange) != 0))
       {
       maingcreversed = XCreateGC(dpy, win, 0,0);
       XSetFont(dpy, maingcreversed, curfontid);
       XSetForeground (dpy, maingcreversed, Orange.pixel);
       XSetBackground (dpy, maingcreversed, Black.pixel);
       }
    else
       {
       maingcreversed = XCreateGC(dpy, win, 0,0);
       XSetFont(dpy, maingcreversed, curfontid);
       XSetForeground (dpy, maingcreversed, fg);
       XSetBackground (dpy, maingcreversed, bg);
       }  /*---*/

    /* 
     * Map the window to make it visible.  
     */
   
     XMapWindow(dpy, win);

    /*
     * Find out the dimension of the window.
     */

    if (XGetWindowAttributes(dpy, win, &xwa) == 0){
       fprintf(stderr,"plot_XY: Can't access attributes for graph window\n");
       exit(1);    
    }

    XClearWindow(dpy, win);
    XSelectInput(dpy,win, ButtonPressMask|KeyPressMask|ExposureMask);

while(looping)
 {   

    XEvent event;

    XNextEvent(dpy,&event);
    switch (event.type){
      case KeyPress: 
      case ButtonPress: looping = 0; 
			break;
      case Expose: draw = 1;
    }


    /* if not drawing, go back and get another event */
    if (!draw) continue;

    /*
     * Draw the coordinate box
     */

    XDrawRectangle(dpy, win, maingcreversed, RECT_X, RECT_Y, RECT_W, RECT_H); 
    
    /*  grid_l_y and grid_l_x are the length of the grid lines.  
     *  Only tick marks are drawn when the value is '8'.
     */

    if (graphic_grid == 'H') {              /* horizontal grid     */
       grid_l_x = RECT_W;
       grid_l_y = 8;
    }
    else if (graphic_grid == 'V') {         /* vertical grid       */
       grid_l_x = 8;
       grid_l_y = RECT_H;
    }
    else if (graphic_grid == 'B') {         /* both direction grid */
       grid_l_x = RECT_W;
       grid_l_y = RECT_H;
    }
    else {
       grid_l_x = 8;                        /* no grid             */
       grid_l_y = 8;
    }
       

    grid_w = RECT_W / (double)(number_of_items + 1);  /* grid seperation */
    grid_h = RECT_H / 10;

    /*
     * Draw grids along with the labels on the Y-Axis
     */

    for (i = 0; i < 11; i++) {
       XDrawLine(dpy, win, maingcreversed, 
		 RECT_X, RECT_Y + i * grid_h, 
		 RECT_X + grid_l_x, RECT_Y + i * grid_h); 
       XDrawLine(dpy, win, maingcreversed, 
		 RECT_X + RECT_W, RECT_Y + i * grid_h, 
                 RECT_X + RECT_W - grid_l_x, RECT_Y + i * grid_h); 

                 sprintf(str,"%.1f",(max_y - Y_Range*i/10));
                 len = strlen(str);
                 text_w = XTextWidth(curfont,str,len+2);
                 XDrawImageString(dpy,win,maingcreversed,
                            RECT_X - text_w,
                            RECT_Y + i*grid_h + curfontheight/3,
                            str,len);

       } /*end for loop*/


    for (i = 1; i < (number_of_items + 1); i++) {
       x = RECT_X + i * grid_w;
       XDrawLine(dpy, win, maingcreversed, 
		 x, RECT_Y, 
		 x, RECT_Y + grid_l_y); 
       XDrawLine(dpy, win, maingcreversed,
                 x, RECT_Y + RECT_H, 
                 x, RECT_Y + RECT_H - grid_l_y); 
    }

    /*
     * Draw titles
     */

    /* first graph title */
    len = strlen(graphic_title[0]);   
    text_w= XTextWidth(curfont, graphic_title[0], len);
    XDrawImageString(dpy, win, maingcreversed, (WIN_W - text_w)/2, RECT_Y/3,
                     graphic_title[0], len);

    /* second graph title */
    len = strlen(graphic_title[1]);
    text_w= XTextWidth(curfont, graphic_title[1], len);
    XDrawImageString(dpy, win, maingcreversed, (WIN_W - text_w)/2, RECT_Y*2/3, 
                     graphic_title[1], len);

    /* x-axis title */
    len = strlen(graphic_title[2]);
    text_w= XTextWidth(curfont, graphic_title[2], len);
    XDrawImageString(dpy, win, maingcreversed, 
		     (WIN_W - text_w)/2, RECT_H + RECT_Y * 3/2, 
		     graphic_title[2], len);
  
    /* y-axis title */
    len = strlen(graphic_title[3]);
    text_w=XTextWidth(curfont,graphic_title[3],len);
       if ((RECT_X-text_w) < 0)
           x = curfontwidth;
       else
           x = RECT_X-text_w;
    XDrawImageString(dpy, win, maingcreversed,
       x, RECT_Y - curfontheight,
       graphic_title[3],len);


    /*
     * Draw Labels on the X-Axis
     */

    width = (RECT_W - (PIX_SEP * (number_of_items + 1)))/number_of_items;
    labelsize = irint(floor(width/(double)curfontwidth)-1);
    y = RECT_Y + RECT_H + curfontheight;
    items = 0; 
    for (i=graphic_range[0].r1; i<=graphic_range[0].r2; i++)
    {
        p = lookat(i,graphic_range[0].c);
        if (!(p->label)) continue;

        x1 = RECT_X + ((items+1)*(grid_w));
        items++;
        strncpy(str, p->label, labelsize);
        str[labelsize] = '\0';
        len = strlen(str);
        text_w = XTextWidth(curfont, str, len);
        
        x = x1-(text_w / 2);
        XDrawImageString(dpy,win,maingcreversed,
                    x,y,str,len);
    } 


    /*
     *  Draw legends
     */

    legend_n = 0;
    for (i = 0;  i < GRAPHLEGENDS; i++) {

/* ---- the following switch statement added by Rama Devi ----*/
              switch (i+1) {
    case 1 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Green", &exactGreen, &Green) != 0))
       {
       XSetForeground (dpy, maingcreversed, Green.pixel);
       XSetBackground (dpy, maingcreversed, Black.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       XSetBackground (dpy, maingcreversed, bg);
       }
                             break;

    case 2 :
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Orchid", &exactOrchid, &Orchid) != 0))
       {
       XSetForeground (dpy, maingcreversed, Orchid.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;

    case 3 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "GreenYellow", &exactGreenYellow, &GreenYellow) != 0))
       {
       XSetForeground (dpy, maingcreversed, GreenYellow.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;

    case 4 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Goldenrod", &exactGoldenrod, &Goldenrod) != 0))
       {
       XSetForeground (dpy, maingcreversed, Goldenrod.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;

    case 5 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));

    if ((XAllocNamedColor (dpy, cmap, "Aquamarine", &exactAquamarine, &Aquamarine) != 0))
       {
       XSetForeground (dpy, maingcreversed, Aquamarine.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;
    case 6 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Yellow", &exactYellow, &Yellow) != 0))
       {
       XSetForeground (dpy, maingcreversed, Yellow.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);

       }
                             break; } /* --switch --- */

       if ((*(graphic_range[i + 1].col) == '\0') || (curves_n < 1) || 
           (graphic_legend[i][0] == '\0')) 
          continue;

      len = strlen(graphic_legend[i]);
          text_w = XTextWidth(curfont, graphic_legend[i], len);
      if (curves_n == 1) { 
                                /* Draw the legend symble */
        if (graphic_format[i] == 'L') {
           XDrawLine(dpy, win, maingcreversed, 
		     RECT_X +(RECT_W - text_w) / 2  - 15, 
                     RECT_Y * 7 / 4 +RECT_H, RECT_X +(RECT_W - text_w) / 2 - 5, 
                     RECT_Y * 7 / 4 +RECT_H); 
        }
        else if ((graphic_format[i] == 'S') || (graphic_format[i] == 'B')) {
	   x = RECT_X + (RECT_W - text_w) / 2 - 10;
	   y = RECT_Y * 7 / 4 + RECT_H;
           switch (i) {
             case 0: LDrawOpenSquare(x,y); 	break;
             case 1: LDrawOpenTriangle(x,y); 	break;
             case 2: LDrawOpenDiamon(x,y); 	break;
             case 3: LDrawCross(x,y); 		break;
             case 4: LDrawCloseSquare(x,y); 	break;
             case 5: LDrawCloseDiamon(x,y); 	break;
           }
           if (graphic_format[i] == 'B') 
              XDrawLine(dpy, win, maingcreversed, 
			x-5, y,
			x+5, y); 
        } 
                                 /* Draw the legend title */

        XDrawImageString(dpy, win, maingcreversed, 
			 RECT_X + (RECT_W - text_w)/2, RECT_Y*7/4 + RECT_H, 
			 graphic_legend[i], len);
        break;
     }
     else {

                 /* Draw the legend symbols */

        if (graphic_format[i] == 'L') {
           XDrawLine(dpy, win, maingcreversed ,
		     RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 15, 
		     RECT_Y * 7 / 4 + RECT_H, 
		     RECT_X / 2 + legend_n * (WIN_W / curves_n - 10) - 5, 
		     RECT_Y *7 / 4 + RECT_H); 
        }
        else if ((graphic_format[i] == 'S') || (graphic_format[i] == 'B')) {
           switch (i) {
           case 0:
              LDrawOpenSquare(RECT_X/2 + legend_n*(WIN_W / curves_n - 10) - 10, 
                             RECT_Y * 7 / 4 + RECT_H);
              break;
           case 1:
              LDrawOpenTriangle(RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 10, 
                               RECT_Y * 7 / 4 + RECT_H);
              break;
           case 2:
              LDrawOpenDiamon(RECT_X/2 + legend_n * (WIN_W/curves_n - 10) - 10, 
                             RECT_Y*7/4 + RECT_H);
              break;
           case 3:
              LDrawCross(RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 10, 
                        RECT_Y*7/4 + RECT_H);
              break;
           case 4:
              LDrawCloseSquare(RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 10, 
                              RECT_Y*7/4 + RECT_H);
              break;
           case 5:
              LDrawCloseDiamon(RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 10, 
                              RECT_Y*7/4 + RECT_H);
              break;
           }
           if (graphic_format[i] == 'B')
              XDrawLine(dpy, win, maingcreversed ,
			RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 15, 
			RECT_Y*7/4 + RECT_H, 
			RECT_X/2 + legend_n*(WIN_W/curves_n - 10) - 5, 
			RECT_Y*7/4 + RECT_H); 
          
        }

                                   /* Draw the legend titles */
        XDrawImageString(dpy, win, maingcreversed, 
			 RECT_X/2 + legend_n*(WIN_W/curves_n - 10), 
			 RECT_Y*7/4 + RECT_H, 
                         graphic_legend[i], len);
        legend_n ++;
      }
   }


/* 
 * Draw curves
 */
        for (i = 1; i < GRAPHRANGES; i++) { 
           if (*(graphic_range[i].col) == '\0') continue;

/* ---- the following switch statement added by Rama Devi ----*/
              switch (i) {
    case 1 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Green", &exactGreen, &Green) != 0))
       {
       XSetForeground (dpy, maingcreversed, Green.pixel);
       XSetBackground (dpy, maingcreversed, Black.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       XSetBackground (dpy, maingcreversed, bg);
       }
                             break;

    case 2 :
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Orchid", &exactOrchid, &Orchid) != 0))
       {
       XSetForeground (dpy, maingcreversed, Orchid.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;

    case 3 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "GreenYellow", &exactGreenYellow, &GreenYellow) != 0))
       {
       XSetForeground (dpy, maingcreversed, GreenYellow.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;

    case 4 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Goldenrod", &exactGoldenrod, &Goldenrod) != 0))
       {
       XSetForeground (dpy, maingcreversed, Goldenrod.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;

    case 5 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));

    if ((XAllocNamedColor (dpy, cmap, "Aquamarine", &exactAquamarine, &Aquamarine) != 0))
       {
       XSetForeground (dpy, maingcreversed, Aquamarine.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       }
                             break;
    case 6 : 
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Yellow", &exactYellow, &Yellow) != 0))
       {
       XSetForeground (dpy, maingcreversed, Yellow.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);

       }
                             break; } /* --switch --- */

	   first_point = 1;
	   format = graphic_format[i];
           items = 0; 

           /*--------The following is commented by Rama Devi -----------

        ------   for (j = graphic_range[0].r1, k = graphic_range[i].r1;
    (j <= graphic_range[0].r2) && (k <= graphic_range[i].r2); j++, k++) {---

           if graphic_range[i].r1 is greater than graphic_range[0].r1 and the 
           difference between graphic_range[i].r1 and graphic_range[i].r2 is 
           less tahn the difference between graphic_range[0].r1 and graphic_
           range[0].r2 then the loop is executed only the difference between 
           graphic_range[0].r1 and graphic_range[0].r2 teimes and comes out
           without drawing the graphs for the specified ranges
           -----------------------------------------------------------*/
           /*--------- The following for loop added by Rama Devi  ------- */
  
           for (j = graphic_range[0].r1; j <= graphic_range[0].r2; j++) {

              if (!((p=lookat(j,graphic_range[0].c))->label)) continue;
              x = RECT_X + (items+1)*grid_w ;
              items++;

           /*------ the following if statement added by Rama Devi --*/
              if ((j >= graphic_range[i].r1) && (j <= graphic_range[i].r2)) 
                 {

	      yent = lookat(j, graphic_range[i].c);
	      if (!(yent->flags & is_valid) || (yent->v < min_y) 
		  || (yent->v > max_y))
		continue;

	      if (first_point){
                x1 = x;
           	y1 = RECT_Y + RECT_H - RECT_H * (yent->v - min_y) / Y_Range;   
	        if ((format == 'S') || (format == 'B'))
	  	  switch(i){
		    case 1: LDrawOpenSquare(x1, y1); 	break;
                    case 2: LDrawOpenTriangle(x1, y1); 	break;
                    case 3: LDrawOpenDiamon(x1, y1); 	break;
              	    case 4: LDrawCross(x1, y1);      	break;
                    case 5: LDrawCloseSquare(x1, y1);	break;
                    case 6: LDrawCloseDiamon(x1, y1);	break;
                  }
		first_point = 0;
		continue;
	      }

              y = RECT_Y + RECT_H - RECT_H * (yent->v - min_y) / Y_Range;   

	      if ((format == 'L') || (format == 'B')) 
                XDrawLine(dpy, win, maingcreversed, x1, y1, x, y);
	      if ((format == 'S') || (format == 'B'))
		switch(i){
		  case 1: LDrawOpenSquare(x, y); 	break;
                  case 2: LDrawOpenTriangle(x, y); 	break;
                  case 3: LDrawOpenDiamon(x, y); 	break;
              	  case 4: LDrawCross(x, y);      	break;
                  case 5: LDrawCloseSquare(x, y);	break;
                  case 6: LDrawCloseDiamon(x, y);	break;
                }
              x1 = x;
              y1 = y;

              } /*-----*/
           }
        }      

    /*---- The following two statements added by Rama Devi ----*/
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    if ((XAllocNamedColor (dpy, cmap, "Orange", &exactOrange, &Orange) != 0))
       {
       XSetForeground (dpy, maingcreversed, Orange.pixel);
       XSetBackground (dpy, maingcreversed, Black.pixel);
       }
    else
       {
       XSetForeground (dpy, maingcreversed, fg);
       XSetBackground (dpy, maingcreversed, bg);
       }

     }  /* end of while */


    /*---- The following statements added by Rama Devi ----*/
cmap = DefaultColormap (dpy, DefaultScreen (dpy));
if (backg && (XAllocNamedColor (dpy, cmap, backg, &exactOrange, &Orange) != 0))
    {
    bg = Orange.pixel;
    }
   else
    {
    bg = WhitePixel(dpy, DefaultScreen(dpy)); /* background */
    }
if (foreg && (XAllocNamedColor (dpy, cmap, foreg, &exactBlack, &Black) != 0))
    {
    fg = Black.pixel;
    }
   else
    {
    fg = BlackPixel(dpy, DefaultScreen(dpy)); /* foreground */
    }

      XSetForeground (dpy, maingc, fg);
      XSetBackground (dpy, maingc, bg);
      XSetForeground (dpy, maingcreversed, bg);
      XSetBackground (dpy, maingcreversed, fg);


    XUnmapWindow(dpy, win);
    XDestroyWindow(dpy, win);
    /*XCloseDisplay(dpy); */  /* NO. don't do this anymore */
}  /* end of plot_line */


/*
 * The following Drawing functions draw diffrent symbols. 
 */

void LDrawOpenDiamon(x, y)
short x, y;
{
  XPoint points[5];

  points[0].x = x; 
  points[0].y = y - 3; 
  points[1].x = 3; 
  points[1].y = 3; 
  points[2].x = -3; 
  points[2].y = 3; 
  points[3].x = -3; 
  points[3].y = -3; 
  points[4].x = 3; 
  points[4].y = -3; 
  XDrawLines(dpy, win, maingcreversed, points, 5, CoordModePrevious);
}

void LDrawCloseDiamon(x, y)
short x, y;
{
  XPoint points[12];

  points[0].x = x; 
  points[0].y = y; 
  points[1].x = -2; 
  points[1].y = 0; 
  points[2].x = 4; 
  points[2].y = 0; 
  points[3].x = -2; 
  points[3].y = 0; 
  points[4].x = 0; 
  points[4].y = -2; 
  points[5].x = 0; 
  points[5].y = 4; 
  points[6].x = 0; 
  points[6].y = -2; 
  points[7].x = 1; 
  points[7].y = 1; 
  points[8].x = -2; 
  points[8].y = -2; 
  points[9].x = 1; 
  points[9].y = 1; 
  points[10].x = -1; 
  points[10].y = 1; 
  points[11].x = 2; 
  points[11].y = -2; 
  XDrawLines(dpy, win, maingcreversed, points, 12, CoordModePrevious);
}

void LDrawOpenSquare(x, y)
short x, y;
{
  XPoint points[5];
  points[0].x = x - 2; 
  points[0].y = y - 2; 
  points[1].x = 4; 
  points[1].y = 0; 
  points[2].x = 0; 
  points[2].y = 4; 
  points[3].x = -4; 
  points[3].y = 0; 
  points[4].x = 0; 
  points[4].y = -4; 
  XDrawLines(dpy, win, maingcreversed, points, 5, CoordModePrevious);
}

void LDrawCloseSquare(x, y)
short x, y;
{
  XPoint points[9];

  points[0].x = x - 2; 
  points[0].y = y - 2; 
  points[1].x = 4; 
  points[1].y = 0; 
  points[2].x = 0; 
  points[2].y = 4; 
  points[3].x = -4; 
  points[3].y = 0; 
  points[4].x = 0; 
  points[4].y = -4; 
  points[5].x = 4; 
  points[5].y = 4; 
  points[6].x = -2; 
  points[6].y = -2; 
  points[7].x = -2; 
  points[7].y = 2; 
  points[8].x = 4; 
  points[8].y = -4; 
  XDrawLines(dpy, win, maingcreversed, points, 9, CoordModePrevious);
}

void LDrawOpenTriangle(x, y)
short x, y;
{ 
  XPoint points[4];

  points[0].x = x; 
  points[0].y = y - 3; 
  points[1].x = 2; 
  points[1].y = 5; 
  points[2].x = -4; 
  points[2].y = 0; 
  points[3].x = 2; 
  points[3].y = -5; 
  XDrawLines(dpy, win, maingcreversed, points, 4, CoordModePrevious);
}


void LDrawCross(x, y)
short x, y;
{
  XPoint points[6];

  points[0].x = x; 
  points[0].y = y; 
  points[1].x = -2; 
  points[1].y = 0; 
  points[2].x = 4; 
  points[2].y = 0; 
  points[3].x = -2; 
  points[3].y = 0; 
  points[4].x = 0; 
  points[4].y = -2; 
  points[5].x = 0; 
  points[5].y = 4; 
  XDrawLines(dpy, win, maingcreversed, points, 6, CoordModePrevious);
}


#ifdef NOTUSED
/*
 * Lrm_tail_zero removes heading Black spaces, trailing Black spaces, 
 * zero '0' and '.' of a string for nice printing of graph lable 
 *
 */

char *Lrm_tail_zero(s)
   char  *s;
{  
   char *t;
   /* Get rid of heading Black spaces */
   for (t = s; *t == ' ' || *t == '\t'; t++);
   strcpy(s, t);

   /* Get rid of trailing Black spaces and zeros */
   t = s;
   while ((*t) && (*t != '\n')) /* while not null or newline */
      t++;
   t--;
   while ((t != s) && ((*t == ' ') || (*t == '0')))
      t--;
   if (*t != '.')     /* Get rid of trailing '.' */
      t++;
   *t = '\0';
   return s;
}
#endif /* NOTUSED */

#endif /* HAVE_X11_X_H	this code for now is X specific */