1

我能够成功地根据价格绘制日期,x 轴上显示日期,y 轴上显示价格。

后来我还想在同一张图表中绘制成交量-日期图,所以我为此目的使用了 CombinedRangeXYPlot。这两个图表的共同数据是日期。理想情况下,我希望图表在另一个下方(水平方向)显示,共同的 x 轴是日期,每个都有自己的 y 轴,一个是价格,另一个是交易量

但我面临的问题是,当方向为水平时,日期显示在 y 轴上,图表显示为以 90 度角倾斜。如果方向是垂直的,则 x 轴由日期组成,y 轴处理价格和交易量。由于价格变动以 100 为单位,而交易量变动以百万计,因此我无法在此图中发现价格变动。理想情况下,当方向为水平时,我希望 x 轴对于包含日期的两个图表都是通用的,但我需要 2 个 y 轴值,每个图表一个。

    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);

这可能吗?关于如何实现这一点的任何建议?

4

1 回答 1

0

我认为你使用了错误的情节。“CombinedRangeXYPlot”中的子图共享一个公共 Y 轴,但“CombinedDomainXYPlot”中的子图共享一个公共 X 轴。您的要求需要一个 CombinedDomainXYPlot。以下是从 jfreechart 演示修改的简化示例。我希望它可以帮助。

/* ===========================================================
 * 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);

}

}

于 2014-01-16T02:49:02.427 回答