package jminhep.gui;

import java.awt.*;

import java.awt.Color;
import javax.swing.*;

import jminhep.cluster.*;

import java.util.ArrayList;

import org.jfree.ui.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.annotations.*;
import org.jfree.data.xy.*;
import org.jfree.data.contour.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.axis.*;

/**
 * Interface to JFreeChart
 * 
 * @author S.Chekanov Aug 20, 2006
 * 
 */

public class Plotter extends JPanel {

	public static final long serialVersionUID = 11214;

	private  JFreeChart chart;

	private   XYSeriesCollection dataset;

	private XYPlot xyplot;

	private  ChartPanel chartPanel;

	private  DefaultContourDataset dataset2D;
//	private  XYSeriesCollection dataset2D;
	
	public DataHolder Ndata;

	public  int iiX;

	public  int iiY;

	private  double pwidth = 1.0;

	private  double phight = 1.0;

	/** Array of data points */
	private ArrayList Centers;

	private ArrayList Seeds;

	// for plotter
	public  int MarkSize = 4;

	public  Color MarkColor = Color.blue;

	public  boolean MarkSelect = false;

	public  Color MarkSelectColor = new Color(0x00aaaa);

	public Plotter() {

		this.setLayout(new BorderLayout());
		dataset = new XYSeriesCollection();

		this.Centers = new ArrayList();
		this.Seeds = new ArrayList();

		chart = ChartFactory.createScatterPlot("Empty", // title
				"", "", dataset, // dataset
				PlotOrientation.VERTICAL, true, // legend? yes
				true, // tooltips? yes
				false // URLs? no
				);// axis
		// dataset = null;
		// xyplot = null;
          ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
          ChartUtilities.applyCurrentTheme(chart);
		// chart.setBackgroundPaint(Color.gray);
		chartPanel = new ChartPanel(chart);
		// chartPanel.setMouseZoomable(true, false);
		// chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		chartPanel.setLayout(new BorderLayout());

		this.add(chartPanel);
		this.setVisible(true);

	}

	/**
	 * Clear data sets
	 * 
	 */
	public void clearData() {

		if (dataset != null) {
			if (dataset.getSeriesCount() > 0)
				dataset.removeAllSeries();
		}

		if (dataset2D != null) 
			if (dataset2D.getSeriesCount()>0) {
				dataset2D=null;
			}
		
		
	}

	
	/**
	 * Get JFreeChart
	 * 
	 * @return
	 */
	public JFreeChart getChart(){
		
		return chart;
		
	}
	
	/**
	 * Get chart panel
	 * 
	 * @return
	 */
	public ChartPanel getChartPanel(){
		
		return chartPanel;
		
	}
	/**
	 * Clear plot (final)
	 * 
	 */
	public void clearPlot() {

		if (dataset != null) {
			if (dataset.getSeriesCount() > 0)
				dataset.removeAllSeries();
		}

		
		if (dataset2D != null) 
			if (dataset2D.getSeriesCount()>0) {
				dataset2D=null;
			}
			

		
		if (chart != null) {
			chart.setTitle("Not loaded");
			chart.setBackgroundPaint(Color.gray);

			xyplot = chart.getXYPlot();
			ValueAxis xx = xyplot.getDomainAxis();
			ValueAxis yy = xyplot.getRangeAxis();
			xx.setLabel("");
			yy.setLabel("");
		}

	}

	/**
	 * draw all points for the DataHolder and raw iX and iY
	 * 
	 */
	public void drawPoints() {

		// System.out.println("Draw points");
		clearData();
		XYSeries SerData = new XYSeries("Data");

		for (int i = 0; i < SetEnv.NRow; i++) {
			DataPoint dp = SetEnv.DATA.getRow(i);
			double x = dp.getAttribute(SetEnv.JboxX);
			double y = dp.getAttribute(SetEnv.JboxY);
			SerData.add(x, y);
		}

                try {
		dataset.addSeries(SerData);
                } catch (IllegalArgumentException e) {
                }

		chart = ChartFactory.createScatterPlot(SetEnv.Mtitle, // title
				SetEnv.XYtitle[SetEnv.JboxX], SetEnv.XYtitle[SetEnv.JboxY], // axis
				// labels
				dataset, // dataset
				PlotOrientation.VERTICAL, true, // legend? yes
				true, // tooltips? yes
				false // URLs? no
				);
		xyplot = chart.getXYPlot();
		xyplot.getRenderer().setSeriesPaint(0, Color.black);
		chart.setBackgroundPaint(Color.white);
		chartPanel.setChart(chart);
		chartPanel.setDomainZoomable(true);
		chartPanel.setRangeZoomable(true);
		pwidth = 10;
		phight = 20;

		ValueAxis xx = xyplot.getDomainAxis();
		ValueAxis yy = xyplot.getRangeAxis();
		pwidth = xx.getRange().getLength();
		phight = yy.getRange().getLength();
		// System.out.println(pwidth);
		// System.out.println(phight);

	}

	public void drawContour() {

		clearData();
		chart = null;

		int BinX = 30; // number of bins (excluding underflow and overflow
		// bins)
		int BinY = 30; // number of bins (excluding underflow and overflow
		// bins)

		Double Mi[] = new Double[SetEnv.Dim];
		Double Ma[] = new Double[SetEnv.Dim];

		for (int m = 0; m < SetEnv.Dim; m++) {
			Mi[m] = SetEnv.Min.getAttribute(m);
			Ma[m] = SetEnv.Max.getAttribute(m);
		}

		double MinX = Mi[SetEnv.JboxX]; // and the lower edge of bin i
		double MinY = Mi[SetEnv.JboxY]; // and the lower edge of bin i

		double MaxX = Ma[SetEnv.JboxX]; // and the lower edge of bin i
		double MaxY = Ma[SetEnv.JboxY]; // and the lower edge of bin i

		double BinWidthX = (MaxX - MinX) / (double) BinX; // and its width
		double BinWidthY = (MaxY - MinY) / (double) BinY; // and its width

		int AllMax = BinX * BinY;
		Double xValues[] = new Double[AllMax];
		Double yValues[] = new Double[AllMax];
		Double zValues[] = new Double[AllMax];

		int nn = 0;
		for (int i = 0; i < BinX; i++) {
			for (int j = 0; j < BinY; j++) {

				double dX = MinX + BinWidthX * i;
				double dY = MinY + BinWidthY * j;

				xValues[nn] = dX + 0.5 * BinWidthX;
				yValues[nn] = dY + 0.5 * BinWidthY;

				int kk = 0;
				for (int m = 0; m < SetEnv.NRow; m++) {
					DataPoint dp = SetEnv.DATA.getRow(m);
					double x = dp.getAttribute(SetEnv.JboxX);
					double y = dp.getAttribute(SetEnv.JboxY);
					if (x > dX && x < dX + BinWidthX && y > dY
							&& y < dY + BinWidthY)
						kk++;
				}
				zValues[nn] = (double) kk;
				nn++;
			}
		}

		// for (int m = 0; m 

Ads help maintain this website.