/* @(#) MultiXYPlot.java 1.0 2000/07/12 Larry Rodrigues
 * Copyright (c) 2000 Larry Rodrigues
 */

package vis.beans.plotter;
import java.awt.*;
import vis.util.*;
import vis.beans.events.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
  /** Draws one or more XY plots on the same graph.
   * @version 1.0  12 July 2000
   * @author Larry Rodrigues
   **/
public class MultiXYPlot extends PlotViewport {
   /** Range of X values as an array of int */
   protected Vector allXY = new Vector();
   /** Range of Y values as an array of int.*/
   protected Vector allY = new Vector();
   public boolean enablePaintGraph = false;

   public MultiXYPlot(){
      //Set default values.
      numXTicks =6;
      numYTicks =4;
      numSmallXTicks =3;
      numSmallYTicks =3;
      setCustomizerClass(MultiXYPlotCustomizer.class);
   }

  /**Plots the XY graph. This is the target method for connecting to
    * the XY Plot bean using plot events.
    * @param e the PlotEvent
    */
   public void plotXYGraph(PlotEvent e){
      y = e.getYValues();
      // x values can sometimes be string.
      Object xs = e.getXValues();
      if(xs instanceof String[]){
         int len = ((String[])xs).length;
         int xi[] = new int[len];
         for(int i=0;i<len;i++) xi[i] =i;
         x= xi;
       }else x =xs;
       drawPlot(x, y);
   }

   /**  Adds a new graph. First checks whether graphNum exceeds maxPlots.
     *  This method is called by the draw() method after creating the graph template.
     *  addGraph() should not be called before executing draw().
     *  If there is only one graph, there is no need to call this method because
     *  drawPlot() calls it once.
     *  @param x an array of X axis values.
     *  @param y an array of Y axis values.
     */
   public void addGraph(Object x, Object y){
      if((x==null) ||(y==null)) return;
      if(graphNum > maxGraphs){
         System.out.println("Exceeds max number of graphs");
         return;
      }
      Object xyCoords[] = new Object[2];
      xyCoords[0] = x;
      xyCoords[1] = y;
      allXY.addElement(xyCoords);
      int numPlots = allXY.size();
      Enumeration e = allXY.elements();
      
      Object xrangearray[] = new Object[numPlots];
      Object yrangearray[] = new Object[numPlots];
      int i=0, j=0;
      while(e.hasMoreElements()){
         Object cord[] = (Object[])e.nextElement();
         Object xm = cord[0];
         Object ym = cord[1];
         if((xm==null) ||(ym==null)) continue;
         Object xrange = Util.getMinMax(xm);
         Object yrange = Util.getMinMax(ym);
         xrangearray[i++] = xrange;
         yrangearray[j++] = yrange;
      }
      xMinmaxObj = Util.getMinMax(xrangearray);
      yMinmaxObj = Util.getMinMax(yrangearray);
      enablePaintGraph = true;
      enableDraw = true;
      graphNum++;
   }
   public void paintGraphs(){
      if(!enablePaintGraph) return;
      Enumeration e = allXY.elements();
      int ind =0;
      while(e.hasMoreElements()){
         Object cord[] = (Object[])e.nextElement();
         Object xm = cord[0];
         Object ym = cord[1];
         if((xm==null) ||(ym==null)) continue;
         //Draw the graph
         int len = Util.getLength(xm);
         int xp = orig.x + Util.getDiff(xm,xMinmaxObj,0,xScale) ;
         int yp = orig.y - Util.getDiff(xm,yMinmaxObj,0,yScale);

         for(int k=0;k<len;k++){
             int xc = orig.x+ Util.getDiff(xm,xMinmaxObj,k,xScale) ;
             int yc = orig.y - Util.getDiff(ym,yMinmaxObj,k,yScale);
             offScrGc.setColor(getGraphColors(ind));
             offScrGc.drawLine(xc, yc, xp, yp);
             offScrGc.setColor(Color.green);
             offScrGc.drawOval(xc-2,yc-2,4,4);
             xp = xc; yp = yc;
         }
         ind++;
      }
      if(legendOn) drawLegends();
      repaint();
   }
    public void resetPlotViewport() {
        super.resetPlotViewport();
        graphNum =0;
        allXY = new Vector();
        allY = new Vector();
    }
}
