我认为你使用了错误的情节。“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);
}
}