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

float datax[5000];
float datay[5000];
float xmin,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,j;
char line[256];

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

   xstart=0;

   i=0;
   while( gets(line) > 0){
      for(j=0;j<strlen(line);j++){
         if(line[j] == ',') line[j] = ' ';
      }
      sscanf(line,"%f %f",&datax[i],&datay[i]);
      i++;
   }
   nn = i;

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

   xmax = -1e10;
   xmin =  1e10;

   for(i=0;i<nn;i++) {
      if(datax[i]>xmax)xmax=datax[i];
      if(datax[i]<xmin)xmin=datax[i];
   }


   ymax = -1e10;
   ymin =  1e10;

   for(i=0;i<nn;i++) {
      if(datay[i]>ymax)ymax=datay[i];
      if(datay[i]<ymin)ymin=datay[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 =  xmin;
   xwin2 =  xmax;
   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,(float)( xwin1 + .1*(xwin2-xwin1) ),ymax,chstr);

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


redraw()
{
int i;
int color;
float xinc;

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

   upset(G,"colo",red);

   umove(G,xmin, ymin);
   udraw(G,xmax,ymin);
   udraw(G,xmax,ymax);
   udraw(G,xmin, ymax);
   udraw(G,xmin, ymin);

   umove(G,datax[0],datay[0]);


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

   if(barplot){
      /* DRAW BAR CHART */
      xinc = (xmax-xmin)/nn;
      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)(xinc*i),ymin,(float)(xinc*(i+1)),datay[i]);
         uset(G,"nofi");
      }
   }
}

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

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

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


