Since 1992


                   Programming domain objects
                   About JavaGantt model
                   Undo / Redo support
                   Localization support
                   Building a treetable
                   Painting a chart
                   Working with the time scale
                   JavaGantt actions
       Promote your software
 Contact Us
© 2012 BeeSoft ®

Working with the time scale

The Gantt chart time scale is significant for gantt chart span, for the chart header content and also for chart clarity and readability.

All classes of the gantt chart header and the time scale are in the package eu.beesoft.gantt.chart:

  • TimeUnit is an enumeration for time unit granularity. It contains constants from seconds to year and some useful methods.
  • TimeSpan envelopes date range (start time - end time). Instances of TimeSpan are created by ChartComponent in its updateChart() method. They contain information about start and end date of time span, whether it is a holiday or weekend, x-position of this time span in chart component coordinates and width of this time span in pixels.
  • ChartHeader paints headers above the ChartComponent instance. This component contains two headers - major and minor - and their models.
  • HeaderModel is used to paint major or minor header for ChartComponent. It contains data for header columns.
  • HeaderColumn is a subclass of javax.swing.table.TableColumn to hold list of TimeSpan instances. These instances are used to calculate sizes of the column.

There is very low probability you need to customize these classes. They are created by JavaGantt in initialization phase. All you need to do is to use them.

Zoom policy

The zoom policy defines the way the chart is zoomed in / out. There is defined an interface ZoomPolicy and also its default implementation built in ChartComponent. But maybe you will need create your own.

The ZoomPolicy requires to implement 4 methods:

  • public int getStepCount () - returns the step count for this policy
  • public TimeUnit getMajorStep (int index) - returns time granularity for major header
  • public TimeUnit getMinorStep (int index) - returns time granularity for minor header
  • public int getTimeSpanWidth (int index) - returns width (in pixels) of TimeSpan instances for given step.

They could be implemented in this manner, for example:

public class MyZoom implements ZoomPolicy {

	private TimeUnit[] majorStep;
	private TimeUnit[] minorStep;
	private int[] widthStep;

	public Zoom () {
		majorStep = new TimeUnit[5];
		minorStep = new TimeUnit[5];
		widthStep = new int[5];

		majorStep[0] = TimeUnit.WEEK;
		minorStep[0] = TimeUnit.DAY;
		widthStep[0] = 80;

		majorStep[1] = TimeUnit.MONTH;
		minorStep[1] = TimeUnit.DAY;
		widthStep[1] = 40;

		majorStep[2] = TimeUnit.MONTH;
		minorStep[2] = TimeUnit.WEEK;
		widthStep[2] = 20;

		majorStep[3] = TimeUnit.YEAR;
		minorStep[3] = TimeUnit.WEEK;
		widthStep[3] = 10;

		majorStep[4] = TimeUnit.YEAR;
		minorStep[4] = TimeUnit.MONTH;
		widthStep[4] = 5;

	public int getStepCount () {
		return majorStep.length;

	public TimeUnit getMajorStep (int index) {
		return majorStep[index];

	public TimeUnit getMinorStep (int index) {
		return minorStep[index];

	public int getTimeSpanWidth (int index) {
		return widthStep[index];

How to customize text for chart header

When HeaderModel (re)constructs itself, it calls method GanttModel.getChartHeaderText (Date, TimeUnit, boolean) for each relevant date. So this method in GanttModel is the place to customize chart header texts.

What you have to do is to return text for given Date and time granularity (TimeUnit). For example, if TimeUnit is YEAR, you will return "2010" if date is of that year. The third (boolean) argument distinguishes between request for major and minor header, because you may want to display the same value differently in each other header.