001/* ==================
002 * BarChartDemo1.java
003 * ==================
004 *
005 * Copyright (c) 2005-2014, Object Refinery Limited.
006 * All rights reserved.
007 *
008 * http://www.jfree.org/jfreechart/index.html
009 *
010 * Redistribution and use in source and binary forms, with or without
011 * modification, are permitted provided that the following conditions are met:
012 *   - Redistributions of source code must retain the above copyright
013 *     notice, this list of conditions and the following disclaimer.
014 *   - Redistributions in binary form must reproduce the above copyright
015 *     notice, this list of conditions and the following disclaimer in the
016 *     documentation and/or other materials provided with the distribution.
017 *   - Neither the name of the Object Refinery Limited nor the
018 *     names of its contributors may be used to endorse or promote products
019 *     derived from this software without specific prior written permission.
020 *
021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
024 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
025 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
028 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * 11-Mar-2014 : Use new ChartFactory method (DG);
039 * 25-Jun-2014 : Update to use real data (DG);
040 * 
041 */
042
043package org.jfree.chart.demo;
044
045import java.awt.Color;
046import java.awt.Dimension;
047
048import org.jfree.chart.ChartFactory;
049import org.jfree.chart.ChartPanel;
050import org.jfree.chart.JFreeChart;
051import org.jfree.chart.StandardChartTheme;
052import org.jfree.chart.axis.NumberAxis;
053import org.jfree.chart.block.BlockBorder;
054import org.jfree.chart.plot.CategoryPlot;
055import org.jfree.chart.renderer.category.BarRenderer;
056import org.jfree.chart.title.TextTitle;
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    static {
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        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
099        dataset.addValue(7445, "JFreeSVG", "Warm-up");
100        dataset.addValue(24448, "Batik", "Warm-up");
101        dataset.addValue(4297, "JFreeSVG", "Test");
102        dataset.addValue(21022, "Batik", "Test");
103        return dataset;
104    }
105
106    /**
107     * Creates a sample chart.
108     *
109     * @param dataset  the dataset.
110     *
111     * @return The chart.
112     */
113    private static JFreeChart createChart(CategoryDataset dataset) {
114        JFreeChart chart = ChartFactory.createBarChart(
115            "Performance: JFreeSVG vs Batik", null /* x-axis label*/, 
116                "Milliseconds" /* y-axis label */, dataset);
117        chart.addSubtitle(new TextTitle("Time to generate 1000 charts in SVG " 
118                + "format (lower bars = better performance)"));
119        chart.setBackgroundPaint(Color.white);
120        CategoryPlot plot = (CategoryPlot) chart.getPlot();
121
122        // ******************************************************************
123        //  More than 150 demo applications are included with the JFreeChart
124        //  Developer Guide...for more information, see:
125        //
126        //  >   http://www.object-refinery.com/jfreechart/guide.html
127        //
128        // ******************************************************************
129
130        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
131        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
132        BarRenderer renderer = (BarRenderer) plot.getRenderer();
133        renderer.setDrawBarOutline(false);
134        chart.getLegend().setFrame(BlockBorder.NONE);
135        return chart;
136    }
137
138    /**
139     * Starting point for the demonstration application.
140     *
141     * @param args  ignored.
142     */
143    public static void main(String[] args) {
144        BarChartDemo1 demo = new BarChartDemo1("JFreeChart: BarChartDemo1.java");
145        demo.pack();
146        RefineryUtilities.centerFrameOnScreen(demo);
147        demo.setVisible(true);
148    }
149
150}