// file: VGraph.java
//

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


/** 
 * This class displays a menu that will allow users to view the statistics
 *  for the walkins, appoinmenys and ailments and displays them to the
 *  screen in the report format. The queries are sent to the database. The 
 *  user enters the function for which statistics are required. The starting 
 *  and ending data for which these statistics are taken are also given.
 *  An extension of JPanel
 *
 */
class PieChart extends JPanel {

    /**
     * define the x position of the upper-left of the graph
     */
    int x_pos;

    /**
     * define the y position of the upper-left of the graph
     */
    int y_pos;
    
    /**
     * define the width of the graph
     */
    int width;

    /**
     * define the height of the graph
     */
    int height;

    /**
     * number of time slots
     */
    static int num_elem = 0;

    /**
     * number of colors
     */
    static final int num_colors = 17;

    /**
     * int values for the chart
     */
    int values[];

    /**
     * label values for the chart
     */
    String labels[];

    /**
     * color associated to each ailment to be represented in the graph
     */
    Color colors[];

    /**
     * sum of all the patients in the particular period
     */
    double total;
    
    /**
     * full width int value
     */
    int full_width;

    /**
     * full height int value
     */
    int full_height;

    private void defColors() {

	colors =  new Color[num_colors];

	// set the colors
	//
	int i = 0;
	float v0 = (float)0.6;
	float v1 = (float)0.3;
	float v2 = (float)1.0;
	
	colors[i++] = new Color(v0, v0, v2); // blue
	colors[i++] = new Color(v0, v1, v0); // dim purple
	colors[i++] = new Color(v0, v2, v0); // lime gello green
	colors[i++] = new Color(v2, v0, v1); // orange
	colors[i++] = new Color(v1, v0, v1); // forest green
	colors[i++] = new Color(v2, v1, v0); // pinkish

	colors[i++] = new Color(v0, v1, v2); // violet
	colors[i++] = new Color(v2, v2, v0); // dim yellow
	colors[i++] = new Color(v1, v0, v0); // dark aqua
	colors[i++] = new Color(v0, v2, v2); // cyan

	colors[i++] = new Color(v2, v1, v1); // bright red
	colors[i++] = new Color(v0, v2, v1); // lime green

	colors[i++] = new Color(v0, v1, v1); // dark purple/brown
	colors[i++] = new Color(v1, v0, v2); // bright blue
	colors[i++] = new Color(v0, v0, v1); // olive brown
	colors[i++] = new Color(v2, v0, v2); // magenta
	colors[i++] = new Color(v1, v1, v0); // dark dark blue
    }
	
    public PieChart (int x, int y, int w, int h, int n, 
		     int val[], String lab[]) {

	defColors();
	
	x_pos = x;
	y_pos = y;
	width = w;
	height = h;
	
	setData(n, val, lab);
	
	if (num_elem > num_colors) {
	    throw(new RuntimeException("too many elements"));
	}
	
	full_width = width + 250 + 2 * x_pos;
	full_height = height + 2 * (y_pos - height);
	Dimension size = new Dimension(full_width, full_height);

	setMinimumSize(size);
	setPreferredSize(size);
    }

    private void setData(int n, int val[], String[] lab) {
	
	num_elem = n;
	
	values = new int[num_elem];
	labels = new String[num_elem];

	for (int j = 0; j < num_elem; j+=1) {
	    values[j] = val[j];
	    labels[j] = lab[j] + " (" + val[j] + ")";
	}

	// insertion sort the data
	//
	for (int j = 1; j < num_elem; j++) {
	    int key = values[j];
	    String skey = labels[j];

	    // insert values[j] into the inserted sequence
	    //
	    int i = j - 1;
	    while ((i >= 0) && (values[i] < key)) {
		values[i+1] = values[i];
		labels[i+1] = labels[i];
		i--;
	    }
	    values[i+1] = key;
	    labels[i+1] = skey;
	}
	
	if (num_elem >= num_colors) {
	    
	    int count = 0;
	    
	    for (int j = num_colors-1; j < num_elem; j++) {
		count += values[j];
	    }
	    values[num_colors-1] = count;
	    labels[num_colors-1] = "Others ("+count+")";
	    num_elem = num_colors;
	}
	
	total = 0;
	
	// compute the total
	//
	for (int j = 0; j < num_elem; j++) {
	    total += values[j];
	}

    }	
    
    public void repaint(Graphics g2) {
	paint(g2);
    }

    public void paint (Graphics g2) {
 
	int i, y;
	double start_angle, arc_angle;
	start_angle = 90;
	y = y_pos - height;

	g2.clearRect(0, 0, full_width + 50, full_height+50);

	int step = height / (num_elem + 1);
	
	if (step < 10) {
	    throw (new RuntimeException("not enough space for legend"));
	}
	int legend_x = x_pos + width + x_pos * 3;
	int legend_y = y_pos - height + step;
	
	for (i = 0; i < num_elem; i++) {

	    // draw the portion of the pie chart
	    //
	    arc_angle = -(360.0 * values[i] / total);
	    g2.setColor(colors[i]);
	    g2.fillArc(x_pos, y, width, height, (int)start_angle, 
		       (int)arc_angle);
	    start_angle += arc_angle;

	    // draw the color box for the legend
	    //
	    g2.fillRect(legend_x, legend_y, 10, 10);
	    
	    // draw the text for the legend
	    // 
	    g2.setColor(Color.black);
	    g2.drawString(labels[i], legend_x + 20, legend_y + 10);
	    legend_y += step;
	}
    }

    /**
     * Used to instantiate class    
     */
    public static void main(String args[]) {
	
	int[] vals = new int[6];
	String[] labs = new String[6];
	vals[0] = 25; labs[0] = "label_0";
	vals[1] = 82; labs[1] = "label_1";
	vals[2] = 76; labs[2] = "label_2";
	vals[3] = 11; labs[3] = "label_3";
	vals[4] = 55; labs[4] = "label_4";
	vals[5] = 21; labs[5] = "label_5";
	
	/*	num_elem = 17;
	
	int[] vals = new int[num_elem];
	String[] labels = new String[num_elem];
	for(int i = 0; i < num_elem; i++) {
	    vals[i] = 10 * (20 - i) * (20 - i);
	    labels[i] = "label_"+i;
	}
	*/
	num_elem = 6;

	JFrame f = new JFrame();
	
	f.setSize(600,600);
	
	int dim = 300;
	int offset = 20;

	PieChart p = new PieChart(offset, dim + offset, dim, dim, 6, vals, labs);
	
	f.getContentPane().add(p);
	
	f.setVisible(true);
    }
}	


