JFREECHART折线图:希望X轴是日期范围的集合



我是JFreeChart的新手。我的要求是将 X 轴(时间轴)显示为如下(时间范围将根据用户输入进行配置),用于假设有 3 个变量的折线图:

8月

3日至8月8日。8月10日-8月15日 [ 等等 ]

目前我的图形的 X 轴是这样的:

1..2..3..4..

5 ..

[无法附加屏幕截图]

我的演示代码如下:

private JFreeChart createChart(final XYDataset dataset) {
    // create the chart...
    final JFreeChart chart = ChartFactory.createXYLineChart(
        "Line Chart Demo ",      // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        PlotOrientation.VERTICAL,
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );
    //  OPTIONAL CUSTOMISATION OF THE CHART...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    final XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, true);  //for line visibility
    renderer.setSeriesShapesVisible(1, false);
    plot.setRenderer(renderer);
    // change the auto tick unit selection to integer units only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
   // final Axis range = plot.get
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    // OPTIONAL CUSTOMISATION COMPLETED.
    return chart;
}

谁能在这里指出我正确的方向?如何仅获取 X 轴上显示的所需值?

域轴和范围轴之间的XYPlot不同。在您的情况下,X 轴是域轴,而 Y 轴是范围轴。

Valuexis domainAxis = plot.getDomainAxis();

您还可以设置不同的域轴:

ValueAxis dAxis = new ...
plot.setDomainAxis(dAxis);

您希望使用 JFreeChart 的时间线接口来限制 DateAxis 上显示的日期(正如 @Uli 已经指出的那样,在您的案例中,它实际上是域轴)。对于您的要求,默认实现 SegmentedTimeline 应该满足您的要求。只需配置它并将其传递到您的轴:

SegmentedTimeline timeline = new SegmentedTimeline(
    86400000l,  // segment size = one day in ms (24*3600*1000)
    5,          // include 5 segments (days)
    2);         // exclude 2 segments (days)
DateAxis axis = new DateAxis();
axis.setTimeline(timeline);

并且不要忘记将绘图配置为使用新DateAxis,因为默认情况下XYPlot使用NumberAxis

plot.setDomainAxis(axis);

hth,
-马丁

最新更新