
/**
 * SensorPlotDialog.java
 * This dialog is used to plot sensor evaluation data
 *
 * Created: February 11, 2006
 *
 * @author Nathan Stevens
 * @version 0.12 8/9/06
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

// class the plots the sensor data
class SensorPlotDialog extends JDialog {
  private float[] wls = null; // stores the wavelengths to evalute sensors at
  private float[][] results1 = null;  // store the results from sensor calculation
  private float[][] results2 = null;  // store the results from sensor calculation
  private String[] cycles = null; // specifies what's being cycled
  
  public SensorPlotDialog(JFrame frame, float[] wls, float[][] results1, float[][] results2,
  String[] cycles) {
    super(frame, "Sensor Histogram", false);
    this.wls = wls;
    this.results1 = results1;
    this.results2 = results2;
    this.cycles = cycles;
    
    getContentPane().setLayout(new BorderLayout());
    
    // add the tab pane here
    JTabbedPane tabbedPane = new JTabbedPane();
    ChartPanel chartPanel = null;
    JFreeChart chart = null;
    
    // plot results 1
    if(results1 != null) {
      CategoryDataset dataset = getDataset1();
      
      chart = ChartFactory.createBarChart(null,
      "Sensor Cycle", "Normalized Area", dataset,
      PlotOrientation.VERTICAL,
      false,
      true,
      false);
      
      chart.setBackgroundPaint(Color.white);
      chart.setAntiAlias(false);
      CategoryPlot plot = (CategoryPlot) chart.getPlot();
      plot.setOutlinePaint(Color.black);
      
      chartPanel = new ChartPanel(chart);
      chartPanel.setDomainZoomable(true);
      chartPanel.setRangeZoomable(true);
      
      //getContentPane().add(chartPanel);
      tabbedPane.addTab("Integrated Intensity", null, chartPanel, "Integrated Intensity...");
      tabbedPane.setSelectedIndex(0);
    }
    
    // add the plots for various wavelenght
    for(int i = 0; i < wls.length; i++) {
      CategoryDataset dataset = getDataset2(i);
      
      chart = ChartFactory.createBarChart(null,
      "Sensor Cycle", "Relative Intensity", dataset,
      PlotOrientation.VERTICAL,
      false,
      true,
      false);
      
      chart.setBackgroundPaint(Color.white);
      chart.setAntiAlias(false);
      
      chartPanel = new ChartPanel(chart);
      chartPanel.setDomainZoomable(true);
      chartPanel.setRangeZoomable(true);
      
      //getContentPane().add(chartPanel);
      String title = "Intensity ("+ wls[i] + " nm)";
      tabbedPane.addTab(title, null, chartPanel, "Relative Intensity...");
    }
    getContentPane().add(tabbedPane, "Center");
    
    // set the size
    setSize(new Dimension(500,405));
  }
  
  // Method to return the dataset for results 1
  private CategoryDataset getDataset1() {
    DefaultCategoryDataset dataset = null;
    // row keys
    String series1 = cycles[0];
    String series2 = cycles[1];
    
    dataset = new DefaultCategoryDataset();
    
    // column keys
    int ccount = 1;
    for(int i = 0; i < results1.length; i += 2) {
      String cycle = "" + ccount;
      dataset.addValue(results1[i][1], series1, cycle);
      
      if(i + 1 != results1.length) {
        dataset.addValue(results1[i+1][1], series2, cycle);
      }
      
      ccount++;
    }
    return dataset;
  }
  
  // Method to return the dataset for results 2
  private CategoryDataset getDataset2(int wli) {
    DefaultCategoryDataset dataset = null;
    
    // row keys
    String series1 = cycles[0];
    String series2 = cycles[1];
    
    dataset = new DefaultCategoryDataset();
    
    // column keys
    int ccount = 1;
    for(int i = 0; i < results2.length; i += 2) {
      String cycle = "" + ccount;
      dataset.addValue(results2[i][wli], series1, cycle);
      
      if(i + 1 != results2.length) {
        dataset.addValue(results2[i+1][wli], series2, cycle);
      }
      
      ccount++;
    }
    return dataset;
  }
} //--end class sensor plot dialog
