/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.plot.ui;

import com.approximatrix.charting.DefaultChart;
import com.approximatrix.charting.Title;
import com.approximatrix.charting.model.ObjectChartDataModel;
import com.approximatrix.charting.render.BarChartRenderer;
import com.approximatrix.charting.swing.ExtendedChartPanel;

import javax.swing.JDialog;

public class HistogramPlot extends GenericPlotInterface {
	
	public HistogramPlot(Object[][] data) {
		String[] bins = new String[data.length];
		int[] frequency = new int[data.length];
		
		for(int i=0; i< data.length;i++) {
			if(data[i].length < 2) continue;
			
			// This should be a String, but call toString just in case
			if(data[i][0] instanceof String)
				bins[i] = (String)data[i][0];
			else if(data[i][0] instanceof Number)
				bins[i] = ((Number)data[i][0]).toString();
			else
				bins[i] = data[i][0].toString();
			
			// This should be a Number class at the very least
			if(data[i][1] == null) continue;
			if(data[i][1] instanceof Number)
				frequency[i] = ((Number)data[i][1]).intValue();
		}
		
		this.constructPlot(bins,frequency);
	}
	
	public HistogramPlot(String[] bins, int[] frequency) {
		
		this.constructPlot(bins,frequency);
		
		//model.addChartDataModelListener(this);
		//model.setAutoScale(true);
		
	}
	
	public HistogramPlot(String[] bins, int[] frequency, String title) {
		
		this.constructPlot(bins,frequency);
		this.panel.setTitle(new Title(title));
		
		//model.addChartDataModelListener(this);
		//model.setAutoScale(true);
		
	}
	
	public HistogramPlot(String variable, String[] bins, int[] frequency) {
		
		this(bins, frequency);
		panel.setTitle(new com.approximatrix.charting.Title(variable));
		
	}
	
	private void constructPlot(String[] bins, int[] frequency) {
		
		Integer[][] data = new Integer[1][frequency.length];
		for(int i=0;i<frequency.length;i++)
			data[0][i] = new Integer(frequency[i]);
		
		String[] row = new String[1];
		row[0] = "Frequency";
		
		ObjectChartDataModel model = new ObjectChartDataModel(data,bins,row);
		
		panel = new ExtPrintableChartPanel(model, "Histogram", DefaultChart.LINEAR_X_LINEAR_Y);
		panel.addChartRenderer(new BarChartRenderer(panel.getCoordSystem(),model),1);
		panel.setLegend(null);
		panel.getCoordSystem().setXAxisUnit("Bin");
		panel.getCoordSystem().setYAxisUnit("Frequency");
		
	}

}
