Source for org.jfree.chart.demo.PieChartDemo1

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------
  28:  * PieChartDemo1.java
  29:  * ------------------
  30:  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   ;
  34:  *
  35:  * $Id: PieChartDemo1.java,v 1.2.2.4 2006/10/25 10:38:47 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
  40:  *               the JFreeChart Developer Guide (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart.demo;
  45: 
  46: import java.awt.Font;
  47: 
  48: import javax.swing.JPanel;
  49: 
  50: import org.jfree.chart.ChartFactory;
  51: import org.jfree.chart.ChartPanel;
  52: import org.jfree.chart.JFreeChart;
  53: import org.jfree.chart.plot.PiePlot;
  54: import org.jfree.data.general.DefaultPieDataset;
  55: import org.jfree.data.general.PieDataset;
  56: import org.jfree.ui.ApplicationFrame;
  57: import org.jfree.ui.RefineryUtilities;
  58: 
  59: /**
  60:  * A simple demonstration application showing how to create a pie chart using 
  61:  * data from a {@link DefaultPieDataset}.
  62:  */
  63: public class PieChartDemo1 extends ApplicationFrame {
  64: 
  65:     /**
  66:      * Default constructor.
  67:      *
  68:      * @param title  the frame title.
  69:      */
  70:     public PieChartDemo1(String title) {
  71:         super(title);
  72:         setContentPane(createDemoPanel());
  73:     }
  74: 
  75:     /**
  76:      * Creates a sample dataset.
  77:      * 
  78:      * @return A sample dataset.
  79:      */
  80:     private static PieDataset createDataset() {
  81:         DefaultPieDataset dataset = new DefaultPieDataset();
  82:         dataset.setValue("One", new Double(43.2));
  83:         dataset.setValue("Two", new Double(10.0));
  84:         dataset.setValue("Three", new Double(27.5));
  85:         dataset.setValue("Four", new Double(17.5));
  86:         dataset.setValue("Five", new Double(11.0));
  87:         dataset.setValue("Six", new Double(19.4));
  88:         return dataset;        
  89:     }
  90:     
  91:     /**
  92:      * Creates a chart.
  93:      * 
  94:      * @param dataset  the dataset.
  95:      * 
  96:      * @return A chart.
  97:      */
  98:     private static JFreeChart createChart(PieDataset dataset) {
  99:         
 100:         JFreeChart chart = ChartFactory.createPieChart(
 101:             "Pie Chart Demo 1",  // chart title
 102:             dataset,             // data
 103:             true,               // include legend
 104:             true,
 105:             false
 106:         );
 107: 
 108:         PiePlot plot = (PiePlot) chart.getPlot();
 109:         plot.setSectionOutlinesVisible(false);
 110:         plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
 111:         plot.setNoDataMessage("No data available");
 112:         plot.setCircular(false);
 113:         plot.setLabelGap(0.02);
 114:         return chart;
 115:         
 116:     }
 117:     
 118:     /**
 119:      * Creates a panel for the demo (used by SuperDemo.java).
 120:      * 
 121:      * @return A panel.
 122:      */
 123:     public static JPanel createDemoPanel() {
 124:         JFreeChart chart = createChart(createDataset());
 125:         return new ChartPanel(chart);
 126:     }
 127:     
 128:     /**
 129:      * Starting point for the demonstration application.
 130:      *
 131:      * @param args  ignored.
 132:      */
 133:     public static void main(String[] args) {
 134: 
 135:         // ******************************************************************
 136:         //  More than 150 demo applications are included with the JFreeChart
 137:         //  Developer Guide...for more information, see:
 138:         //
 139:         //  >   http://www.object-refinery.com/jfreechart/guide.html
 140:         //
 141:         // ******************************************************************
 142:         
 143:         PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");
 144:         demo.pack();
 145:         RefineryUtilities.centerFrameOnScreen(demo);
 146:         demo.setVisible(true);
 147:     }
 148: 
 149: }