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 */
059package plugins.tutorial.chart;
060
061import icy.gui.frame.IcyFrame;
062import icy.gui.util.GuiUtil;
063import icy.plugin.abstract_.PluginActionable;
064
065import java.awt.BasicStroke;
066import java.awt.Color;
067import java.awt.Dimension;
068
069import javax.swing.JPanel;
070
071import org.jfree.chart.ChartFactory;
072import org.jfree.chart.ChartPanel;
073import org.jfree.chart.JFreeChart;
074import org.jfree.chart.axis.NumberAxis;
075import org.jfree.chart.plot.XYPlot;
076import org.jfree.chart.renderer.xy.DeviationRenderer;
077import org.jfree.data.time.RegularTimePeriod;
078import org.jfree.data.time.Week;
079import org.jfree.data.xy.XYDataset;
080import org.jfree.data.xy.YIntervalSeries;
081import org.jfree.data.xy.YIntervalSeriesCollection;
082import org.jfree.ui.RectangleInsets;
083
084/**
085 * A simple demonstration of a more complex graph from JFreeChart, adapted to ICY
086 */
087public class ChartTutorial2 extends PluginActionable
088{
089
090    JPanel mainPanel = GuiUtil.generatePanel("Graph");
091    IcyFrame mainFrame = GuiUtil.generateTitleFrame("Chart demo", mainPanel, new Dimension(300, 100), true, true, true,
092            true);
093
094    /**
095     * Returns a sample dataset.
096     * 
097     * @return The dataset.
098     */
099    private static XYDataset createDataset()
100    {
101
102        YIntervalSeries yintervalseries = new YIntervalSeries("Series 1");
103        YIntervalSeries yintervalseries1 = new YIntervalSeries("Series 2");
104        Object obj = new Week();
105        double d = 100D;
106        double d1 = 100D;
107        for (int i = 0; i <= 52; i++)
108        {
109            double d2 = 0.050000000000000003D * i;
110            yintervalseries.add(((RegularTimePeriod) (obj)).getFirstMillisecond(), d, d - d2, d + d2);
111            d = (d + Math.random()) - 0.45000000000000001D;
112            double d3 = 0.070000000000000007D * i;
113            yintervalseries1.add(((RegularTimePeriod) (obj)).getFirstMillisecond(), d1, d1 - d3, d1 + d3);
114            d1 = (d1 + Math.random()) - 0.55000000000000004D;
115            obj = ((RegularTimePeriod) (obj)).next();
116        }
117
118        YIntervalSeriesCollection yintervalseriescollection = new YIntervalSeriesCollection();
119        yintervalseriescollection.addSeries(yintervalseries);
120        yintervalseriescollection.addSeries(yintervalseries1);
121        return yintervalseriescollection;
122
123    }
124
125    /**
126     * Creates a sample chart.
127     * 
128     * @param dataset
129     *        the dataset.
130     * @return The chart.
131     */
132    private static JFreeChart createChart(XYDataset xydataset)
133    {
134
135        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Projected Values - Test", "Date",
136                "Index Projection", xydataset, true, true, false);
137        jfreechart.setBackgroundPaint(Color.white);
138        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
139        xyplot.setInsets(new RectangleInsets(5D, 5D, 5D, 20D));
140        xyplot.setBackgroundPaint(Color.lightGray);
141        xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
142        xyplot.setDomainGridlinePaint(Color.white);
143        xyplot.setRangeGridlinePaint(Color.white);
144        DeviationRenderer deviationrenderer = new DeviationRenderer(true, false);
145        deviationrenderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1));
146        deviationrenderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1));
147        deviationrenderer.setSeriesStroke(1, new BasicStroke(3F, 1, 1));
148        deviationrenderer.setSeriesFillPaint(0, new Color(255, 200, 200));
149        deviationrenderer.setSeriesFillPaint(1, new Color(200, 200, 255));
150        xyplot.setRenderer(deviationrenderer);
151        NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
152        numberaxis.setAutoRangeIncludesZero(false);
153        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
154        return jfreechart;
155
156    }
157
158    @Override
159    public void run()
160    {
161
162        XYDataset dataset = createDataset();
163        JFreeChart chart = createChart(dataset);
164        ChartPanel chartPanel = new ChartPanel(chart);
165        chartPanel.setFillZoomRectangle(true);
166        chartPanel.setMouseWheelEnabled(true);
167        chartPanel.setPreferredSize(new Dimension(500, 270));
168        mainPanel.add(chartPanel);
169
170        mainFrame.pack();
171        addIcyFrame(mainFrame);
172
173        mainFrame.setVisible(true);
174        mainFrame.center();
175        mainFrame.requestFocus();
176
177    }
178
179}