    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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-2005, 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.1 2005/10/25 20:41:32 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
    076            super(title);
    077            CategoryDataset dataset = createDataset();
    078            JFreeChart chart = createChart(dataset);
    079            ChartPanel chartPanel = new ChartPanel(chart, false);
    080            chartPanel.setPreferredSize(new Dimension(500, 270));
    081            setContentPane(chartPanel);
    082
    083        }
    084
    085        /**
    086         * Returns a sample dataset.
    087         *
    088         * @return The dataset.
    089         */
    090        private static CategoryDataset createDataset() {
    091
    092            // row keys...
    093            String series1 = "First";
    094            String series2 = "Second";
    095            String series3 = "Third";
    096
    097            // column keys...
    098            String category1 = "Category 1";
    099            String category2 = "Category 2";
    100            String category3 = "Category 3";
    101            String category4 = "Category 4";
    102            String category5 = "Category 5";
    103
    104            // create the dataset...
    105            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    106
    107            dataset.addValue(1.0, series1, category1);
    108            dataset.addValue(4.0, series1, category2);
    109            dataset.addValue(3.0, series1, category3);
    110            dataset.addValue(5.0, series1, category4);
    111            dataset.addValue(5.0, series1, category5);
    112
    113            dataset.addValue(5.0, series2, category1);
    114            dataset.addValue(7.0, series2, category2);
    115            dataset.addValue(6.0, series2, category3);
    116            dataset.addValue(8.0, series2, category4);
    117            dataset.addValue(4.0, series2, category5);
    118
    119            dataset.addValue(4.0, series3, category1);
    120            dataset.addValue(3.0, series3, category2);
    121            dataset.addValue(2.0, series3, category3);
    122            dataset.addValue(3.0, series3, category4);
    123            dataset.addValue(6.0, series3, category5);
    124
    125            return dataset;
    126
    127        }
    128
    129        /**
    130         * Creates a sample chart.
    131         *
    132         * @param dataset  the dataset.
    133         *
    134         * @return The chart.
    135         */
    136        private static JFreeChart createChart(CategoryDataset dataset) {
    137
    138            // create the chart...
    139            JFreeChart chart = ChartFactory.createBarChart(
    140                "Bar Chart Demo",         // chart title
    141                "Category",               // domain axis label
    142                "Value",                  // range axis label
    143                dataset,                  // data
    144                PlotOrientation.VERTICAL, // orientation
    145                true,                     // include legend
    146                true,                     // tooltips?
    147                false                     // URLs?
    148            );
    149
    150            // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    151
    152            // set the background color for the chart...
    153            chart.setBackgroundPaint(Color.white);
    154
    155            // get a reference to the plot for further customisation...
    156            CategoryPlot plot = chart.getCategoryPlot();
    157            plot.setBackgroundPaint(Color.lightGray);
    158            plot.setDomainGridlinePaint(Color.white);
    159            plot.setDomainGridlinesVisible(true);
    160            plot.setRangeGridlinePaint(Color.white);
    161
    162            // set the range axis to display integers only...
    163            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    164            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    165
    166            // disable bar outlines...
    167            BarRenderer renderer = (BarRenderer) plot.getRenderer();
    168            renderer.setDrawBarOutline(false);
    169
    170            // set up gradient paints for series...
    171            GradientPaint gp0 = new GradientPaint(
    172                0.0f, 0.0f, Color.blue,
    173                0.0f, 0.0f, new Color(0, 0, 64)
    174            );
    175            GradientPaint gp1 = new GradientPaint(
    176                0.0f, 0.0f, Color.green,
    177                0.0f, 0.0f, new Color(0, 64, 0)
    178            );
    179            GradientPaint gp2 = new GradientPaint(
    180                0.0f, 0.0f, Color.red,
    181                0.0f, 0.0f, new Color(64, 0, 0)
    182            );
    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(Math.PI / 6.0)
    190            );
    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
    204            BarChartDemo1 demo = new BarChartDemo1("Bar Chart Demo");
    205            demo.pack();
    206            RefineryUtilities.centerFrameOnScreen(demo);
    207            demo.setVisible(true);
    208
    209        }
    210
    211    }