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-2007, 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-2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   ;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
  38:  *               the JFreeChart Developer Guide (DG);
  39:  *
  40:  */
  41: 
  42: package org.jfree.chart.demo;
  43: 
  44: import java.awt.Font;
  45: 
  46: import javax.swing.JPanel;
  47: 
  48: import org.jfree.chart.ChartFactory;
  49: import org.jfree.chart.ChartPanel;
  50: import org.jfree.chart.JFreeChart;
  51: import org.jfree.chart.plot.PiePlot;
  52: import org.jfree.data.general.DefaultPieDataset;
  53: import org.jfree.data.general.PieDataset;
  54: import org.jfree.ui.ApplicationFrame;
  55: import org.jfree.ui.RefineryUtilities;
  56: 
  57: /**
  58:  * A simple demonstration application showing how to create a pie chart using
  59:  * data from a {@link DefaultPieDataset}.
  60:  */
  61: public class PieChartDemo1 extends ApplicationFrame {
  62: 
  63:     /**
  64:      * Default constructor.
  65:      *
  66:      * @param title  the frame title.
  67:      */
  68:     public PieChartDemo1(String title) {
  69:         super(title);
  70:         setContentPane(createDemoPanel());
  71:     }
  72: 
  73:     /**
  74:      * Creates a sample dataset.
  75:      *
  76:      * @return A sample dataset.
  77:      */
  78:     private static PieDataset createDataset() {
  79:         DefaultPieDataset dataset = new DefaultPieDataset();
  80:         dataset.setValue("One", new Double(43.2));
  81:         dataset.setValue("Two", new Double(10.0));
  82:         dataset.setValue("Three", new Double(27.5));
  83:         dataset.setValue("Four", new Double(17.5));
  84:         dataset.setValue("Five", new Double(11.0));
  85:         dataset.setValue("Six", new Double(19.4));
  86:         return dataset;
  87:     }
  88: 
  89:     /**
  90:      * Creates a chart.
  91:      *
  92:      * @param dataset  the dataset.
  93:      *
  94:      * @return A chart.
  95:      */
  96:     private static JFreeChart createChart(PieDataset dataset) {
  97: 
  98:         JFreeChart chart = ChartFactory.createPieChart(
  99:             "Pie Chart Demo 1",  // chart title
 100:             dataset,             // data
 101:             true,               // include legend
 102:             true,
 103:             false
 104:         );
 105: 
 106:         PiePlot plot = (PiePlot) chart.getPlot();
 107:         plot.setSectionOutlinesVisible(false);
 108:         plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
 109:         plot.setNoDataMessage("No data available");
 110:         plot.setSimpleLabels(true);
 111: 
 112:         return chart;
 113: 
 114:     }
 115: 
 116:     /**
 117:      * Creates a panel for the demo (used by SuperDemo.java).
 118:      *
 119:      * @return A panel.
 120:      */
 121:     public static JPanel createDemoPanel() {
 122:         JFreeChart chart = createChart(createDataset());
 123:         return new ChartPanel(chart);
 124:     }
 125: 
 126:     /**
 127:      * Starting point for the demonstration application.
 128:      *
 129:      * @param args  ignored.
 130:      */
 131:     public static void main(String[] args) {
 132: 
 133:         // ******************************************************************
 134:         //  More than 150 demo applications are included with the JFreeChart
 135:         //  Developer Guide...for more information, see:
 136:         //
 137:         //  >   http://www.object-refinery.com/jfreechart/guide.html
 138:         //
 139:         // ******************************************************************
 140: 
 141:         PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");
 142:         demo.pack();
 143:         RefineryUtilities.centerFrameOnScreen(demo);
 144:         demo.setVisible(true);
 145:     }
 146: 
 147: }