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