// (c) Copyright 1997 Peter Sandborn ALL RIGHTS RESERVED

import java.awt.*;
import java.awt.image.*;
import java.lang.Math;


//------------------------------------------------------------------------------
// BarChart.java:
//		Creates a dialog box for displaying a bar chart
//------------------------------------------------------------------------------

public class BarChart extends Frame
{
	private SetupButton parent;
	private Button ok_button;
	private Button print_button;
	private Button help_button;
	private Panel titlePanel;
	private Label canvas;

    private int number_bars;
	private String x_axis;
	private String y_axis;
	private double[] bar_values;
	private String[] bar_labels;

	// Constructor
	//--------------------------------------------------------------------------
	public BarChart (SetupButton p,String topic)
	{
		super("Bar Chart");
		parent = p;

		// Title
		titlePanel = new Panel();
		Label label = new Label(topic);
		label.setFont(new Font("Helvetica",Font.BOLD,14));
		titlePanel.add(label);
		add("North",titlePanel);

		// Buttons
		Panel buttonPanel = new Panel();
		ok_button = new Button("OK");
		print_button = new Button("Print");
		help_button = new Button("Help");
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(ok_button);
		buttonPanel.add(print_button);
		buttonPanel.add(help_button);
		add("South",buttonPanel);

		print_button.disable();
		help_button.disable();
		reshape(0,0,150,200);  // does nothing
	}


    public void setCanvas(Label c)
    {
        canvas = c;
    }

    // n = number of bars
    // labels = array of bar labels
    // values = array of bar values
    // x = x axis label
    // y = y axis label
    public void loadBarData(int n,String[] labels,double[] values,String x,String y)
    {
        number_bars = n;
        x_axis = x;
		y_axis = y;

        bar_values = values;
        bar_labels = labels;
    }


    public boolean drawBarChart()
	{
	    this.setFont(new Font("Helvetica",Font.PLAIN,10));
		repaint();
		return true;
	}

	public void paint(Graphics g)
	{
	    Image im;
	    Graphics ig;
	    FilteredImageSource source;
	    Image si;
        FontMetrics fm = getFontMetrics(getFont());

		// Find highest bar and longest label
		double max = 0.0;
		int max_l = 0;
		for(int i = 0; i < number_bars; i++) {
		    if(bar_values[i] > max)
		        max = bar_values[i];
		    if(fm.stringWidth(bar_labels[i]) > max_l)
		        max_l = fm.stringWidth(bar_labels[i]);
		}

		// Get drawing area size
        int top_border;
		int bottom_border;
		int left_border;
		int right_border;
		if(System.getProperty("java.vendor").compareTo("Netscape Communications Corporation") == 0 && System.getProperty("os.name").compareTo("SunOS") == 0)  {
		    top_border = 70;
		    bottom_border = 10;
		}  else  {
		    top_border = 30;
		    bottom_border = 60;
        }
		int dim = 0;  // smallest drawing area dimension
		Dimension dimension = size();
		int x_dim = dimension.width;
		int y_dim = dimension.height-(top_border+bottom_border);
		dim = x_dim;
		if(dim > y_dim)  {
			dim = y_dim;
		}

        // Set offsets
        left_border = 35;
        right_border = 25;

		// Draw chart axes
		int bottom_axis = 3*(y_dim+top_border)/4;  // y location of horizontal axis
		int left_axis = left_border;  // x location of vertical axis
		int length_x_axis = x_dim - left_border - 25;
		int length_y_axis = bottom_axis - top_border;
		g.drawLine(left_axis,top_border,left_axis,bottom_axis);
		g.drawLine(left_axis,bottom_axis,left_axis+length_x_axis,bottom_axis);

		// Label axes
		int j = fm.stringWidth(x_axis);  // length of the x_axis label
		g.drawString(x_axis,left_border+length_x_axis/2-j/2,bottom_axis+max_l+2+2*fm.getHeight());
		im = createImage(fm.stringWidth(y_axis)+2,fm.getHeight());
		ig = im.getGraphics();
		ig.setColor(Color.white);
		ig.fillRect(0,0,100,100);
		ig.setColor(Color.black);
		ig.drawString(y_axis,1,10);
		source = new FilteredImageSource(im.getSource(),new Rotate90DegreeFilter());
		si = createImage(source);
		g.drawImage(si,12,top_border+length_y_axis/2-(fm.stringWidth(y_axis)+2)/2,null);

		// Plot bars
		g.setColor(Color.red);
		int bar_width = length_x_axis/number_bars - 4;
		for(int i = 0; i < number_bars; i++)  {
		    g.fillRect(i*(bar_width+4)+left_border+2,bottom_axis-(int)(length_y_axis*bar_values[i]/max),bar_width,(int)(length_y_axis*bar_values[i]/max));
		}

        // Label bars
        for(int i = 0; i < number_bars; i++)  {
            im = createImage(max_l+2,fm.getHeight());
		    ig = im.getGraphics();
		    ig.setColor(Color.white);
		    ig.fillRect(0,0,100,100);
		    ig.setColor(Color.black);
		    ig.drawString(bar_labels[i],1,10);
		    source = new FilteredImageSource(im.getSource(),new Rotate90DegreeFilter());
		    si = createImage(source);
		    g.drawImage(si,i*(bar_width+4)+left_border-fm.getHeight()/2,bottom_axis+3,null);
		    //g.drawImage(si,i*(bar_width+4)+left_border+2+(bar_width-fm.getHeight())/2,bottom_axis+3,null);

            // Add tick marks
            g.setColor(Color.black);
            g.drawLine(i*(bar_width+4)+left_border,bottom_axis,i*(bar_width+4)+left_border,bottom_axis+2);
	    }

	    // Add mean and standard deviation
	    g.setColor(Color.blue);
	    //Double z = new Double(data_member.doublevalue());
	    String mean_string = "Mean = "; // + z.toString();
	    g.drawString(mean_string,left_border + 2,top_border + fm.getHeight());
	    //z = new Double(data_member.stddev);
	    String stddev_string = "Standard Deviation = "; // + z.toString();
	    g.drawString(stddev_string,left_border+2,top_border + 2*fm.getHeight());

	}

    public boolean handleEvent(Event e) {
        if (e.id == Event.WINDOW_DESTROY && e.target == this) {
            dispose();
    		return true;
        }
        return super.handleEvent(e);
    }

	// Catch events in the dialog box
	//--------------------------------------------------------------------------
	public boolean action(Event event,Object obj)
	{
		Object target = event.target;

		if(target instanceof Button)
		{
			Button button = (Button)target;
			String buttonLabel = button.getLabel();
			if(target == ok_button)
			{
			    if(canvas != null)  {
    				//canvas.deselect(); // make sure it gets deselected
    				//canvas.setBackground(parent.sdacolor.panelbackground);
    				canvas.repaint();
    			}
				dispose();
			}
			if(target == print_button)
			{

			}
			if(target == help_button)
			{
			/*	HelpDialog helpdialog = null;
				String s = "The horizontal axis labels indicate the value between each bar.  " +
				           "You may need to stretch this window to obtain a better picture. ";
				helpdialog = new HelpDialog(parent,"Bar Chart",s,true);
				if(helpdialog != null)
					helpdialog.popup(); */
			} 
			return true;
		}
		return false;

	}

	public boolean keyDown (Event event,int key)
	{
		// Regular return (enter) key
		if(event.id == event.KEY_PRESS && key == '\n')  {
			hide();
		}
		return true;
	}

	public void popup()
	{
	    if(System.getProperty("java.vendor").compareTo("Netscape Communications Corporation") == 0 && System.getProperty("os.name").compareTo("SunOS") == 0)  {
    		show();
    		//resize(400,550);
    		resize(300,300);
    		refresh_dialog();
    		titlePanel.requestFocus();
    	}  else  {
    		show();
    		//resize(400,550);
    		resize(300,300);
    		titlePanel.requestFocus();
    	}
	}

	private void refresh_dialog()
	{
	    // Jitter the size of the form to force a refresh - necessary for NetScape

			// Get actuals here
			//Dimension dimension = size();
			int x = 400; //dimension.width;
			int y = 400; //dimension.height;

			// Resize
			resize(x+100,y+100);
			show();

			// Reset current size
			resize(x,y);

	}

}