<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 *   Copyright (c) 2008 by Richard Koolish. 
 *   All Rights Reserved.
 *
 */


/*********************************************************************
*                                                                    * 
*  PieChart - Create a JPG image of a pie chart                      * 
*                                                                    * 
*********************************************************************/  

import java.util.*;
import java.io.*;
import java.awt.*;


public class PieChart  {

    private static boolean verbose = false;
    private static String source = null;
    private static String dest = null;
    private static String indir = null;
    private static String outdir = null;
    private static String usage = "usage: java Piechart -s &lt;source&gt; -d &lt;dest&gt; -indir &lt;dir&gt; -outdir &lt;dir&gt;";
    private static int canvas_w = 1024;
    private static int canvas_h = 1024;
    private static Hashtable colors;


    private static void parseArgs(String[] args) {

        for (int i = 0; i &lt; args.length; i++) {
	    String arg = args[i];

	    if (arg.equals("-v")) {
		verbose = true;
            } else if (arg.equals ("-s")) {
                i++;
                source = args[i];
            } else if (arg.equals ("-d")) {
                i++;
                dest = args[i];
            } else if (arg.equals ("-indir")) {
                i++;
                indir = args[i];
            } else if (arg.equals ("-outdir")) {
                i++;
                outdir = args[i];
            } else if (arg.equals ("-w")) {
                i++;
                canvas_w = Integer.parseInt(args[i]);
            } else if (arg.equals ("-h")) {
                i++;
                canvas_h = Integer.parseInt(args[i]);
            } else {
                System.out.println ("Unknown arg: " + arg);
            }
	  } 

        if (source == null) {
            System.err.println ("No source file specified");
            System.err.println (usage);
            System.exit (-1);
        }

        if (dest == null) {
            System.err.println ("No destination file specified");
            System.err.println (usage);
            System.exit (-1);
        }

        if (indir == null) {
	    indir = ".";
        }

        if (outdir == null) {
	    outdir = ".";
        }
    }


    public static void main (String[] args) { 

        colors = new Hashtable ();
        colors.put ("black", Color.black);
        colors.put ("red", Color.red);
        colors.put ("blue", Color.blue);
        colors.put ("green", Color.green);
	colors.put ("magenta", Color.magenta);
	colors.put ("cyan", Color.cyan);
	colors.put ("yellow", Color.yellow);
	colors.put ("gray", Color.gray);
	colors.put ("pink", Color.pink);
	colors.put ("orange", Color.orange);

        parseArgs (args);

        process_file (source, dest);

        System.exit (0);
    }

    
    private static void process_file (String in_filename, String out_filename) {

        BufferedReader in;
        String line;
        String new_line;

        System.err.println ("processing '" + in_filename + "'");

        try {
            in = new BufferedReader (new FileReader (indir + "/" + in_filename));
        } catch (Exception e) {
            System.err.println ("Can't open input file '" + indir + "/" + in_filename + "'");
            e.toString ();
            return;
        }

        BufferedImageCanvas canvas = new BufferedImageCanvas (canvas_w, canvas_h);

	Double startAng = 270.0;
	Double used = 0.0;
	int center_x = canvas_w / 2;
	int center_y = canvas_h / 2;
	int radius = (int)((double)(canvas_w / 2) * .75);
	canvas.drawCircle (center_x, center_y, radius, 1, Color.black);

        while (true) {
            try {
                line = in.readLine ();
                if (line == null)
                    break;
		if (verbose)
		    System.err.println("line: " + line);
		line = line.trim();
                if (line.length() == 0)
                    continue;
                if (line.startsWith ("#"))
                    continue;
                Vector v = tokenize (line, null);
                if (v.size() &lt; 2)
                    continue;
                double percent = Double.parseDouble ((String)v.elementAt (0));
		if (percent &lt;= 0.0)
		    percent = 100.0 - used;
		used += percent;
		double ang = (percent / 100.0) * 360.0;
                String color = (String)v.elementAt (1);
                Color c = (Color)colors.get (color);
                if (c == null)
                    c = Color.gray;
                canvas.fillArc (center_x, center_y, radius, -(int)Math.round(startAng), 
				-(int)Math.round(ang), c);
		startAng += ang;
            } catch (Exception e) {
                e.printStackTrace ();
                break;
            }
        }

        canvas.write (outdir + "/" + out_filename);
    }


    // tokenize

    private static Vector tokenize (String s, String delim) {

        StringTokenizer st;
        if (delim == null)
            st = new StringTokenizer (s);
        else
            st = new StringTokenizer (s, delim);

        Vector tokens = new Vector ();
        while (st.hasMoreTokens ())        
            tokens.add (st.nextToken ());

        return tokens;
    }


}
   </pre></body></html>