charts.PieChart3D.java Source code

Java tutorial

Introduction

Here is the source code for charts.PieChart3D.java

Source

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 *
 * ------------------
 * PieChartchart1.java
 * ------------------
 * (C) Copyright 2003-2014, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   ;
 *
 * Changes
 * -------
 * 09-Mar-2005 : Version 1, copied from the chart collection that ships with
 *               the JFreeChart Developer Guide (DG);
 *
 */

package charts;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.Toolkit;
import java.awt.geom.Point2D;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;

import model.PieChartModel;

/**
 * A simple chartnstration application showing how to create a pie chart using
 * data from a {@link DefaultPieDataset}.
 */
public class PieChart3D extends ApplicationFrame {

    private static final long serialVersionUID = 1L;

    private static PieChart3D chart = null;

    static {
        // set a theme using the new shadow generator feature available in
        // 1.0.14 - for backwards compatibility it is not enabled by default
        ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", true));
    }

    /**
     * Default constructor.
     *
     * @param title
     *            the frame title.
     */
    public PieChart3D(PieChartModel model) {
        super(model.getTitle());
        setContentPane(createchartPanel(model));
    }

    /**
     * Creates a sample dataset.
     * 
     * Source: http://www.bbc.co.uk/news/business-15489523
     *
     * @return A sample dataset.
     */
    private static PieDataset createDataset(PieChartModel model) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        if (model.getDataList() == null || model.getDataList().isEmpty()) {
            dataset.setValue("FCH", new Double(400));
            dataset.setValue("FCA", new Double(1500));
            dataset.setValue("FCG", new Double(204));
            dataset.setValue("FCS", new Double(430));
            dataset.setValue("FCJ", new Double(40));
        } else {
            model.getDataList().forEach(data -> dataset.setValue(data.getKey(), data.getValue()));
        }
        return dataset;
    }

    /**
     * Creates a chart.
     *
     * @param dataset
     *            the dataset.
     *
     * @return A chart.
     */
    private static JFreeChart createChart(PieDataset dataset, PieChartModel model) {

        JFreeChart chart = ChartFactory.createPieChart(model.getTitle(), // chart
                // title
                dataset, // data
                false, // no legend
                true, // tooltips
                false // no URL generation
        );

        // set a custom background for the chart
        chart.setBackgroundPaint(
                new GradientPaint(new Point(0, 0), new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));

        // customise the title position and font
        TextTitle t = chart.getTitle();
        t.setHorizontalAlignment(HorizontalAlignment.LEFT);
        t.setPaint(new Color(240, 240, 240));
        t.setFont(new Font("Arial", Font.BOLD, 26));

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setBackgroundPaint(null);
        plot.setInteriorGap(0.04);
        plot.setOutlineVisible(false);

        // use gradients and white borders for the section colours
        plot.setSectionPaint("FCA", createGradientPaint(new Color(200, 200, 255), Color.BLUE));
        plot.setSectionPaint("FCH", createGradientPaint(new Color(255, 200, 200), Color.RED));
        plot.setSectionPaint("FCS", createGradientPaint(new Color(200, 255, 200), Color.GREEN));
        plot.setSectionPaint("FCG", createGradientPaint(new Color(200, 255, 200), Color.YELLOW));
        plot.setSectionPaint("FCJ", createGradientPaint(new Color(200, 255, 200), Color.BLACK));
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} ({2}) "));
        plot.setBaseSectionOutlinePaint(Color.WHITE);
        plot.setSectionOutlinesVisible(true);
        plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f));

        // customise the section label appearance
        plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
        plot.setLabelLinkPaint(Color.WHITE);
        plot.setLabelLinkStroke(new BasicStroke(2.0f));
        plot.setLabelOutlineStroke(null);
        plot.setLabelPaint(Color.WHITE);
        plot.setLabelBackgroundPaint(null);

        // add a subtitle giving the data source
        TextTitle source = new TextTitle(model.getSubTitle(), new Font("Courier New", Font.PLAIN, 12));
        source.setPaint(Color.WHITE);
        source.setPosition(RectangleEdge.BOTTOM);
        source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        chart.addSubtitle(source);
        return chart;

    }

    /**
     * A utility method for creating gradient paints.
     * 
     * @param c1
     *            color 1.
     * @param c2
     *            color 2.
     * 
     * @return A radial gradient paint.
     */
    private static RadialGradientPaint createGradientPaint(Color c1, Color c2) {
        Point2D center = new Point2D.Float(0, 0);
        float radius = 200;
        float[] dist = { 0.0f, 1.0f };
        return new RadialGradientPaint(center, radius, dist, new Color[] { c1, c2 });
    }

    /**
     * Creates a panel for the chart (used by Superchart.java).
     *
     * @return A panel.
     */
    public static JPanel createchartPanel(PieChartModel model) {
        JFreeChart chart = createChart(createDataset(model), model);
        chart.setPadding(new RectangleInsets(20, 8, 2, 2));
        ChartPanel panel = new ChartPanel(chart);
        panel.setMouseWheelEnabled(true);
        panel.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
        return panel;
    }

    /**
     * Starting point for the chartnstration application.
     *
     * @param args
     *            ignored.
     */
    public static void createPieChart(PieChartModel model) {
        chart = new PieChart3D(model);
        chart.pack();
        RefineryUtilities.centerFrameOnScreen(chart);
        chart.setVisible(true);

    }

    public static void closeChart() {
        if (chart != null) {
            chart.setVisible(false);
        }
    }
}