// BasicSplineRiskMap:


// Basic requirements for creating a SplineRiskMap chart:
//   1) Create a data source, usually a Java TableModel
//   2) Create a data model that uses the data source
//   3) Set up the desired risk map. This example uses the
//      SplineRiskMap class, which requires calculated
//      interpolation lines and colors to associate with
//      axis values
//   4) Construct a RiskMapPlot that uses the data model and risk map

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import com.sas.graphics.components.Variable;
import com.sas.graphics.components.PlotVariable;
import com.sas.graphics.components.ClassificationVariable;
import com.sas.graphics.components.SegmentedRangeModel;
import com.sas.graphics.components.riskmapplot.RiskMapPlot;
import com.sas.graphics.components.riskmapplot.RiskMapPlotTableDataModel;
import com.sas.graphics.components.riskmapplot.SplineRiskMap;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BasicSplineRiskMap extends JPanel {
  private BasicSplineRiskMap  theApp;
  private static final int POINTS_PER_LINE = 5;
  private static final double MIN_WEIGHT   = 25;
  private static final double MAX_WEIGHT   = 400;

  private static final double UNDERWEIGHT_LIMIT = 18.5;
  private static final double NORMAL_LIMIT      = 25;
  private static final double OVERWEIGHT_LIMIT  = 30;

  public BasicSplineRiskMap () {
      setLayout(new BorderLayout());

  // 1) Create a data source, usually a Java TableModel
     Data dataTable = new Data();  // defined below

  // 2) Construct a data model that uses the data source
     RiskMapPlotTableDataModel dataModel=
       new RiskMapPlotTableDataModel(dataTable);
     dataModel.setXVariable(new PlotVariable("Weight in pounds"));
     dataModel.setYVariable(new PlotVariable("Height in inches"));
     dataModel.setGroupVariable(new ClassificationVariable("Patient"));

//   3) Set up the desired risk map
     ArrayList<List> lines = new ArrayList<List>();
     lines.add(createLine(UNDERWEIGHT_LIMIT, "Underweight"));  // createLine() defined below
     lines.add(createLine(NORMAL_LIMIT, "Normal"));
     lines.add(createLine(OVERWEIGHT_LIMIT, "Overweight"));		

     SplineRiskMap splineRiskMap = new SplineRiskMap(lines, 
       new Color[]{Color.YELLOW,Color.ORANGE,Color.RED,Color.WHITE});

  // 4) Construct a RiskMapPlot that uses the data model and risk map
     RiskMapPlot riskMapPlot = new RiskMapPlot(dataModel);
     riskMapPlot.getGraphModel().setRiskMap(splineRiskMap);
     riskMapPlot.getGraphModel().setInterpolationEnabled(true);

     add(riskMapPlot, BorderLayout.CENTER);
  }
  // This method  will create a list of points that the SplineRiskMap will use to
  // create a spline that represents the boundary of a risk category.  The points
  // are calculated to represent a width and a height along the bmi curve.
  private List createLine(double bmiLimit, String category)
  {
     ArrayList<Point2D.Double> line = new ArrayList<Point2D.Double>();
     double weight = MIN_WEIGHT;
     double weightInc = (MAX_WEIGHT - MIN_WEIGHT) / (POINTS_PER_LINE-1);

     for (int i=0; i<POINTS_PER_LINE; i++)
     {
       double height = Math.sqrt(weight / (bmiLimit/703d));
       line.add(new Point2D.Double(weight, height));
       weight += weightInc;
     }
     return line;
  }
  private static class Data extends DefaultTableModel
  {
     private Class   columnClass[] = {Double.class, Double.class, String.class, String.class};
     private String  columnNames[] = {"Weight in pounds", "Height in inches", "Patient", "Year"};
     private Object  data[][]      = {
       {new Double(43),  new Double(48),   "Bob", "1975"},
       {new Double(85),  new Double(55),   "Bob", "1980"},
       {new Double(125), new Double(66),   "Bob", "1985"},
       {new Double(175), new Double(67),   "Bob", "1990"},
       {new Double(200), new Double(68),   "Bob", "1995"},
       {new Double(219), new Double(68.2), "Bob", "2000"},
       {new Double(250), new Double(68.2), "Bob", "2005"},

       {new Double(80),  new Double(43),   "Fred", "1975"},
       {new Double(90),  new Double(58),   "Fred", "1980"},
       {new Double(140), new Double(70),   "Fred", "1985"},
       {new Double(182), new Double(70),   "Fred", "1990"},
       {new Double(195), new Double(70),   "Fred", "2005"},

       {new Double(45),  new Double(42),   "Jack", "1975"},
       {new Double(135), new Double(54),   "Jack", "1980"},
       {new Double(147), new Double(63),   "Jack", "1985"},
       {new Double(160), new Double(73),   "Jack", "1990"},
       {new Double(170), new Double(75),   "Jack", "2000"},
       {new Double(185), new Double(75),   "Jack", "2005"}
     };
     public Data()
     {
       super();
       setDataVector(data, columnNames);
     }
     public Class getColumnClass(int column)
     {
       return columnClass[column];
     }
  }
  public static void main(String[] args) {
     JFrame frame = new JFrame("BIP Graph Sample");
     Container container = frame.getContentPane();
     container.setLayout(new BorderLayout());
     container.setBackground(Color.white);

     JPanel bipPanel = new JPanel();
     bipPanel.setLayout(new BorderLayout());
     BasicSplineRiskMap  bipGraphSample = new BasicSplineRiskMap();

     bipPanel.add(bipGraphSample, BorderLayout.CENTER);
     container.add(bipPanel, BorderLayout.CENTER);
     frame.setSize(450,600);
     frame.addWindowListener(
       new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
           System.exit(0);
         }
       }
     );
     frame.setVisible(true);
  }
}