我能够根据价格成功绘制日期,在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);
这可能吗?关于如何实现这一目标的任何建议?
答案 0 :(得分: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);
}
}