com.pureinfo.srm.reports.impl.PieChartBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.reports.impl.PieChartBuilder.java

Source

/**
 * PureInfo Quake
 * @(#)PiePureChart.java   1.0 Sep 20, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm.reports.impl;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

import com.pureinfo.dolphin.model.DolphinObject;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.srm.reports.IChartBuilder;

/**
 * <P>
 * Created on Sep 20, 2005 3:35:21 PM <BR>
 * Last modified on Sep 20, 2005
 * </P>
 * 
 * @author Freeman.Hu
 * @version 1.0, Sep 20, 2005
 * @since Quake 1.0
 */
public class PieChartBuilder extends ChartBuilderBase implements IChartBuilder {
    public final static int TYPE_NUMBER = 0;

    public final static int TYPE_PERCENT = 1;

    private List m_datas;

    private int m_nType;

    private String m_sTitle;

    /**
     * Constructor
     *  
     */
    public PieChartBuilder(List _datas, int _nType, String _sTitle) {
        m_datas = _datas;
        m_nType = _nType;
        m_sTitle = _sTitle;
    }

    /**
     * @throws PureException
     * @see com.pureinfo.srm.reports.IChartBuilder#draw(java.util.List, int)
     */
    public JFreeChart buildChart() {
        Iterator result = m_datas.iterator();
        DefaultPieDataset ds = getDataset(result);

        JFreeChart jfreechart = ChartFactory.createPieChart3D(null, ds, false, false, false);
        jfreechart.setBackgroundPaint(Color.WHITE);

        PiePlot3D pieplot3d = (PiePlot3D) jfreechart.getPlot();
        pieplot3d.setOutlinePaint(Color.GRAY);
        pieplot3d.setOutlineStroke(new BasicStroke(1));
        pieplot3d.setStartAngle(30D);
        pieplot3d.setDepthFactor(0.04);
        pieplot3d.setDrawingSupplier(new DefaultDrawingSupplier(MyPaintMgr.getPaints(),
                DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));

        pieplot3d.setCircular(false);
        pieplot3d.setDirection(Rotation.CLOCKWISE);
        pieplot3d.setOutlinePaint(Color.WHITE);
        pieplot3d.setInteriorGap(.15);
        Color color = new Color(0xaa, 0xaa, 0xaa, 255);
        pieplot3d.setBaseSectionOutlinePaint(color);
        pieplot3d.setBaseSectionOutlineStroke(new BasicStroke(0));
        pieplot3d.setForegroundAlpha(0.6f);
        pieplot3d.setLabelLinkStroke(new BasicStroke(1));
        pieplot3d.setLabelOutlinePaint(new Color(0x777777));
        pieplot3d.setLabelBackgroundPaint(new Color(0xf1f1f7));
        pieplot3d.setLabelLinkPaint(new Color(0xaa, 0xaa, 0xaa, 60));
        pieplot3d.setNoDataMessage("No data to display");
        pieplot3d.setLabelShadowPaint(new Color(0xdddddd));
        pieplot3d.setLabelFont(new Font("", Font.PLAIN, 10));
        if (m_nType == TYPE_PERCENT) {

            pieplot3d.setLabelGenerator(new StandardPieItemLabelGenerator("{0} = {2}",
                    NumberFormat.getNumberInstance(), PERCENT_NUMBER_FORMAT));
        }

        fillChartInfo(ds);
        return jfreechart;
    }

    private void fillChartInfo(DefaultPieDataset _dataset) {
        int n = _dataset.getItemCount();
        m_ChartInfo = new ChartInfo();
        m_ChartInfo.setChartTitle(m_sTitle);
        NumberFormat format = NumberFormat.getNumberInstance();
        String[] labels = new String[n];
        for (int i = 0; i < n; i++) {
            labels[i] = "" + _dataset.getKey(i);
            labels[i] += " = ";
            labels[i] += format.format(_dataset.getValue(i));
            double totalValue = DatasetUtilities.calculatePieDatasetTotal(_dataset);
            if (totalValue != 0) {
                labels[i] += "  ";
                labels[i] += PERCENT_NUMBER_FORMAT.format(_dataset.getValue(i).doubleValue() / totalValue);
            }
        }
        m_ChartInfo.setLabels(labels);
        m_ChartInfo.setShowBoder(true);
        m_ChartInfo.setChartSize(ChartInfo.SIZE_WIDE_AND_THIN);
        m_ChartInfo.setLengedPosition(ChartInfo.LENGEND_POSITION_BUTTOM);
    }

    private static final NumberFormat PERCENT_NUMBER_FORMAT = new DecimalFormat("#,###.##%");

    private DefaultPieDataset getDataset(Iterator _result) {
        DefaultPieDataset ds = new DefaultPieDataset();
        while (_result.hasNext()) {
            DolphinObject obj = (DolphinObject) _result.next();
            String sName = obj.getStrProperty("name");
            double dblValue = obj.getDoubleProperty("value", 0.0);
            ds.setValue(sName, dblValue);
        }
        return ds;
    }
}