1

I was able to plot successfully date against price, with date on the x-axis and price shown in the y-axis.

Later I also want to plot volume-date graph in the same chart, so I've used CombinedRangeXYPlot for this purpose. The common data here for both the graphs is the date. Ideally I want the graphs to be displayed one below the other (horizontal orientation) with common x-axis being the date and each having its own y-axis, one being the price and other being the volume

But the issue I am facing is, when the orientation is horizontal dates are displayed in the y-axis with the graphs appeared as tilted at 90 degree angle. If the orientation is vertical, then the x-axis consists of dates and the y-axis handles both price and volume. Since the price movement is in 100's and the volume movement is in millions, I am not able to spot the price movements in this graph. Ideally I want the x-axis to be common for both the graphs containing the dates when the orientation is horizontal, but I would require 2 y-axis values one for each graph.

    final XYPlot priceplot = new XYPlot(dataset, new DateAxis("Date"), null, new StandardXYItemRenderer());
    NumberAxis numberAxis = (NumberAxis)priceplot.getRangeAxis();
    numberAxis.setRange(new Range(minprice,maxprice));

    final XYPlot volumeplot = new XYPlot(volumedataset, new DateAxis("Date"), null, new XYBarRenderer(0.20));
    NumberAxis numberAxisVolume = (NumberAxis)volumeplot.getRangeAxis();
    numberAxisVolume.setRange(new Range(minvolume,maxvolume));

    // create a parent plot...
    final CombinedRangeXYPlot plot = new CombinedRangeXYPlot();

    // add the subplots...
    plot.add(priceplot, 1);
    plot.add(volumeplot, 1);
    plot.setOrientation(PlotOrientation.HORIZONTAL);

Is this possible? Any advice on how this can be achieved?

user1669327
  • 135
  • 3
  • 9

1 Answers1

0

I think that you used the wrong plot. subplots in "CombinedRangeXYPlot" share a common Y axis, but subplots in "CombinedDomainXYPlot" share a common X axis. Your requirement needs a CombinedDomainXYPlot. The following is a simplified example modified from a jfreechart demo. I hope it can help.

/* ===========================================================
 * 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.]
 *
 * ------------------------
 * CombinedXYPlotDemo2.java
 * ------------------------
 * (C) Copyright 2002-2004, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited).
 * Contributor(s):   -;
 *
 * $Id $
 *
 * Changes
 * -------
 * 23-Apr-2002 : Version 1 (DG);
 * 23-May-2002 : Renamed MultiPlotDemo --> CombinedXYPlotDemo (DG);
 * 25-Jun-2002 : Removed unnecessary imports (DG);
 * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
 * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
 *
 */

package testfreechart;

import java.util.Date;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultIntervalXYDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A demonstration application showing how to create a combined chart.  A
 * bar chart is displayed on the left, and a line chart on the right.
 *
 */
public class CombinedXYPlotDemo2 extends ApplicationFrame {

    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public CombinedXYPlotDemo2(final String title) {

        super(title);
        final JFreeChart chart = createCombinedChart();
        final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(panel);

    }

    /**
     * Creates a combined XYPlot chart.
     *
     * @return the combined chart.
     */
    private JFreeChart createCombinedChart() {

        // create subplot 1...
        final XYBarDataset  data1 = createDataset1();
        final XYItemRenderer renderer1 = new XYBarRenderer(1.0);
        final XYPlot subplot1 = new XYPlot(data1, null, new NumberAxis("volume"),  renderer1);

        // create subplot 2...
        final DefaultXYDataset  data2 = createDataset2();
        final XYItemRenderer renderer2 = new StandardXYItemRenderer();
        final XYPlot subplot2 = new XYPlot(data2, null, new NumberAxis("Price"),  renderer2);

        // create a parent plot...
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Date"));

        // add the subplots...
        plot.add(subplot1, 1);
        plot.add(subplot2, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        // return a new chart containing the overlaid plot...
        return new JFreeChart(
            "Combined  XY Plot", JFreeChart.DEFAULT_TITLE_FONT, plot, true
        );

    }

    // ****************************************************************************
    // * 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.             *
    // ****************************************************************************

    /**
     * Creates a sample dataset.
     *
     * @return Series 1.
     */
    private XYBarDataset createDataset1() {

        double []values={19642.3, 18253.5, 15352.3, 13532.0, 12635.3, 
                13998.2,  11943.2,  16943.9,  17843.2,  16495.3,  17943.6, 18500.7,  19595.9};
        double [][] data = new double[2][values.length];

        int j=3;
        for(int i=0; i< values.length; i++)
        {
            Date dt = new Date(2002, 3, j);
            data[0][i] = dt.getTime();
            data[1][i] = values[i];
            j++;
        }
        DefaultXYDataset dataset = new DefaultXYDataset();
        dataset.addSeries("volume", data);

        return new XYBarDataset(dataset, 50.0);
    }


    /**
     * Creates a sample dataset.
     *
     * @return Series 2.
     */
    private DefaultXYDataset createDataset2() {


        double []values={42.3, 53.5, 52.3, 32.0, 35.3, 
                98.2,  43.2,  43.9,  43.2,  95.3,  43.6, 10.7,  95.9};

        double [][] data = new double[2][values.length];

        int j=3;
        for(int i=0; i< values.length; i++)
        {
            Date dt = new Date(2002, 3, j);
            data[0][i] = dt.getTime();
            data[1][i] = values[i];
            j++;
        }
        DefaultXYDataset dataset = new DefaultXYDataset();
        dataset.addSeries("price", data);
        return dataset;

    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final CombinedXYPlotDemo2 demo = new CombinedXYPlotDemo2("Combined XY Plot Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);

}

}

frankli22586
  • 710
  • 6
  • 19