Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:33:31+00:00 2026-06-05T18:33:31+00:00

I bumped into this problem when I created a chart that need a axis

  • 0

I bumped into this problem when I created a chart that need a axis with week period.

When I set up tick unit of DateAxis with new DateTickUnit(DateTickUnitType.Day, 7), it display tick mark every 7 days. However, the date of tick mark does not start from the first day of the week. You can observer this behavior in the screenshot.

Cyan color line 05-01 w18(May. 1, Week 18) is below the w18(Week 18) tick mark, this is because the date of tick mark w18 are actually May 2, which is Wednesday.

This make chart looks incorrect because people tend to think each tick are suppose to be the start of the week.

After I check the source code, I found out that DateAxis dose not support such behavior for week (There is no week type anyway).

I cannot to create another DateTickUnitType because the private constructor, and correctTickDateForPosition() method in DateAxis is also private. I have tried to override the nextStandardDate() but did not come out with satisfy result.

How can I make DateAxis always draw tick mark start from first day of week ?


Here is the screenshot and example code

Chart with DateAxis in week period

JFreeChart/JCommon required

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);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:33:33+00:00Added an answer on June 5, 2026 at 6:33 pm

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

    Explanation :

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

    nextStandardDate(getMinimumDate(), unit);
    

    , 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.

    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().

    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 :

    Result

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I bumped into an additional question that I needed in regards to this: Using
I'm not 100% convinced that this is a good idea, but I bumped into
I am doing some localization and have bumped into a problem that I cant
During usage of NHibernate ... One strange problem that I have bumped into was
Hi I bumped into this problem this morning (I've already found a work around)
I've bumped into a small problem that i cant seem to find the fix
I have bumped into this problem several times on the type of input data
I just bumped into this little problem and I wanted the input of other
I am trying to middle align an element, But I have bumped into this
I accidentally bumped into an ad like this: http://monotouch.net/DownloadTrial?ref=so1 . Then I start to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.