001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------ 028 * PieChartDemo1.java 029 * ------------------ 030 * (C) Copyright 2003-2014, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): ; 034 * 035 * Changes 036 * ------- 037 * 09-Mar-2005 : Version 1, copied from the demo collection that ships with 038 * the JFreeChart Developer Guide (DG); 039 * 040 */ 041 042package org.jfree.chart.demo; 043 044import java.awt.BasicStroke; 045import java.awt.Color; 046import java.awt.Dimension; 047import java.awt.Font; 048import java.awt.GradientPaint; 049import java.awt.Point; 050import java.awt.RadialGradientPaint; 051import java.awt.geom.Point2D; 052import javax.swing.JPanel; 053 054import org.jfree.chart.ChartFactory; 055import org.jfree.chart.ChartPanel; 056import org.jfree.chart.JFreeChart; 057import org.jfree.chart.StandardChartTheme; 058import org.jfree.chart.plot.PiePlot; 059import org.jfree.chart.title.TextTitle; 060import org.jfree.data.general.DefaultPieDataset; 061import org.jfree.data.general.PieDataset; 062import org.jfree.ui.ApplicationFrame; 063import org.jfree.ui.HorizontalAlignment; 064import org.jfree.ui.RectangleEdge; 065import org.jfree.ui.RectangleInsets; 066import org.jfree.ui.RefineryUtilities; 067 068/** 069 * A simple demonstration application showing how to create a pie chart using 070 * data from a {@link DefaultPieDataset}. 071 */ 072public class PieChartDemo1 extends ApplicationFrame { 073 074 private static final long serialVersionUID = 1L; 075 076 static { 077 // set a theme using the new shadow generator feature available in 078 // 1.0.14 - for backwards compatibility it is not enabled by default 079 ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", 080 true)); 081 } 082 083 /** 084 * Default constructor. 085 * 086 * @param title the frame title. 087 */ 088 public PieChartDemo1(String title) { 089 super(title); 090 setContentPane(createDemoPanel()); 091 } 092 093 /** 094 * Creates a sample dataset. 095 * 096 * Source: http://www.bbc.co.uk/news/business-15489523 097 * 098 * @return A sample dataset. 099 */ 100 private static PieDataset createDataset() { 101 DefaultPieDataset dataset = new DefaultPieDataset(); 102 dataset.setValue("Samsung", new Double(27.8)); 103 dataset.setValue("Others", new Double(55.3)); 104 dataset.setValue("Nokia", new Double(16.8)); 105 dataset.setValue("Apple", new Double(17.1)); 106 return dataset; 107 } 108 109 /** 110 * Creates a chart. 111 * 112 * @param dataset the dataset. 113 * 114 * @return A chart. 115 */ 116 private static JFreeChart createChart(PieDataset dataset) { 117 118 JFreeChart chart = ChartFactory.createPieChart( 119 "Smart Phones Manufactured / Q3 2011", // chart title 120 dataset, // data 121 false, // no legend 122 true, // tooltips 123 false // no URL generation 124 ); 125 126 // set a custom background for the chart 127 chart.setBackgroundPaint(new GradientPaint(new Point(0, 0), 128 new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY)); 129 130 // customise the title position and font 131 TextTitle t = chart.getTitle(); 132 t.setHorizontalAlignment(HorizontalAlignment.LEFT); 133 t.setPaint(new Color(240, 240, 240)); 134 t.setFont(new Font("Arial", Font.BOLD, 26)); 135 136 PiePlot plot = (PiePlot) chart.getPlot(); 137 plot.setBackgroundPaint(null); 138 plot.setInteriorGap(0.04); 139 plot.setOutlineVisible(false); 140 141 // use gradients and white borders for the section colours 142 plot.setSectionPaint("Others", createGradientPaint(new Color(200, 200, 255), Color.BLUE)); 143 plot.setSectionPaint("Samsung", createGradientPaint(new Color(255, 200, 200), Color.RED)); 144 plot.setSectionPaint("Apple", createGradientPaint(new Color(200, 255, 200), Color.GREEN)); 145 plot.setSectionPaint("Nokia", createGradientPaint(new Color(200, 255, 200), Color.YELLOW)); 146 plot.setBaseSectionOutlinePaint(Color.WHITE); 147 plot.setSectionOutlinesVisible(true); 148 plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f)); 149 150 // customise the section label appearance 151 plot.setLabelFont(new Font("Courier New", Font.BOLD, 20)); 152 plot.setLabelLinkPaint(Color.WHITE); 153 plot.setLabelLinkStroke(new BasicStroke(2.0f)); 154 plot.setLabelOutlineStroke(null); 155 plot.setLabelPaint(Color.WHITE); 156 plot.setLabelBackgroundPaint(null); 157 158 // add a subtitle giving the data source 159 TextTitle source = new TextTitle("Source: http://www.bbc.co.uk/news/business-15489523", 160 new Font("Courier New", Font.PLAIN, 12)); 161 source.setPaint(Color.WHITE); 162 source.setPosition(RectangleEdge.BOTTOM); 163 source.setHorizontalAlignment(HorizontalAlignment.RIGHT); 164 chart.addSubtitle(source); 165 return chart; 166 167 } 168 169 /** 170 * A utility method for creating gradient paints. 171 * 172 * @param c1 color 1. 173 * @param c2 color 2. 174 * 175 * @return A radial gradient paint. 176 */ 177 private static RadialGradientPaint createGradientPaint(Color c1, Color c2) { 178 Point2D center = new Point2D.Float(0, 0); 179 float radius = 200; 180 float[] dist = {0.0f, 1.0f}; 181 return new RadialGradientPaint(center, radius, dist, 182 new Color[] {c1, c2}); 183 } 184 185 /** 186 * Creates a panel for the demo (used by SuperDemo.java). 187 * 188 * @return A panel. 189 */ 190 public static JPanel createDemoPanel() { 191 JFreeChart chart = createChart(createDataset()); 192 chart.setPadding(new RectangleInsets(4, 8, 2, 2)); 193 ChartPanel panel = new ChartPanel(chart); 194 panel.setMouseWheelEnabled(true); 195 panel.setPreferredSize(new Dimension(600, 300)); 196 return panel; 197 } 198 199 /** 200 * Starting point for the demonstration application. 201 * 202 * @param args ignored. 203 */ 204 public static void main(String[] args) { 205 206 // ****************************************************************** 207 // More than 150 demo applications are included with the JFreeChart 208 // Developer Guide...for more information, see: 209 // 210 // > http://www.object-refinery.com/jfreechart/guide.html 211 // 212 // ****************************************************************** 213 214 PieChartDemo1 demo = new PieChartDemo1("JFreeChart: Pie Chart Demo 1"); 215 demo.pack(); 216 RefineryUtilities.centerFrameOnScreen(demo); 217 demo.setVisible(true); 218 } 219 220} 221