JFreeChart具有双x轴

时间:2012-11-26 08:16:24

标签: jfreechart

我想问一下是否有可能有一个双x轴(或y轴,但因为它是一个时间序列数据集,我专注于双x轴)的时间序列图表。我有两个数据文件,其中测量是在一分钟或一整天内完成的。结果我不能有一个x轴。因此,一种解决方案是将一个数据文件转换为另一个数据文件的格式或具有两个不同的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

2 个答案:

答案 0 :(得分:5)

是的,您可以添加辅助X(域)轴。要将辅助域轴添加到您链接的演示,请添加以下代码:

final NumberAxis xAxis2 = new NumberAxis("Secondary X Axis");
plot.setDomainAxis(1, xAxis2 );
plot.mapDatasetToDomainAxis(1, 1);
在您对绘图(plot

进行参考后,

到构造函数

这将允许您显示两个数据集:

enter image description here

答案 1 :(得分:0)

我之前发布的代码就是这样的

enter image description here