001/*
002 * Copyright 2010, 2011 Institut Pasteur.
003 * 
004 * This file is part of ICY.
005 * 
006 * ICY is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * ICY is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
018 */
019
020/*
021 * ===========================================================
022 * JFreeChart : a free chart library for the Java(tm) platform
023 * ===========================================================
024 * 
025 * (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
026 * 
027 * Project Info: http://www.jfree.org/jfreechart/index.html
028 * 
029 * This library is free software; you can redistribute it and/or modify it
030 * under the terms of the GNU Lesser General Public License as published by
031 * the Free Software Foundation; either version 2.1 of the License, or
032 * (at your option) any later version.
033 * 
034 * This library is distributed in the hope that it will be useful, but
035 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
036 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
037 * License for more details.
038 * 
039 * You should have received a copy of the GNU Lesser General Public
040 * License along with this library; if not, write to the Free Software
041 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
042 * USA.
043 * 
044 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
045 * in the United States and other countries.]
046 * 
047 * ------------------
048 * BarChartDemo1.java
049 * ------------------
050 * (C) Copyright 2003-2009, by Object Refinery Limited and Contributors.
051 * 
052 * Original Author: David Gilbert (for Object Refinery Limited);
053 * Contributor(s): ;
054 * 
055 * Changes
056 * -------
057 * 09-Mar-2005 : Version 1 (DG);
058 */
059
060package plugins.tutorial.chart;
061
062import icy.gui.frame.IcyFrame;
063import icy.gui.util.GuiUtil;
064import icy.plugin.abstract_.PluginActionable;
065
066import java.awt.Color;
067import java.awt.Dimension;
068import java.awt.GradientPaint;
069
070import javax.swing.JPanel;
071
072import org.jfree.chart.ChartFactory;
073import org.jfree.chart.ChartPanel;
074import org.jfree.chart.JFreeChart;
075import org.jfree.chart.axis.CategoryAxis;
076import org.jfree.chart.axis.CategoryLabelPositions;
077import org.jfree.chart.axis.NumberAxis;
078import org.jfree.chart.plot.CategoryPlot;
079import org.jfree.chart.plot.PlotOrientation;
080import org.jfree.chart.renderer.category.BarRenderer;
081import org.jfree.data.category.CategoryDataset;
082import org.jfree.data.category.DefaultCategoryDataset;
083
084/**
085 * A simple demonstration application showing how to create a bar chart, adapted to ICY.
086 */
087public class ChartTutorial1 extends PluginActionable
088{
089    JPanel mainPanel = GuiUtil.generatePanel("Graph");
090    IcyFrame mainFrame = GuiUtil.generateTitleFrame("Chart demo", mainPanel, new Dimension(300, 100), true, true, true,
091            true);
092
093    /**
094     * Returns a sample dataset.
095     * 
096     * @return The dataset.
097     */
098    private static CategoryDataset createDataset()
099    {
100        // row keys...
101        String series1 = "First";
102        String series2 = "Second";
103        String series3 = "Third";
104
105        // column keys...
106        String category1 = "Category 1";
107        String category2 = "Category 2";
108        String category3 = "Category 3";
109        String category4 = "Category 4";
110        String category5 = "Category 5";
111
112        // create the dataset...
113        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
114
115        dataset.addValue(1.0, series1, category1);
116        dataset.addValue(4.0, series1, category2);
117        dataset.addValue(3.0, series1, category3);
118        dataset.addValue(5.0, series1, category4);
119        dataset.addValue(5.0, series1, category5);
120
121        dataset.addValue(5.0, series2, category1);
122        dataset.addValue(7.0, series2, category2);
123        dataset.addValue(6.0, series2, category3);
124        dataset.addValue(8.0, series2, category4);
125        dataset.addValue(4.0, series2, category5);
126
127        dataset.addValue(4.0, series3, category1);
128        dataset.addValue(3.0, series3, category2);
129        dataset.addValue(2.0, series3, category3);
130        dataset.addValue(3.0, series3, category4);
131        dataset.addValue(6.0, series3, category5);
132
133        return dataset;
134
135    }
136
137    /**
138     * Creates a sample chart.
139     * 
140     * @param dataset
141     *        the dataset.
142     * @return The chart.
143     */
144    private static JFreeChart createChart(CategoryDataset dataset)
145    {
146        // create the chart...
147        JFreeChart chart = ChartFactory.createBarChart("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, 0.0f, 0.0f, new Color(0, 0, 64));
183        GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0));
184        GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(64, 0, 0));
185        renderer.setSeriesPaint(0, gp0);
186        renderer.setSeriesPaint(1, gp1);
187        renderer.setSeriesPaint(2, gp2);
188
189        CategoryAxis domainAxis = plot.getDomainAxis();
190        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
191        // OPTIONAL CUSTOMISATION COMPLETED.
192
193        return chart;
194
195    }
196
197    @Override
198    public void run()
199    {
200        CategoryDataset dataset = createDataset();
201        JFreeChart chart = createChart(dataset);
202        ChartPanel chartPanel = new ChartPanel(chart);
203        chartPanel.setFillZoomRectangle(true);
204        chartPanel.setMouseWheelEnabled(true);
205        chartPanel.setPreferredSize(new Dimension(500, 270));
206        mainPanel.add(chartPanel);
207
208        mainFrame.pack();
209
210        addIcyFrame(mainFrame);
211
212        mainFrame.setVisible(true);
213        mainFrame.center();
214        mainFrame.requestFocus();
215    }
216}