/*
The following code shows various capabilities of the GraphView object. 
Graph View is placed in the public domain for non-commercial use by not-for-profit educational institutions in the United States.   All other rights reserved by Wolfgang Christian.

GraphView objects contain a storage object that remembers points, line segments, arrays of data and histograms for future rendering and rescaling.  Once data has been registered with a Graph View it must be cleared out with a clearDataInfo:sender message.  Temporary drawing can done inside a graphView using the drawWithContentsFrom: method.  Arrays of data points drawn with this method call are not registered but this method is very fast since it uses display postcript userpaths.  The drawWithContentsFrom: method is useful for animation of graphs.

GraphView objects can clone themselves into new windows, zoom, unzoom, print, copy and paste.


Be sure and call the "awake" method for ANY graphView that you instantiate in your application.  You can then set the scale and various other parameters for
your graphs or you can leave these values uninitialized and change them at run time with the GraphView inspector.

*/

#import "PlotControl.h"
#import <math.h>
#import <appkit/appkit.h>
#import "GraphView.h"

#define  NUMPTS  50

@implementation PlotControl
- appDidInit:sender
{
[thePlotView1 awake];[thePlotView2 awake];[thePlotView3 awake];


[[[thePlotView1 manualXMin:-1.0] manualXMax:NUMPTS] setXMajorTickWidthTo:NUMPTS/5];
[[[thePlotView1 manualYMin:-1.0] manualYMax:NUMPTS] setYMajorTickWidthTo:NUMPTS/5];
[thePlotView1 setXAxisLocationTo:ORIGIN];    //default would have been center of graph
[thePlotView1 setYAxisLocationTo:ORIGIN]; 
[thePlotView1 showGrid:NO];   
[thePlotView1 enableDrawing:nil];

[[[thePlotView2 manualXMin:0.0] manualXMax:100.0] setXMajorTickWidthTo:20.0];
[[[thePlotView2 manualYMin:-0.2] manualYMax:1.0] setYMajorTickWidthTo:0.2];
[thePlotView2 setCurrentPenColorToRGBColor:1.0:1.0:1.0];
[thePlotView2 enableDrawing:nil];

[[[thePlotView3 manualXMin:0.0] manualXMax:100.0] setXMajorTickWidthTo:20.0];
[[[thePlotView3 manualYMin:-1.0] manualYMax:1.2] setYMajorTickWidthTo:0.2];
[[thePlotView3 setCurrentPenColorToRGBColor:1.0:1.0:1.0]colorOn:nil];
[[[thePlotView3 setTitleTo:"Animation Example" at:(double) 0.3:(double)0.92]
                            setXTitleTo:"Point Number"]
			    setYTitleTo:"Amplitude"];
[thePlotView3 enableDrawing:nil];

return self;
}

- plotPoints:sender
{
  int i,j; 
  float sum;
  NXDPoint histPoint, aPoint;  
  
  [thePlotView1 setCurrentPenStyleTo:LIFT];   
  [thePlotView1 setTitleTo:"Binominal Distribution" at:0.2:0.92];
  [[thePlotView1 setCurrentPenColorToRGBColor:0.0:1.0:0.0] colorOn:nil];  
 
// Draw  the Normal Distribution for comparison.
  for(i=0;i<=NUMPTS;i++){
    if (i==1)   [thePlotView1 setCurrentPenStyleTo:SLINE];  
    aPoint.x=i;
    aPoint.y=NUMPTS*exp(-sqr(i-NUMPTS/2.0)/NUMPTS);
    [thePlotView1 addThePoint:aPoint];
  }
  
[[thePlotView1 setBinWidthTo:1.0] setCurrentBinEdgeTo:0.5]; 
[thePlotView1 setCurrentPenColorToRGBColor:1.0:1.0:0.0];
histPoint.y=1.0;
for(i=1;i<NUMPTS;i++)
  {
  sum = 0.0;
  for (j=0;j<NUMPTS;j++) sum += (((int)random())&1);
  histPoint.x=sum;
  [thePlotView1 addHistogramPoint:histPoint];
  }
return self;
}

- plotLines:sender
{
  int i; 
  NXDPoint oldPoint1,oldPoint2,nextPoint;  
  [thePlotView2 showGrid:NO];   
  [thePlotView2 setXAxisLocationTo:ORIGIN];    //default would have been center of graph
  [thePlotView2 setYAxisLocationTo:ORIGIN]; 
  oldPoint1.x=0;
  oldPoint1.y=1.0;  
  oldPoint2.x=0;
  oldPoint2.y=1.0;  
  for(i=1;i<100;i++){
    nextPoint.x=i;
    nextPoint.y=exp(-0.01*i);
    [thePlotView2 drawLineFrom:oldPoint1 to:nextPoint];
    oldPoint1=nextPoint; 
    nextPoint.y=exp(-0.02*i);
    [thePlotView2 drawLineFrom:oldPoint2 to:nextPoint];    
    oldPoint2=nextPoint;
  }
[thePlotView2 setTitleTo:"Two Functions" at:0.1:0.1];
return self;
}
- plotArray:sender
{	
NXDPoint	dataArray1[100];
NXDPoint	dataArray2[100];
int		i;
		
for(i=0;i<100;i++)						// Generate the data
  {dataArray1[i].x = dataArray2[i].x = i;
    dataArray1[i].y = sin(0.2*(i/2.0));
    dataArray2[i].y = cos(0.2*(i/2.0));
  }
  
[thePlotView3 clearDataInfo:nil];
[thePlotView3 setCurrentPenStyleTo:SQUARE];
[[thePlotView3 setCurrentPenColorToRGBColor:1.0:0.0:0.0]
	                    acceptContentsFrom:dataArray1 arrayOfLength:100 asNewTrace:YES];
[thePlotView3 setCurrentPenStyleTo:CIRCLE];
[[thePlotView3 setCurrentPenColorToRGBColor:0.0:0.0:1.0]
	                    acceptContentsFrom:dataArray2 arrayOfLength:100 asNewTrace:YES];
return self;
}

- tempPlotArray:sender    //drawing commands will NOT register the points with the Plot view.
{
NXDPoint	dataArray1[100];
NXDPoint	dataArray2[100];
int		i;
float		t=0;

while (t<5.0)
{
  for(i=0;i<100;i++)
    {
      dataArray1[i].x = dataArray2[i].x = i;
      dataArray1[i].y = sin(0.2*(i/2.0)+t);
      dataArray2[i].y = cos(0.2*(i/2.0)+t);
     }
	
  [[thePlotView3 setCurrentPenColorToRGBColor:1.0:0.0:0.0]
	                    drawWithContentsFrom:dataArray1 arrayOfLength:100 asNewTrace:YES];
  [[thePlotView3 setCurrentPenColorToRGBColor:0.0:0.0:1.0]
	                    drawWithContentsFrom:dataArray2 arrayOfLength:100 asNewTrace:NO];
  [[thePlotView3 window] flushWindow];
  t+= 0.05;
 }	
return self;

}
-printKeyWindow:sender
{
if([NXApp keyWindow]) [[NXApp keyWindow]  smartPrintPSCode:nil];
return self;
}
@end
