001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2006, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------ 028 * BarChartDemo1.java 029 * ------------------ 030 * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): ; 034 * 035 * $Id: BarChartDemo1.java,v 1.1.2.3 2006/10/25 10:38:47 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 09-Mar-2005 : Version 1, copied from the demo collection that ships with 040 * the JFreeChart Developer Guide (DG); 041 * 042 */ 043 044 package org.jfree.chart.demo; 045 046 import java.awt.Color; 047 import java.awt.Dimension; 048 import java.awt.GradientPaint; 049 050 import org.jfree.chart.ChartFactory; 051 import org.jfree.chart.ChartPanel; 052 import org.jfree.chart.JFreeChart; 053 import org.jfree.chart.axis.CategoryAxis; 054 import org.jfree.chart.axis.CategoryLabelPositions; 055 import org.jfree.chart.axis.NumberAxis; 056 import org.jfree.chart.plot.CategoryPlot; 057 import org.jfree.chart.plot.PlotOrientation; 058 import org.jfree.chart.renderer.category.BarRenderer; 059 import org.jfree.data.category.CategoryDataset; 060 import org.jfree.data.category.DefaultCategoryDataset; 061 import org.jfree.ui.ApplicationFrame; 062 import org.jfree.ui.RefineryUtilities; 063 064 /** 065 * A simple demonstration application showing how to create a bar chart. 066 */ 067 public class BarChartDemo1 extends ApplicationFrame { 068 069 /** 070 * Creates a new demo instance. 071 * 072 * @param title the frame title. 073 */ 074 public BarChartDemo1(String title) { 075 super(title); 076 CategoryDataset dataset = createDataset(); 077 JFreeChart chart = createChart(dataset); 078 ChartPanel chartPanel = new ChartPanel(chart, false); 079 chartPanel.setPreferredSize(new Dimension(500, 270)); 080 setContentPane(chartPanel); 081 } 082 083 /** 084 * Returns a sample dataset. 085 * 086 * @return The dataset. 087 */ 088 private static CategoryDataset createDataset() { 089 090 // row keys... 091 String series1 = "First"; 092 String series2 = "Second"; 093 String series3 = "Third"; 094 095 // column keys... 096 String category1 = "Category 1"; 097 String category2 = "Category 2"; 098 String category3 = "Category 3"; 099 String category4 = "Category 4"; 100 String category5 = "Category 5"; 101 102 // create the dataset... 103 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 104 105 dataset.addValue(1.0, series1, category1); 106 dataset.addValue(4.0, series1, category2); 107 dataset.addValue(3.0, series1, category3); 108 dataset.addValue(5.0, series1, category4); 109 dataset.addValue(5.0, series1, category5); 110 111 dataset.addValue(5.0, series2, category1); 112 dataset.addValue(7.0, series2, category2); 113 dataset.addValue(6.0, series2, category3); 114 dataset.addValue(8.0, series2, category4); 115 dataset.addValue(4.0, series2, category5); 116 117 dataset.addValue(4.0, series3, category1); 118 dataset.addValue(3.0, series3, category2); 119 dataset.addValue(2.0, series3, category3); 120 dataset.addValue(3.0, series3, category4); 121 dataset.addValue(6.0, series3, category5); 122 123 return dataset; 124 125 } 126 127 /** 128 * Creates a sample chart. 129 * 130 * @param dataset the dataset. 131 * 132 * @return The chart. 133 */ 134 private static JFreeChart createChart(CategoryDataset dataset) { 135 136 // create the chart... 137 JFreeChart chart = ChartFactory.createBarChart( 138 "Bar Chart Demo 1", // chart title 139 "Category", // domain axis label 140 "Value", // range axis label 141 dataset, // data 142 PlotOrientation.VERTICAL, // orientation 143 true, // include legend 144 true, // tooltips? 145 false // URLs? 146 ); 147 148 // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... 149 150 // set the background color for the chart... 151 chart.setBackgroundPaint(Color.white); 152 153 // get a reference to the plot for further customisation... 154 CategoryPlot plot = chart.getCategoryPlot(); 155 plot.setBackgroundPaint(Color.lightGray); 156 plot.setDomainGridlinePaint(Color.white); 157 plot.setDomainGridlinesVisible(true); 158 plot.setRangeGridlinePaint(Color.white); 159 160 // ****************************************************************** 161 // More than 150 demo applications are included with the JFreeChart 162 // Developer Guide...for more information, see: 163 // 164 // > http://www.object-refinery.com/jfreechart/guide.html 165 // 166 // ****************************************************************** 167 168 // set the range axis to display integers only... 169 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 170 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 171 172 // disable bar outlines... 173 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 174 renderer.setDrawBarOutline(false); 175 176 // set up gradient paints for series... 177 GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 178 0.0f, 0.0f, new Color(0, 0, 64)); 179 GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 180 0.0f, 0.0f, new Color(0, 64, 0)); 181 GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 182 0.0f, 0.0f, new Color(64, 0, 0)); 183 renderer.setSeriesPaint(0, gp0); 184 renderer.setSeriesPaint(1, gp1); 185 renderer.setSeriesPaint(2, gp2); 186 187 CategoryAxis domainAxis = plot.getDomainAxis(); 188 domainAxis.setCategoryLabelPositions( 189 CategoryLabelPositions.createUpRotationLabelPositions( 190 Math.PI / 6.0)); 191 // OPTIONAL CUSTOMISATION COMPLETED. 192 193 return chart; 194 195 } 196 197 /** 198 * Starting point for the demonstration application. 199 * 200 * @param args ignored. 201 */ 202 public static void main(String[] args) { 203 BarChartDemo1 demo = new BarChartDemo1("Bar Chart Demo 1"); 204 demo.pack(); 205 RefineryUtilities.centerFrameOnScreen(demo); 206 demo.setVisible(true); 207 } 208 209 }