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

时间:2014-12-16 11:58:15

标签: java jfreechart

我是JFreeChart的新手。我的要求是使用假设3个变量显示X轴(时间轴)如下(时间范围可根据用户输入进行配置):

3rdAug-8thAug..10thAug-15thAug .. [等等]

目前我的图表的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轴上显示的所需值?

2 个答案:

答案 0 :(得分:0)

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

Valuexis domainAxis = plot.getDomainAxis();

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

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

答案 1 :(得分:0)

您希望使用JFreeChart的TimeLine接口限制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,
- 马丁