Rendering Timescaled Gantt Chart Views

Microsoft Project lets users set the timescale that a Gantt chart is displayed in. (The timescale is indicated at the top of the Gantt chart view.) They can have up to three tiers of timescale to give them exactly the time resolution they need.

Aspose.Tasks for Java supports this feature and lets you render Gantt charts with different timescale settings. Gantt charts can be rendered to one page image using these options.

Setting Timescales and Saving to an Image

The ImageSaveOptions class' Timescale property determines a project’s timescale settings. The timescale is set to Days by default. The other options are Month and Third of Months.

The following code sample:

  1. Reads a project file.
  2. Sets each different timescale setting.
  3. Saves the file to disk as a JPG.
Project project = new Project("NewProductDev.mpp");
// Save to one page image (Timescale.days by default)"NewProductDevDays.jpeg", new ImageSaveOptions(SaveFileFormat.JPEG));
// Save to one page image (Timescale.ThirdsOfMonths)
ImageSaveOptions options = new ImageSaveOptions(SaveFileFormat.JPEG);
options.setTimescale(Timescale.ThirdsOfMonths);"NewProductDevThirdsOfMonths.jpeg", options);
// Save to one page image (Timescale.Months)
options.setTimescale(Timescale.Months);"NewProductDevMonths.jpeg", options);

Rolling Up Gantt Bars During Rendering

If the SaveOptions.RollUpGanttBars property is set to true, any summary task in the project is marked by its visible subtasks, keeping into account the Task.HideBar property when rendering. (If Task.HideBar = true, the bar is not rendered on the Gantt chart.) If SaveOptions.RollUpGanttBars = false, the subtask will be shown on the summary task anyway, provided that subtask.IsRollup and subtask.ParentTask.IsRollup are set to true. Also, if SaveOptions.DrawNonWorkingTime (default value = true) is set to false, non-working time will not be shown on the Gantt chart.

Example 1

PdfSaveOptions options = new PdfSaveOptions();
options.PresentationFormat = PresentationFormat.GanttChart;
options.FitContent = true;
options.RollUpGanttBars = false;
options.DrawNonWorkingTime = false;
options.PageSize = PageSize.A3;
string file = Path.Combine(Common.TestdataPath + "Rendering\\", fileName);
string resFile = Path.Combine(resultFolder, fileName.Replace(".mpp", ".pdf"));
Project project = new Project(file); // We can read a project without the ProjectReader now.
project.Save(resFile, options);

Output from example 1

Gannt chart PDF example

Example 2

In this example, SaveOptions.RollUpGanttBars = true, SaveOptions.DrawNonWorkingTime = true, and task.HideBar = false for hidden tasks.

PdfSaveOptions options = new PdfSaveOptions();
Project project = new Project("RollUpGanttBars.Mpp"); // We can read a project without the ProjectReader now."RollUpGanttBars.pdf", options);

Output from example 2

export MPP as Gannt chart PDF

Customizing Text with Task Bars

In this example, the text on the right of the task bar can be customized by delegate instance. Here we customize text on the critical tasks.

long OneSec = 10000000;//microsecond * 10
long OneMin = 60 * OneSec;
long OneHour = 60 * OneMin;
Project project = new Project();
Task task1 = project.addTask("Task 1");
Task task2 = project.addTask("Task 2");
double dDuration = OneHour * 8.0 * 3.0; //3 days tasks
task1.setDuration((long) dDuration);
task1.setDuration((long) dDuration);
TaskLink link = new TaskLink(task1, task2, TaskLinkType.FinishToStart);
Task task3 = project.addTask("Task 3");
Resource rsc1 = project.addResource("Resource 1");
Resource rsc2 = project.addResource("Resource 2");
Resource rsc3 = project.addResource("Resource 3");
project.addResourceAssignment(task1, rsc1);
project.addResourceAssignment(task2, rsc2);
project.addResourceAssignment(task3, rsc3);
SaveOptions options = new PdfSaveOptions();
BarStyle style = new BarStyle();
style.setBarTextConverter(new BarStyle.TaskToBarTextConverter() {
  public String invoke(Task task) {
     return "task " + task.getName() + "is on critical path";
} );
BarStyle style2 = new BarStyle();
options.setBarStyles(new ArrayList<BarStyle>());
options.getBarStyles().add(style2);"result2.pdf", options);

Align Cell Contents

Text can be aligned by the GanttChartColumn.StringAlignment, ResourceViewColumn.StringAlignment properties. Alignment by default is StringAlignment.Near (left).

Project project = new Project("RenderMe.mpp"); // attached test project
SaveOptions options = new PdfSaveOptions();
GanttChartColumn col = (GanttChartColumn) options.getView().getColumns().get(2);// as GanttChartColumn;
col.setStringAlignment(1); // center
col = (GanttChartColumn) options.getView().getColumns().get(3);
col.setStringAlignment(2); // far
col = (GanttChartColumn) options.getView().getColumns().get(4);
col.setStringAlignment(2); // far"result GanttChart.pdf", options);
ResourceViewColumn col1 = (ResourceViewColumn) options.getView().getColumns().get(2);
col1 = (ResourceViewColumn) options.getView().getColumns().get(3);
col1 = (ResourceViewColumn) options.getView().getColumns().get(4);
col1.setStringAlignment(2);"result ResourceSheet.pdf", options);

Changing Gantt Chart Bars Color Gradient

Microsoft Project allows users to render the output of Gantt charts using gradient colors. The same functionality is provided by Aspose.Tasks as shown in the following sample code.

Project project = new Project("NewProductDev.mpp");
SaveOptions options = new XamlOptions();
options.setUseGradientBrush(false);"solid.xaml", options);
options.setUseGradientBrush(true);"gradient.xaml", options);

Setting Start date of Gantt Chart View

The TimeScaleStart property exposed by Prj class allows to set the start date of Gantt chart view. However, if the saved file is opened by MSP in maximized window mode then the timescale start date will not match.

Project project = new Project("REL_Constr.mpp");
project.set(Prj.TIMESCALE_START, new Date(115, 2, 3));"temp.mpp", SaveFileFormat.MPP);