Customizing Gantt charts

Describes the sample applications used in this documentation.

Describes the sample applications on which this documentation is based.

Explains how to run the Custom Gantt example.

Describes the customizations possible using the API.

Explains how to add a user-defined priority property to each activity in the Custom Gantt chart example and then how to create all activity instances with a default priority value by using a factory.

Customization examples

The information in this section is based on the Custom Gantt Chart example.

The Rendering a Custom Data Model example is supplied with JViews Gantt to illustrate several advanced customization techniques that can be applied to the charts. The Rendering a Custom Data Model example application extends the Activity Chart (SDK) example. This application file simplifies the source code so that it only contains the customizations that override the default behavior of the Gantt chart.

Running the Custom Gantt example

The source code file of the Custom Gantt example application is named CustomGanttExample.java and can be found here:

<installdir>/jviews-gantt/samples/customData/src/customData/CustomGanttExample.java

To run the example as an application:

  1. Make sure that the Ant utility is properly configured. If not, read Starting the samples for instructions on how to configure Ant for JViews Gantt:

  2. Go to the directory where the example is installed and type:

    ant run

Customization overview

The following figure shows what the application looks like when the Custom Gantt example is launched.

This example illustrates several techniques that you can use to customize the Gantt and Schedule charts for your own application needs.

The following customizations are discussed in subsequent sections:

  • Each leaf activity is rendered with a customized graphic, which represents the activity priority as a horizontal yellow bar. See Customizing activity rendering.

  • Each parent activity is displayed in the tree column with a custom icon that depends on whether the activity is expanded or collapsed. See Customizing table columns.

  • A column has been added to the table to display the priority level of each activity. Column rendering has been customized so that priority levels are displayed in different colors depending on their value. A slider has been substituted for the default text field mechanism for editing the priority values. See Customizing table columns.

Customizing the Gantt data model

In this section, you learn how to add a user-defined priority property to each activity in the Custom Gantt chart example and then how to create all activity instances with a default priority value by using a factory. You can apply the concepts set out through the tutorial to add your own properties to activities, resources, constraints, or reservations.

Defining your own priority property

The SimpleEngineeringProject class implements the data model for this sample. It can be found at:

<installdir>/jviews-gantt/samples/customData/src/shared/data/SimpleEngineeringProject.java

This class populates the data model with activities, resources, constraints, and reservations that are created by the data factories of the chart. Because the default data factories of IlvHierarchyChart create "general" data objects, the SimpleEngineeringProject data model is populated with instances of IlvGeneralActivity, IlvGeneralResource, IlvGeneralConstraint, and IlvGeneralReservation.

To define your own priority property:

  1. Add a numerical priority property to any activity in the data model by simply using code like this:

    IlvGeneralActivity activity = ...

    activity.setProperty("priority", new Integer(5));

    Encapsulate this behavior in the static utility methods of the class PriorityProperty:

  2. public static final String PRIORITY_PROPERTY = "priority";

    public static final int HIGHEST_PRIORITY = 1;

    public static final int LOWEST_PRIORITY = 10;

     

    ...

     

    public static void setPriority(IlvActivity activity, int priority) {

      setPriority(activity, new Integer(priority));

    }

     

    public static void setPriority(IlvActivity activity, Number priority) {

      if (!(activity instanceof IlvUserPropertyHolder))

        throw new IllegalArgumentException("Priority cannot be set on " +

    activity);

      if (priority == null)

        throw new IllegalArgumentException("Priority cannot be null");

      int priorityValue = priority.intValue();

      if (priorityValue > LOWEST_PRIORITY)

        priority = new Integer(LOWEST_PRIORITY);

      if (priorityValue < HIGHEST_PRIORITY)

        priority = new Integer(HIGHEST_PRIORITY);

      ((IlvUserPropertyHolder)activity).setProperty(PRIORITY_PROPERTY,

    priority);

    }

    This ensure that you always use the “priority” property name, that you handle errors in the unlikely case that the activity does not support user-defined properties, and that the priority value is within an acceptable range, you

  3. Use the following code to set the value of an activity property instead:

    IlvGeneralActivity activity = ...

    PriorityProperty.setPriority(activity, 5);

Creating activity instances

Now that you have a method that helps you to set a numerical priority property on an activity instance, you need to have the example application create all activity instances with a default priority value. You do this by creating a custom activity factory and then by telling the chart to use this factory instead of the default.

  1. Implemented a custom activity factory by creating the CustomGanttExample.CustomActivityFactory inner class and its createActivityImpl method, which creates activity instances of CustomActivity upon request by the application:

    Example   Creating an activity factory to instantiate activities

    class CustomActivityFactory extends SimpleActivityFactory {

    ...

      protected IlvActivity createActivityImpl(IlvTimeInterval interval,

                                               int activityNum) {

        IlvGeneralActivity activity =

          new IlvGeneralActivity("CA #" + activityNum,

                                 "Custom Activity #" + activityNum,

                                 interval);

        PriorityProperty.setPriority(activity, PriorityProperty.DEFAULT_PRIORITY);

        return activity;

      }

  2. Get the Custom Gantt chart sample to use the new factory by overriding its customizeFactories method:

    Example   Overriding the customizeFactories method to use the new factory

    protected void customizeFactories() {

      super.customizeFactories();

      // Change the default activity factory.

      chart.setActivityFactory(new CustomActivityFactory(chart));

      ...