4

我想问一下是否有可能有一个带有双 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);

我想要的输出示例

4

2 回答 2

5

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

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

在你有对 Plot ( plot)的引用之后给构造函数

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

在此处输入图像描述

于 2012-11-26T09:28:25.377 回答
0

使用我之前发布的代码,我得到的是这样的

在此处输入图像描述

于 2012-11-27T07:21:01.173 回答