且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有一种解决方法可以从一周的第一天开始制作DateAxis抽奖活动?

更新时间:2023-11-26 18:59:10

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

Here is the my version of the DateAxis that supported week period (only).

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 计算滴答时,它将从最低值开始,通过调用

Explanation :

When DateAxis calculate ticks, it will start from the lowest value by calling

nextStandardDate(getMinimumDate(), unit);

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

, where getMinimumDate() are the edge value of the chart. Then it will keep using the last tick of date as input to calculate next date until it reach the highest available date.

所以我将时间设置为星期的第一天 previousStandardDate(),然后每当我计算下一个滴答时,我会在 previousStandardDate()的结果中添加一周。

So I set the time to first day of week in previousStandardDate(), then every time I calculate the next tick, I add one week to result of the previousStandardDate().

resetFieldBelowDay()部分只是删除一些噪音数据,这些数据可能会导致线条不与tick。

The resetFieldBelowDay() part are simply to remove some noise data that can cause line dose not align with the tick.

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

Here is another data set I use to validate the result, you can simply replace the it with the example code I provide in question.

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;
}



结果:



Result :