2012-11-26 153 views
4

我想问是否有可能有一个双x轴(或y轴的时间序列图,但因为它是一个时间序列数据集我集中在双x轴)。我有两个数据文件,在一分钟或一整天内进行测量。因此我不能有一个x轴。所以,一种解决方案是将一个数据文件转换为另一个数据文件的格式或者具有两个不同的x轴。第二个解决方案是可以做到的吗?JFreeChart与双x轴

我发现JFreeChartDualAxisDemo

但它不是我想要的。

在此先感谢您的帮助!

编辑:

我有下面的代码,我试图创建一个时间序列图与属于一个特定的X轴(正如你可以看到我创建了两个X轴)两个数据集。我试图执行此代码,但创建的图表不正确,实际上它只有x轴和y轴。有人能告诉我哪里出了问题吗?

int year1 = 2004; 
int year2 = 2005; 
int year3 = 2006; 
int year4 = 2007; 

int month1 = 1; 
int month2 = 5; 
int month3 = 8; 
int month4 = 12; 

int day1 = 3; 
int day2 = 10; 
int day3 = 15; 
int day4 = 30; 


TimeSeries ts= new TimeSeries("Metrisi", Day.class); 
ts.add(new Day(day1, month1, year1), 100); 
ts.add(new Day(day2, month2, year2), 150); 
ts.add(new Day(day3, month3, year3), 250); 
ts.add(new Day(day4, month4, year4), 275); 

TimeSeriesCollection dataset = new TimeSeriesCollection(); 
dataset.addSeries(ts); 


int year1_2 = 2004; 
int year2_2 = 2005; 
int year3_2 = 2006; 
int year4_2 = 2007; 

int month1_2 = 2; 
int month2_2 = 7; 
int month3_2 = 9; 
int month4_2 = 11; 

int day1_2 = 23; 
int day2_2 = 14; 
int day3_2 = 19; 
int day4_2 = 22; 


TimeSeries ts2= new TimeSeries("Metrisi", Day.class); 
ts.add(new Day(day1_2, month1_2, year1_2), 100); 
ts.add(new Day(day2_2, month2_2, year2_2), 150); 
ts.add(new Day(day3_2, month3_2, year3_2), 250); 
ts.add(new Day(day4_2, month4_2, year4_2), 275); 

TimeSeriesCollection dataset2 = new TimeSeriesCollection(); 
dataset.addSeries(ts2); 


JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Measurement", 
"Date", 
"Measurement", 
dataset, 
true, 
true, 
false); 

final XYPlot plot = chart.getXYPlot(); 

plot.setBackgroundPaint(Color.lightGray); 
plot.setDomainGridlinePaint(Color.white); 
plot.setRangeGridlinePaint(Color.white); 
plot.setDomainCrosshairVisible(true); 
plot.setRangeCrosshairVisible(false); 




final DateAxis axis2 = new DateAxis("Secondary"); 
//xAxis2.setAutoRangeIncludesZero(false); 
//plot.setDomainAxis(1, axis); 
plot.setRangeAxis(1, axis2); 
plot.setDataset(1, dataset2); 
plot.mapDatasetToRangeAxis(1, 1); 

final XYItemRenderer renderer = plot.getRenderer(); 
renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance()); 
if (renderer instanceof StandardXYItemRenderer) { 
    final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; 
    //rr.setPlotShapes(true); 

    rr.setShapesFilled(true); 
    //renderer.setSeriesStroke(0, new BasicStroke(2.0f)); 
    //renderer.setSeriesStroke(1, new BasicStroke(2.0f)); 
    } 

final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer(); 
renderer2.setSeriesPaint(0, Color.black); 
//renderer2.setPlotShapes(true); 
renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance()); 
plot.setRenderer(1, renderer2); 

final DateAxis axis = (DateAxis) plot.getDomainAxis(); 

这就是我想要做的。正如你所看到的,我有两个x轴,但只有一个数据集,这是问题所在。我已经做到了与下面的代码图像中所示的例子中:

final XYPlot plot = chart.getXYPlot(); 

    plot.setBackgroundPaint(Color.lightGray); 
    plot.setDomainGridlinePaint(Color.white); 
    plot.setRangeGridlinePaint(Color.white); 
    plot.setDomainCrosshairVisible(true); 
    plot.setRangeCrosshairVisible(false); 


    final XYItemRenderer renderer = plot.getRenderer(); 
    if (renderer instanceof StandardXYItemRenderer) { 
     final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; 
     //rr.setPlotShapes(true); 
     rr.setShapesFilled(true); 
     renderer.setSeriesStroke(0, new BasicStroke(2.0f)); 
     renderer.setSeriesStroke(1, new BasicStroke(2.0f)); 
     } 

    final DateAxis axis = (DateAxis) plot.getDomainAxis(); 

    final DateAxis xAxis2 = (DateAxis) plot.getDomainAxis(); 
    plot.setDomainAxis(1, xAxis2); 
    plot.mapDatasetToDomainAxis(1, 1); 

Example of the output that I want

+0

请修改您的问题以包含展示您描述的问题的[sscce](http://sscce.org/)。这[重复的问题](http://stackoverflow.com/q/13563997/230513)是不完整的。 – trashgod

+0

感谢您的评论。现在可以吗?还是我应该做更多的改变? –

回答

5

是可以添加的二次X(域)轴。为了一个二级域名轴添加到您链接到添加以下代码演示:你有一个全球化志愿服务青年后

final NumberAxis xAxis2 = new NumberAxis("Secondary X Axis"); 
plot.setDomainAxis(1, xAxis2); 
plot.mapDatasetToDomainAxis(1, 1); 

到构造的情节(plot

这将允许你同时显示的数据集因此:

enter image description here

+0

非常有用您的帖子,但我怎么能指定第一个轴是为第一个数据集和第二个轴为另一个数据集? –

+1

@kostaspapadopoulos这是[mapDatasetToDomainAxis]的目的(http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/XYPlot.html#mapDatasetToDomainAxis%28int,%20int%29) – GrahamA

+0

谢谢你的帮助!!!正如你可以看到我试图做mapDataset,但结果没有数据集映射到一个轴。我已经发布了代码。 –

0

随着我什么,我得到之前发布的代码是这样的

enter image description here