com.church.tools.ChartTools.java Source code

Java tutorial

Introduction

Here is the source code for com.church.tools.ChartTools.java

Source

/*
 * Church Software
 *
 * Package : com.church.tools
 * Filename: ChartTools.java
 * Author  : Felix Chandrakumar <i@fc.id.au>
 * Website : www.churchsw.org
 * 
 * PUBLIC DOMAIN
 * 
 * As the Word of God says,
 * 
 * Freely you have received, freely give. - (Mat 10:8). For all things are from Jesus Christ,
 * by Jesus Christ, and for Jesus Christ. - (Romans 11:36)
 * 
 * Free means completely free! Church Software has no restrictions at all. The source code, 
 * binary and everything that is not licensed by others but included in Church Software bundle
 * is released to public domain and you can modify anything to fit your church needs. Public 
 * domain simply means everyone can use it without any restrictions.
 * 
 * WHY PUBLIC DOMAIN?
 * 
 * The manifestation of the Spirit is given to each one for the profit of all: for to one is
 * given the word of wisdom through the Spirit, to another the word of knowledge through the
 * same Spirit, to another faith by the same Spirit, to another gifts of healings by the same
 * Spirit, to another the working of miracles, to another prophecy, to another discerning of
 * spirits, to another different kinds of tongues, to another the interpretation of tongues.
 * But one and the same Spirit works all these things, distributing to each one individually
 * as He wills. - 1 Co 12:7-11
 * 
 * Woe unto them! for they have gone in the way of Cain, and ran greedily after the error of
 * Balaam for reward, and perished in the rebellion of Korah. - Jud 1:11
 * 
 * Jesus Christ gives word of knowledge through Holy Spirit for the profit of all. I dont
 * want to run greedily after the error of Balaam for reward. Balaam is a prophet of God who
 * went greedily to Balak. Unfortunately, many christian organizations, churches, bands, song
 * writers, musicians etc market the gifts of Holy Spirit, doing the same error of Balam for
 * reward. My Bible says, Woe to them and I don't want to be a part of them.
 */
package com.church.tools;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

/**
 * The Class ChartTools.
 */
public class ChartTools {

    /** The log. */
    Logger log = Logger.getLogger(this.getClass());

    /**
     * Generate line chart.
     * 
     * @param title the title
     * @param values the values
     * @param captions the captions
     * @param width the width
     * @param height the height
     * @param color the color
     * 
     * @return the buffered image
     */
    public static BufferedImage GenerateLineChart(String title, double[] values, String[] captions, int width,
            int height, Color color) {
        BufferedImage bufferedImage = null;
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < values.length; i++) {
            dataset.addValue(values[i], "Balance Timeline", String.valueOf(i + 1));
        }
        JFreeChart chart = ChartFactory.createLineChart(title, "Balance Timeline", "Amount", dataset,
                PlotOrientation.VERTICAL, false, false, false);
        chart.setBackgroundPaint(color);
        bufferedImage = chart.createBufferedImage(width, height);
        return bufferedImage;
    }

    /**
     * Generate pie chart.
     * 
     * @param title the title
     * @param values the values
     * @param captions the captions
     * @param width the width
     * @param height the height
     * @param color the color
     * 
     * @return the buffered image
     */
    public static BufferedImage GeneratePieChart(String title, double[] values, String[] captions, int width,
            int height, Color color) {
        BufferedImage bufferedImage = null;
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        Hashtable<String, String> ht = new Hashtable<String, String>();
        for (int i = 0; i < values.length; i++)
            ht.put(captions[i], Double.toString(values[i]));

        Enumeration<String> enu = ht.keys();
        int i = 0;
        while (enu.hasMoreElements()) {
            String str = (String) enu.nextElement();
            pieDataset.setValue(str, new Double(Double.parseDouble((String) ht.get(str))));
            i++;
        }

        JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false);
        chart.setBackgroundPaint(color);
        bufferedImage = chart.createBufferedImage(width, height);
        return bufferedImage;
    }
}