JFreeChart: Dual Axis Demo 2 : Dual Axis Chart : Chart : Java examples (example source code) Organized by topic

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Chart » Dual Axis ChartScreenshots 
JFreeChart: Dual Axis Demo 2
JFreeChart: Dual Axis Demo 2

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ------------------
 * DualAxisDemo2.java
 * ------------------
 * (C) Copyright 2002-2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: DualAxisDemo2.java,v 1.17 2004/05/21 10:09:10 mungady Exp $
 *
 * Changes
 * -------
 * 19-Nov-2002 : Version 1 (DG);
 * 27-Apr-2004 : Updated for changes to the XYPlot class (DG);
 *
 */

package org.jfree.chart.demo;

import java.awt.Color;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * An example of a time series chart.  For the most part, default settings are used, except that
 * the renderer is modified to show filled shapes (as well as lines) at each data point.
 *
 */
public class DualAxisDemo2 extends ApplicationFrame {

    /**
     * A demonstration application showing how to create a time series chart with dual axes.
     *
     @param title  the frame title.
     */
    public DualAxisDemo2(final String title) {

        super(title);

        // create a title...
        final String chartTitle = "Dual Axis Demo 2";
        final XYDataset dataset = createDataset1();

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
            chartTitle, 
            "Date"
            "Price Per Unit",
            dataset, 
            true, 
            true, 
            false
        );

  //      final StandardLegend legend = (StandardLegend) chart.getLegend();
    //    legend.setDisplaySeriesShapes(true);
        
        final XYPlot plot = chart.getXYPlot();
        final NumberAxis axis2 = new NumberAxis("Secondary");
        axis2.setAutoRangeIncludesZero(false);
        plot.setRangeAxis(1, axis2);
        plot.setDataset(1, createDataset2());
        plot.mapDatasetToRangeAxis(11);
        final XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        if (renderer instanceof StandardXYItemRenderer) {
            final StandardXYItemRenderer rr = (StandardXYItemRendererrenderer;
            rr.setPlotShapes(true);
            rr.setShapesFilled(true);
        }
        
        final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
        renderer2.setSeriesPaint(0, Color.black);
        renderer2.setPlotShapes(true);
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(1, renderer2);
        
        final DateAxis axis = (DateAxisplot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500270));
        setContentPane(chartPanel);

    }

    /**
     * Creates a sample dataset.
     *
     @return The dataset.
     */
    private XYDataset createDataset1() {

        final TimeSeries s1 = new TimeSeries("Random Data 1", Month.class);
        s1.add(new Month(22001)181.8);
        s1.add(new Month(32001)167.3);
        s1.add(new Month(42001)153.8);
        s1.add(new Month(52001)167.6);
        s1.add(new Month(62001)158.8);
        s1.add(new Month(72001)148.3);
        s1.add(new Month(82001)153.9);
        s1.add(new Month(92001)142.7);
        s1.add(new Month(102001)123.2);
        s1.add(new Month(112001)131.8);
        s1.add(new Month(122001)139.6);
        s1.add(new Month(12002)142.9);
        s1.add(new Month(22002)138.7);
        s1.add(new Month(32002)137.3);
        s1.add(new Month(42002)143.9);
        s1.add(new Month(52002)139.8);
        s1.add(new Month(62002)137.0);
        s1.add(new Month(72002)132.8);

        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        return dataset;

    }

    /**
     * Creates a sample dataset.
     *
     @return The dataset.
     */
    private XYDataset createDataset2() {


        final TimeSeries s2 = new TimeSeries("Random Data 2", Month.class);
        s2.add(new Month(22001)429.6);
        s2.add(new Month(32001)323.2);
        s2.add(new Month(42001)417.2);
        s2.add(new Month(52001)624.1);
        s2.add(new Month(62001)422.6);
        s2.add(new Month(72001)619.2);
        s2.add(new Month(82001)416.5);
        s2.add(new Month(92001)512.7);
        s2.add(new Month(102001)501.5);
        s2.add(new Month(112001)306.1);
        s2.add(new Month(122001)410.3);
        s2.add(new Month(12002)511.7);
        s2.add(new Month(22002)611.0);
        s2.add(new Month(32002)709.6);
        s2.add(new Month(42002)613.2);
        s2.add(new Month(52002)711.6);
        s2.add(new Month(62002)708.8);
        s2.add(new Month(72002)501.6);

        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s2);

        return dataset;

    }

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    
    /**
     * Starting point for the demonstration application.
     *
     @param args  ignored.
     */
    public static void main(final String[] args) {

        final DualAxisDemo2 demo = new DualAxisDemo2("Dual Axis Demo 2");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}


           
       
jfreechart-1.0.0-rc1.zip( 3,559 k)
Related examples in the same category
1. JFreeChart: Dual Axis DemoJFreeChart: Dual Axis Demo
2. JFreeChart: Contour Plot DemoJFreeChart: Contour Plot Demo
3. JFreeChart: Dual Axis Demo 3JFreeChart: Dual Axis Demo 3
4. JFreeChart: Dual Axis Demo 4JFreeChart: Dual Axis Demo 4
5. JFreeChart: Dual Axis Demo 5JFreeChart: Dual Axis Demo 5
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.