/*
 * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
 * Published By SunSoft Press/Prentice-Hall
 * Copyright (C) 1996 Sun Microsystems Inc.
 * All Rights Reserved. ISBN 0-13-565755-5
 *
 * Permission to use, copy, modify, and distribute this 
 * software and its documentation for NON-COMMERCIAL purposes
 * and without fee is hereby granted provided that this 
 * copyright notice appears in all copies. 
 * 
 * THE AUTHORS AND PUBLISHER MAKE 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. THE AUTHORS
 * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
 
/**
 * @version 1.00 07 Feb 1996 
 * @author Cay Horstmann
 */

import java.awt.*;
import java.applet.*;
import java.io.*;
import corejava.*;

public class Chart extends Applet 
{  public void init() 
   {  int n = Format.atoi(getParameter("values"));
      values = new double[n];
      names = new String[n];
      title = getParameter("title");
      int i;
      for (i = 0; i < n; i++)
      {  values[i] 
            = Format.atof(getParameter("value_" + (i + 1)));
         names[i] = getParameter("name_" + (i + 1));
      }
   }      
   
   public void paint(Graphics g)
   {  int i;
      int n = Format.atoi(getParameter("values"));
      double minValue = 0;
      double maxValue = 0;
      for (i = 0; i < values.length; i++)
      {  if (minValue > values[i]) minValue = values[i];
         if (maxValue < values[i]) maxValue = values[i];
      }
            
      Dimension d = size();
      int clientWidth = d.width;
      int clientHeight = d.height;
      int barWidth = clientWidth / n;
      
      Font titleFont = new Font("Helvetica", Font.BOLD, 20);
      FontMetrics titleFontMetrics 
         = g.getFontMetrics(titleFont);
      Font labelFont = new Font("Helvetica", Font.PLAIN, 10);
      FontMetrics labelFontMetrics 
         = g.getFontMetrics(labelFont);
         
      int titleWidth = titleFontMetrics.stringWidth(title);
      int y = titleFontMetrics.getAscent();
      int x = (clientWidth - titleWidth) / 2;
      g.setFont(titleFont);
      g.drawString(title, x, y);
      
      int top = titleFontMetrics.getHeight();
      int bottom = labelFontMetrics.getHeight();
      if (maxValue == minValue) return;
      double scale = (clientHeight - top - bottom) 
         / (maxValue - minValue);
      y = clientHeight - labelFontMetrics.getDescent();
      g.setFont(labelFont);
      
      for (i = 0; i < n; i++)
      {  int x1 = i * barWidth + 1;
         int y1 = top;
         int height = (int)(values[i] * scale);
         if (values[i] >= 0)
            y1 += (int)((maxValue - values[i]) * scale);
         else
         {  y1 += (int)(maxValue * scale);
            height = -height;
         }
         
         g.setColor(Color.red);
         g.fillRect(x1, y1, barWidth - 2, height);         
         g.setColor(Color.black);
         g.drawRect(x1, y1, barWidth - 2, height);
         int labelWidth 
            = labelFontMetrics.stringWidth(names[i]);
         x = i * barWidth + (barWidth - labelWidth) / 2;
         g.drawString(names[i], x, y);
      }
   }

   private double values[];
   private String names[];
   private String title;
}
