Display Multiple Columns in Rendered Gantt Chart Image

Overview

In Microsoft Project, project data is not only available in the textual form, but can also be displayed graphically as a chart. The most popular chart types are Gantt Chart, Task Usage and Resource Usage. Aspose.Tasks for .NET supports rendering of project data to a chart.

This article describes two approaches which allows to customize project columns to be included in a Gantt chart and demonstrates how to render the chart to an image.

Gantt Chart

A Gantt chart is a graphical representation of project tasks broken down by days, weeks or months. A project is composed of tasks assigned to different resources. An individual task may be divided into sub-tasks as part of tasks management. Every task has a start date and end date that determines its duration. A Gantt chart in Microsoft Project gives a quick view of such project data. This screenshot shows a typical Gantt chart in Microsoft Project:

Gantt chart represented by Microsoft Project

Gantt Chart Image in Aspose.Tasks for .NET

In Aspose.Tasks for .NET, the Project class is the main class for handling project files. The Project class exposes different overloads if “Save” method for exporting project data to different file formats. The users can configure this method to export project data to any supported format by passing parameters.

For instance, consider Save(string, SaveOptions) overload.

SaveOptions Type

The rendering behavior can be customized using properties of SaveOptions class (or one of its inheritors). For example, page size of output document can be set using SaveOptions.CustomPageSize property or SaveOptions.LegendOnEachPage can be set to define whether a legend should be displayed on each page.

1PdfSaveOptions saveOptions = new PdfSaveOptions()
2{
3   CustomPageSize = new SizeF(800, 600),
4   LegendOnEachPage = false
5};

There are at least two approaches to customize appearance of the rendered chart.

Customize Gantt Chart columns using ProjectView Type

Please note that it’s an obsolete way to customize appearance and it’s less likely that it will be extended or improved in future. The ProjectView class has limited functionality and can be used to display specific columns in the output image. A constructor of this class takes an array list of the GanttChartColumn class as its argument. The resulting ProjectView instance should be set as a value of SaveOptions.View property. The example code shows how it works:

 1//Create the view columns
 2var columns = new List<GanttChartColumn>();
 3columns.Add(new GanttChartColumn("Name", 100, new TaskToColumnTextConverter(TaskName)));
 4columns.Add(new GanttChartColumn("Notes", 100, new TaskToColumnTextConverter(TaskNotes)));
 5columns.Add(new GanttChartColumn("Resources", 200, new TaskToColumnTextConverter(TaskResources)));
 6//Create the view
 7ProjectView projectView = new ProjectView(columns);
 8
 9// Create SaveOptions
10PdfSaveOptions saveOptions = new PdfSaveOptions()
11{
12   CustomPageSize = new SizeF(800, 600),
13   View = projectView
14};
15
16// Save the project to PDF.
17project.Save("output.pdf", saveOptions);

The constructor of the GanttChartColumn class takes three arguments - the column name, width and a delegate TaskToColumnTextConverter - to convert task data to column text.

In the above code example, TaskToColumnTextConverter delegate calls three target methods, TaskName, TaskNotes and TaskResources, to convert the data in these columns to text. The three methods are implemented in the following code example.

 1/// <summary>
 2/// Converts a task's name data to column text.
 3/// </summary>
 4/// <param name="task">Current task.</param>
 5/// <returns>Column's text.</returns>
 6private string TaskName(Task task)
 7{
 8    StringBuilder res = new StringBuilder();
 9    for (int i = 1; i < task.OutlineLevel; i++)
10    {
11        res.Append("  ");
12    }
13
14    res.AppendFormat("{0}. {1}", task.Id, task.Name);
15    return res.ToString();
16}
17
18/// <summary>
19/// Converts a task's name data to column text.
20/// </summary>
21/// <param name="task">Current task.</param>
22/// <returns>Column's text.</returns>
23private string TaskNameHtml(Task task)
24{
25    StringBuilder res = new StringBuilder();
26    for (int i = 1; i < task.OutlineLevel; i++)
27    {
28        res.Append("&nbsp;&nbsp;");
29    }
30
31    res.AppendFormat("{0}. {1}", task.Id, task.Name);
32    return res.ToString();
33}
34
35/// <summary>
36/// Converts a task's notes data to column text.
37/// </summary>
38/// <param name="task">Current task.</param>
39/// <returns>Column's text.</returns>
40private string TaskNotes(Task task)
41{
42    if (task.NotesText != null)
43        return task.NotesText;
44    else
45        return string.Empty;
46}
47
48/// <summary>
49/// Converts a task's resources data to column text.
50/// </summary>
51/// <param name="task">Current task.</param>
52/// <returns>Column's text.</returns>
53private string TaskResources(Task task)
54{
55    StringBuilder res = new StringBuilder();
56    Project project = task.ParentProject;
57    bool bFirst = true;
58    foreach (ResourceAssignment assignment in project.GetResourceAssignmentsByTask(task))
59    {
60        if (assignment.Resource != null)
61        {
62            if (!bFirst)
63            {
64                res.Append(", ");
65            }
66 
67            res.Append(assignment.Resource.Name);
68            bFirst = false;
69        }
70    }
71
72    return res.ToString();
73}

The following is the image and HTML image produced with the example code:

resulting HTML exported by Aspose.Tasks 1 resulting HTML exported by Aspose.Tasks 2

Customize Gantt Chart columns using View type

Usage View class and its inheritors is a recommended way to customize charts when rendering project data. View class corresponds to view settings used in MS Project (which are stored in MPP file) and provided richer API compared to ProjectView’s API. Available properties of View class can be found in API Reference. The existing View can be taken (for projects loaded from MPP file with views saved by Microsoft Project), customized and passed to Project.Save method as demonstrated in the following code snippet:

 1Project project = new Project("input.mpp"));
 2
 3// Get existing view.
 4var view = (GanttChartView) project.Views.GetByName("&Gantt Chart");
 5
 6// Define and insert new column to the view.
 7TableField field = new TableField()
 8{
 9    AlignData = StringAlignment.Far,
10    Width = 50,
11    Field = Field.TaskName,
12    Title = "Task name"
13};
14
15view.Table.TableFields.Insert(1, field);
16PdfSaveOptions saveOptions = new PdfSaveOptions()
17{
18    ViewSettings = view
19};
20
21project.Save("output.pdf", saveOptions);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.