2013-05-05 40 views
0

我能够成功地绘制日期价格与日期的X轴和价格显示在Y轴。JFreeChart CombinedXYPlot与时间序列

后来我还想在同一个图表中绘制卷日期图,所以我使用了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

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

} 

}