com.datamyne.charts.UglyDemo.java Source code

Java tutorial

Introduction

Here is the source code for com.datamyne.charts.UglyDemo.java

Source

package com.datamyne.charts;

/**
 * Copyright 2010 Jee Vang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Ugly demo of how to use jFreeChart
 * with iText. The output will have
 * the stair-case effect.
 * 
 * @author Jee Vang
 *
 */
public class UglyDemo {

    /**
     * Gets a {@link JFreeChart}.
     * @return {@link JFreeChart}.
     */
    public JFreeChart getChart() {
        //create dummy data
        //taken from http://www.java2s.com/Code/Java/Chart/JFreeChartPieChartDemo1.htm
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));

        //use the ChartFactory to create a pie chart
        JFreeChart chart = ChartFactory.createPieChart("Dummy Data", dataset, true, true, false);
        return chart;
    }

    /**
     * Creates PDf file.
     * @param outputStream {@link OutputStream}.
     * @throws DocumentException
     * @throws IOException
     */
    public void create(OutputStream outputStream) throws DocumentException, IOException {
        Document document = null;
        PdfWriter writer = null;

        try {
            //instantiate document and writer
            document = new Document();
            writer = PdfWriter.getInstance(document, outputStream);

            //open document
            document.open();

            //add image
            int width = 300;
            int height = 300;
            JFreeChart chart = getChart();
            BufferedImage bufferedImage = chart.createBufferedImage(width, height);
            Image image = Image.getInstance(writer, bufferedImage, 1.0f);
            document.add(image);

            //release resources
            document.close();
            document = null;

            writer.close();
            writer = null;
        } catch (DocumentException de) {
            throw de;
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            //release resources
            if (null != document) {
                try {
                    document.close();
                } catch (Exception ex) {
                }
            }

            if (null != writer) {
                try {
                    writer.close();
                } catch (Exception ex) {
                }
            }
        }
    }

    /**
     * Main method.
     * @param args No args required.
     * @throws FileNotFoundException
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
        (new UglyDemo()).create(new FileOutputStream(new File("/storage/tradeFiles/ugly-demo.pdf")));
    }
}