Source for org.jfree.chart.demo.BarChartDemo1

   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:  * BarChartDemo1.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 (DG);
  38:  *
  39:  */
  40: 
  41: package org.jfree.chart.demo;
  42: 
  43: import java.awt.Color;
  44: import java.awt.Dimension;
  45: import java.awt.GradientPaint;
  46: 
  47: import org.jfree.chart.ChartFactory;
  48: import org.jfree.chart.ChartPanel;
  49: import org.jfree.chart.JFreeChart;
  50: import org.jfree.chart.axis.CategoryAxis;
  51: import org.jfree.chart.axis.CategoryLabelPositions;
  52: import org.jfree.chart.axis.NumberAxis;
  53: import org.jfree.chart.plot.CategoryPlot;
  54: import org.jfree.chart.plot.PlotOrientation;
  55: import org.jfree.chart.renderer.category.BarRenderer;
  56: import org.jfree.data.category.CategoryDataset;
  57: import org.jfree.data.category.DefaultCategoryDataset;
  58: import org.jfree.ui.ApplicationFrame;
  59: import org.jfree.ui.RefineryUtilities;
  60: 
  61: /**
  62:  * A simple demonstration application showing how to create a bar chart.
  63:  */
  64: public class BarChartDemo1 extends ApplicationFrame {
  65: 
  66:     /**
  67:      * Creates a new demo instance.
  68:      *
  69:      * @param title  the frame title.
  70:      */
  71:     public BarChartDemo1(String title) {
  72:         super(title);
  73:         CategoryDataset dataset = createDataset();
  74:         JFreeChart chart = createChart(dataset);
  75:         ChartPanel chartPanel = new ChartPanel(chart, false);
  76:         chartPanel.setPreferredSize(new Dimension(500, 270));
  77:         setContentPane(chartPanel);
  78:     }
  79: 
  80:     /**
  81:      * Returns a sample dataset.
  82:      * 
  83:      * @return The dataset.
  84:      */
  85:     private static CategoryDataset createDataset() {
  86:         
  87:         // row keys...
  88:         String series1 = "First";
  89:         String series2 = "Second";
  90:         String series3 = "Third";
  91: 
  92:         // column keys...
  93:         String category1 = "Category 1";
  94:         String category2 = "Category 2";
  95:         String category3 = "Category 3";
  96:         String category4 = "Category 4";
  97:         String category5 = "Category 5";
  98: 
  99:         // create the dataset...
 100:         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 101: 
 102:         dataset.addValue(1.0, series1, category1);
 103:         dataset.addValue(4.0, series1, category2);
 104:         dataset.addValue(3.0, series1, category3);
 105:         dataset.addValue(5.0, series1, category4);
 106:         dataset.addValue(5.0, series1, category5);
 107: 
 108:         dataset.addValue(5.0, series2, category1);
 109:         dataset.addValue(7.0, series2, category2);
 110:         dataset.addValue(6.0, series2, category3);
 111:         dataset.addValue(8.0, series2, category4);
 112:         dataset.addValue(4.0, series2, category5);
 113: 
 114:         dataset.addValue(4.0, series3, category1);
 115:         dataset.addValue(3.0, series3, category2);
 116:         dataset.addValue(2.0, series3, category3);
 117:         dataset.addValue(3.0, series3, category4);
 118:         dataset.addValue(6.0, series3, category5);
 119:         
 120:         return dataset;
 121:         
 122:     }
 123:     
 124:     /**
 125:      * Creates a sample chart.
 126:      * 
 127:      * @param dataset  the dataset.
 128:      * 
 129:      * @return The chart.
 130:      */
 131:     private static JFreeChart createChart(CategoryDataset dataset) {
 132:         
 133:         // create the chart...
 134:         JFreeChart chart = ChartFactory.createBarChart(
 135:             "Bar Chart Demo 1",       // chart title
 136:             "Category",               // domain axis label
 137:             "Value",                  // range axis label
 138:             dataset,                  // data
 139:             PlotOrientation.VERTICAL, // orientation
 140:             true,                     // include legend
 141:             true,                     // tooltips?
 142:             false                     // URLs?
 143:         );
 144: 
 145:         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
 146: 
 147:         // set the background color for the chart...
 148:         chart.setBackgroundPaint(Color.white);
 149: 
 150:         // get a reference to the plot for further customisation...
 151:         CategoryPlot plot = (CategoryPlot) chart.getPlot();
 152:         plot.setBackgroundPaint(Color.lightGray);
 153:         plot.setDomainGridlinePaint(Color.white);
 154:         plot.setDomainGridlinesVisible(true);
 155:         plot.setRangeGridlinePaint(Color.white);
 156: 
 157:         // ******************************************************************
 158:         //  More than 150 demo applications are included with the JFreeChart
 159:         //  Developer Guide...for more information, see:
 160:         //
 161:         //  >   http://www.object-refinery.com/jfreechart/guide.html
 162:         //
 163:         // ******************************************************************
 164:         
 165:         // set the range axis to display integers only...
 166:         final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
 167:         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
 168: 
 169:         // disable bar outlines...
 170:         BarRenderer renderer = (BarRenderer) plot.getRenderer();
 171:         renderer.setDrawBarOutline(false);
 172:         
 173:         // set up gradient paints for series...
 174:         GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 
 175:                 0.0f, 0.0f, new Color(0, 0, 64));
 176:         GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 
 177:                 0.0f, 0.0f, new Color(0, 64, 0));
 178:         GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 
 179:                 0.0f, 0.0f, new Color(64, 0, 0));
 180:         renderer.setSeriesPaint(0, gp0);
 181:         renderer.setSeriesPaint(1, gp1);
 182:         renderer.setSeriesPaint(2, gp2);
 183: 
 184:         CategoryAxis domainAxis = plot.getDomainAxis();
 185:         domainAxis.setCategoryLabelPositions(
 186:                 CategoryLabelPositions.createUpRotationLabelPositions(
 187:                         Math.PI / 6.0));
 188:         // OPTIONAL CUSTOMISATION COMPLETED.
 189:         
 190:         return chart;
 191:         
 192:     }
 193:     
 194:     /**
 195:      * Starting point for the demonstration application.
 196:      *
 197:      * @param args  ignored.
 198:      */
 199:     public static void main(String[] args) {
 200:         BarChartDemo1 demo = new BarChartDemo1("Bar Chart Demo 1");
 201:         demo.pack();
 202:         RefineryUtilities.centerFrameOnScreen(demo);
 203:         demo.setVisible(true);
 204:     }
 205: 
 206: }