/*
 * -------------------------------------------------------------------------
 *      $Id: CDPanel.java,v 1.3 2004/10/14 16:19:46 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 2004 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */
/*
 *  CohenDDemo
 *  Copyright (c), 2003, Gary H. McClelland
 *  Shows relationship between values of the effect size measure Cohen's d
 *  and the amount of overlap, depicted both visually and numerically, of the
 *  normal distributions.  Most researchers are surprised by the amount of overlap
 *  in the normal distributions for commonly observed effect sizes.
 *
 *  User alters the value of Cohen's d by dragging in the graph.
*/

/**
 *  @author     Gary H. McClelland
 *  @created    5 October 2003
*/

package com.imsl.demo.stats;
import com.imsl.chart.*;
import com.imsl.stat.Cdf;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class CDPanel extends JPanel implements MouseMotionListener {

   private JFrame parentFrame;
   private JPanelChart jPanelChart;
   private JPanel jPanel;
   private JLabel jLabel;
    Chart chart;
    AxisXY axis;
    Data data, data2, line;

    private DecimalFormat df2 = new DecimalFormat("0.00");
    private DecimalFormat df1 = new DecimalFormat("0.0");

    private double CohenD = 3;
    private double[] current = new double[2];

    private double[] lineX = {3,3};
    private double[] lineY = {0,.04};

   public CDPanel(javax.swing.JFrame parent) {
      this.parentFrame = parent;

      initComponents();
      setPreferredSize(new java.awt.Dimension(parent.getSize().width, (int)(0.85*parent.getSize().height)));
      jPanelChart.setPreferredSize(new java.awt.Dimension(parent.getSize().width, (int)(0.66*parent.getSize().height)));

        jPanelChart.setChart(new Chart());
      chart = jPanelChart.getChart();
        axis = new AxisXY(chart);

        chart.getChartTitle().setFontSize(16);

        //prevent display of Y axis
        axis.getAxisY().setTextColor(getBackground());
        axis.getAxisY().setLineColor(getBackground());

        //use integer format for displaying tick values
        axis.getAxisX().setTextFormat("0");
        axis.getAxisX().setFirstTick(0);
        axis.getAxisX().setNumber(7);
        axis.getAxisX().setDensity(2);
        axis.getAxisX().setTickInterval(1);
        axis.getAxisX().setFontSize(16);
        axis.getAxisX().getAxisTitle().setTitle("d");

        //define standard normal density function
        ChartFunction norm = new ChartFunction() {
            public double f(double z) {
                return (1/Math.sqrt(2*Math.PI))*Math.pow(Math.E,-z*z/2);
            }
        };

        data = new Data(axis, norm, -4, 6);
        data.setDataType(Data.DATA_TYPE_FILL);
        data.setFillColor(new Color(.25f, .25f, 1.f,.5f));
        data.setFillOutlineColor(new Color(0.f, 0.f, 1.f, .5f));

        drawGraph();
    }

   private void initComponents() {
      jPanel = new JPanel();
      setLayout(new BorderLayout());

      jPanel.setLayout(new FlowLayout());
      jLabel = new JLabel("Drag the bar to change the value of d");
      jPanel.add(jLabel);
      add(jPanel, BorderLayout.NORTH);

      jPanelChart = new com.imsl.chart.JPanelChart();
      add(jPanelChart, BorderLayout.CENTER);

      jPanelChart.addMouseMotionListener(this);
   }

   //mouse drags determine the distance CohenD between the two distributions
    public void mouseDragged(MouseEvent me) {
        axis.mapDeviceToUser(me.getX(), me.getY(), current);

        //don't allow negative values of CohenD
        if(current[0] < 0)
            CohenD = 0;
        else if (current[0] > 5) //don't allow large values of CohenD
            CohenD = 5;
        else
            CohenD = current[0];

        drawGraph();
        repaint();
    }

    public void mouseMoved(MouseEvent me) {}

    private void drawGraph() {
        //overlap formulas from Cohen (1969, p. 21)
        double overlapP = Cdf.normal(CohenD/2);
        double percentOverlap = 100*(1-(2*overlapP - 1) / overlapP);

        chart.getChartTitle().setTitle("Cohen's d = "+ df2.format(CohenD)+"  Overlap = "+
            df1.format(percentOverlap)+"%");

        if(data2 != null) data2.remove();

        //define normal density function with standard deviation 1, offset by amount CohenD
        ChartFunction norm2 = new ChartFunction() {
            public double f(double z) {
                return (1/Math.sqrt(2*Math.PI))*Math.pow(Math.E,-(z-CohenD)*(z-CohenD)/2);
            }
        };

        data2 = new Data(axis, norm2, -4, 6);
        data2.setDataType(Data.DATA_TYPE_FILL);
        data2.setFillColor(new Color(1.f, .25f, .25f, .5f));
        data2.setFillOutlineColor(new Color(1.f, 0.f, 0.f, .5f));

        //create a marker to move along the axis to denote current value of CohenD
        lineX[0] = CohenD;
        lineX[1] = CohenD;
        lineY[0] = 0;
        lineY[1] = .1*(1/Math.sqrt(2*Math.PI));
        line = new Data(axis,lineX, lineY);
        line.setLineWidth(2.);
    }
}