2

当我创建一个需要带有周周期的轴的图表时,我遇到了这个问题。

当我设置 with 的刻度单位时DateAxisnew DateTickUnit(DateTickUnitType.Day, 7)它每 7 天显示一次刻度线。但是,刻度线的日期并非从一周的第一天开始。您可以在屏幕截图中观察此行为。

青色线05-01 w18(May. 1, Week 18)在刻度线下方w18(Week 18),这是因为刻度线的日期w18实际上是 5 月 2 日,也就是星期三。

这使得图表看起来不正确,因为人们倾向于认为每个刻度都应该是一周的开始。

检查源代码后,我发现DateAxis一周不支持这种行为(反正没有周类型)。

我不能创建另一个DateTickUnitType,因为私有构造函数和correctTickDateForPosition()方法DateAxis也是私有的。我试图覆盖nextStandardDate()但没有得到满意的结果。

我怎样才能让DateAxis总是从一周的第一天开始画刻度线?


这是屏幕截图和示例代码

带有 DateAxis 在周期间的图表

需要 JFreeChart/JCommon

public class Main extends ApplicationFrame {

  public Main (String title) throws ParseException {
    super(title);
    JPanel chartPanel = createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
    setContentPane(chartPanel);
  }

  private static JFreeChart createChart(XYDataset dataset) {
    JFreeChart chart = ChartFactory.createXYLineChart("Week period in DateAxis", "X", "Y", 
        dataset, PlotOrientation.VERTICAL, true, true, false);

    DateAxis x = new DateAxis("X");
    x.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 1, new SimpleDateFormat("MMM.")));

    DateAxis y = new DateAxis("Y");
    y.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 7, new SimpleDateFormat("MM-dd 'W'w.")));

    XYPlot plot = chart.getXYPlot();
    plot.setDomainAxis(x);
    plot.setRangeAxis(y);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinesVisible(true);
    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("{2}", 
        new SimpleDateFormat("MMM."), new SimpleDateFormat("MM-dd 'w'w")));
    renderer.setBaseItemLabelsVisible(true);
    return chart;
  }

  private static XYDataset createDataset() throws ParseException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");

    String[] allDate = new String[]{
        "2012/03/27", "2012/04/03", "2012/04/10", 
        "2012/04/17", "2012/04/24", "2012/05/01"};

    String dateY1 = "2012/01/15";
    String dateY2 = "2012/02/15";
    String dateY3 = "2012/03/15";
    String dateY4 = "2012/04/15";
    String dateY5 = "2012/05/15";
    String dateY6 = "2012/06/15";

    XYSeriesCollection dataset = new XYSeriesCollection();
    for (String date : allDate) {
      XYSeries series = new XYSeries(date);

      series.add(df.parse(dateY1).getTime(), df.parse(date).getTime());
      series.add(df.parse(dateY2).getTime(), df.parse(date).getTime());
      series.add(df.parse(dateY3).getTime(), df.parse(date).getTime());
      series.add(df.parse(dateY4).getTime(), df.parse(date).getTime());
      series.add(df.parse(dateY5).getTime(), df.parse(date).getTime());
      series.add(df.parse(dateY6).getTime(), df.parse(date).getTime());
      dataset.addSeries(series);
    }
    return dataset;
  }

  public static JPanel createDemoPanel() throws ParseException {
    JFreeChart chart = createChart(createDataset());
    return new ChartPanel(chart);
  }

  public static void main(String[] args) throws ParseException {
    Main demo = new Main("Test");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
  }
}
4

2 回答 2

6

这是我DateAxis支持的周期间的版本(仅限)。

public static class WeeklyDateAxis extends DateAxis {

  private static final long serialVersionUID = 1L;
  private Locale locale;

  public WeeklyDateAxis(String label, TimeZone zone, Locale locale) {
    super(label, zone, locale);
    this.locale = locale;
  }

  @Override
  protected Date previousStandardDate(Date date, DateTickUnit unit) {
    Calendar cal = Calendar.getInstance(getTimeZone(), locale);
    cal.setTime(date);
    resetFieldBelowDay(cal);

    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    return cal.getTime();
  }

  @Override
  protected Date nextStandardDate(Date date, DateTickUnit unit) {
    Date previous = previousStandardDate(date, unit);
    Calendar cal = Calendar.getInstance(getTimeZone(), locale);
    cal.setTime(previous);
    resetFieldBelowDay(cal);

    cal.add(Calendar.WEEK_OF_YEAR, 1);
    return cal.getTime();
  }

  private void resetFieldBelowDay(Calendar cal) {
    cal.clear(Calendar.MILLISECOND);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MINUTE);
    cal.set(Calendar.HOUR_OF_DAY, 0);
  }
}

解释 :

计算刻度时DateAxis,它将通过调用从最低值开始

nextStandardDate(getMinimumDate(), unit);

, 哪里getMinimumDate()是图表的边缘值。然后它将继续使用日期的最后一个刻度作为输入来计算下一个日期,直到它达到最高可用日期。

所以我将时间设置为 中的一周的第一天previousStandardDate(),然后每次计算下一个刻度时,我都会在 的结果中添加一周previousStandardDate()

resetFieldBelowDay()部分只是为了消除一些可能导致线剂量与刻度线不一致的噪声数据。

这是我用来验证结果的另一个数据集,您可以简单地用我提供的示例代码替换它。

private static XYDataset createDataset() throws ParseException {
  SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");

  String[][] allDate = new String[][] {
      {"2012/03/25", "2012/03/29", "2012/03/28", "2012/03/27", "2012/03/27", "2012/03/27"},
      {"2012/04/01", "2012/04/02", "2012/04/06", "2012/04/06", "2012/04/06", "2012/04/06"},
      {"2012/04/11", "2012/04/12", "2012/04/08", "2012/04/10", "2012/04/10", "2012/04/10"},
      {"2012/04/15", "2012/04/14", "2012/04/15", "2012/04/18", "2012/04/19", "2012/04/19"},
      {"2012/05/01", "2012/05/02", "2012/05/08", "2012/05/04", "2012/05/04", "2012/05/04"},
      {"2012/05/12", "2012/05/12", "2012/05/18", "2012/05/14", "2012/05/14", "2012/05/14"},
      {"2012/05/22", "2012/05/22", "2012/05/28", "2012/05/28", "2012/05/28", "2012/05/30"},
  };

  String dateY1 = "2012/01/15";
  String dateY2 = "2012/02/15";
  String dateY3 = "2012/03/15";
  String dateY4 = "2012/04/15";
  String dateY5 = "2012/05/15";
  String dateY6 = "2012/06/15";

  XYSeriesCollection dataset = new XYSeriesCollection();
  for (String[] dateOfOneSeries : allDate) {
    XYSeries series = new XYSeries(dateOfOneSeries[0]);

    series.add(df.parse(dateY1).getTime(), df.parse(dateOfOneSeries[0]).getTime());
    series.add(df.parse(dateY2).getTime(), df.parse(dateOfOneSeries[1]).getTime());
    series.add(df.parse(dateY3).getTime(), df.parse(dateOfOneSeries[2]).getTime());
    series.add(df.parse(dateY4).getTime(), df.parse(dateOfOneSeries[3]).getTime());
    series.add(df.parse(dateY5).getTime(), df.parse(dateOfOneSeries[4]).getTime());
    series.add(df.parse(dateY6).getTime(), df.parse(dateOfOneSeries[5]).getTime());
    dataset.addSeries(series);
  }
  return dataset;
}

结果 :

结果

于 2012-06-15T16:07:14.377 回答
1

通过覆盖我设法调整刻度线previousStandardDateDateAxis星期几。

在您的示例中,数据显示为星期二,因此在每周第一天下方的代码中Calendar设置为星期二,这将数据线与刻度线对齐。

DateAxis y = new DateAxis("Y") {
  @Override
  protected Date previousStandardDate(Date date, DateTickUnit unit) {
    Date prevDate = super.previousStandardDate(date, unit);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(prevDate);

    int showDayOfWeek = Calendar.TUESDAY;
    calendar.setFirstDayOfWeek(showDayOfWeek);
    if (showDayOfWeek != calendar.get(Calendar.DAY_OF_WEEK)) {
      calendar.set(Calendar.DAY_OF_WEEK, showDayOfWeek);
    }

    return calendar.getTime();
  }
};

[编辑:]我刚刚意识到您实际上并不想对齐刻度线,而是在一周的实际第一天显示它们。解决方案是

DateAxis y = new DateAxis("Y") {    
  @Override
  protected Date previousStandardDate(Date date, DateTickUnit unit) {
    Date prevDate = super.previousStandardDate(date, unit);

    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    calendar.setTime(prevDate);

    int firstDayOfWeek = calendar.getFirstDayOfWeek();
    if (firstDayOfWeek != calendar.get(Calendar.DAY_OF_WEEK)) {
      calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
    }

    return calendar.getTime();
  }
};
于 2012-06-15T13:59:18.077 回答