#include "gwin.user.h"
/* ROUTINE TO GRAPH REAL POINTS FROM STANDARD INPUT */

float data[5000];
float xmax;
int xstart,xstop;
float xdelta;
float green = 2.0;
float blue = 3.0;
float white = 7.0;
float black = 0.0;
float red = 1.0;
int quitfunction();
int barchart();
int barplot = 0;
int graphchart();
int graphplot = 1;
float ymin,ymax;
float xx,xtic;
float xwin1,xwin2,ywin1,ywin2;
char chstr[100];
int nn;

main(argc,argv)
int argc;
char *argv[];
{
int i;

   printf("\n\nIf you are typing in data via standard input\n");
   printf("enter carriage return then CTRL-\\ to end data input.\n\n");

   xstart=0;

   i=0;
   while( (scanf("%f",&data[i]) ) > 0) i++;
   nn = i;

   graph();
}
graph()
{
int i;
float x,y;
int makegrid;

   ymax = -1e10;
   ymin =  1e10;

   for(i=0;i<nn;i++) {
      if(data[i]>ymax)ymax=data[i];
      if(data[i]<ymin)ymin=data[i];
   }

   ymax = ymax + .05*(ymax-ymin);
   ymin = ymin - .05*(ymax-ymin);

   USTART("high2",0.,640.,0.,400.);
   uamenu(G,1,0,0,"FUNCTION",' ',0,MIDRAWN|MENUENABLED,0);
   uamenu(G,1,1,0,"GRAPH   ",'G',0,MIDRAWN|ITEMTEXT|HIGHCOMP
             |COMMSEQ|ITEMENABLED,graphchart);

   uamenu(G,1,2,0,"BARCHART",'B',0,MIDRAWN|ITEMTEXT|HIGHCOMP
             |COMMSEQ|ITEMENABLED,barchart);

   uamenu(G,1,3,0,"QUIT    ",'Q',0,MIDRAWN|ITEMTEXT|HIGHCOMP
             |COMMSEQ|ITEMENABLED,quitfunction);

   makegrid = TRUE;

   xwin1 =  0.0;
   xwin2 =  nn;
   ywin1 =  ymin;
   ywin2 =  ymax+.05*(ymax-ymin);

   uwindo(G,xwin1,xwin2,ywin1,ywin2);

   redraw();

   while(TRUE){
      if(ugrinl(G,&x,&y,&uuev)){UEND();exit(0);};
      upset(G,"colo",white);

      sprintf(chstr,"X = %10.3f",x);
      uprint(G,( xwin1 + .1*(xwin2-xwin1) ),ymax,chstr);

      sprintf(chstr,"Y = %10.3f",y);
      uprint(G,( xwin1 + .4*(xwin2-xwin1) ),ymax,chstr);
   }
}


redraw()
{
int i;
int color;

   upset(G,"colo",black);
   uset(G,"fill");
   uoutln(G);
   uset(G,"nofi");

   upset(G,"colo",red);

   umove(G,0.0, ymin);
   udraw(G,(float)nn,ymin);
   udraw(G,(float)nn,ymax);
   udraw(G,0.0, ymax);
   udraw(G,0.0, ymin);

   umove(G,0.,data[0]);


   if(graphplot){
      /* DRAW GRAPH */
      upset(G,"colo",green);
      uwindo(G,xwin1,xwin2-1.0,ywin1,ywin2);
      for(i=0;i<nn;i++) udraw(G,(float)i,data[i]);
   }

   if(barplot){
      /* DRAW BAR CHART */
      uset(G,"rint");
      uwindo(G,xwin1,xwin2,ywin1,ywin2);
      color = 0;
      for (i=0;i<nn;i++){
         if((color++ % 15) == 0) color = 1;
         upset(G,"colo",(float)color);
         uset(G,"fill");
         urect(G,(float)(i),ymin,(float)i+1,data[i]);
         uset(G,"nofi");
      }
   }
}

graphchart()
{
barplot = 0;
graphplot = 1;
redraw();
}

barchart()
{
barplot = 1;
graphplot = 0;
redraw();
}

quitfunction()
{
UEND();
exit(0);
}


