Back to Devexpress

Task Baselines

windowsforms-401939-controls-and-libraries-gantt-control-task-baselines.md

latest13.1 KB
Original Source

Task Baselines

  • Sep 16, 2024
  • 6 minutes to read

The control supports task baselines — a task’s start and finish dates, and its duration captured at a certain moment. A user can compare a task’s current date and duration with its baselines to track changes as the project progresses. Baselines can be used to indicate a schedule agreed at the start of a project.

Tip

Run the following demo to see baselines: Startup Plan.

Enable Baselines and Bind to Data Source

To display baselines in the chart, enable the ShowBaselines option. To specify data source fields that contain baselines, use the following properties:

Use the BaselineFinishDateFieldName or BaselineDurationFieldName property, depending on the data source and the fields it contains. If you specify both properties, the control uses the finish date.

Example: Reset Baselines

The example below shows how to display a command in the ribbon that resets all the baselines.

The code calls the DoOperation method to enumerate tasks and reset baselines. Use the control’s NodesIterator property to access the enumerator.

The following code uses a BarButtonItem and handles its ItemClick event to display a command in the ribbon:

csharp
using System;
using System.ComponentModel;
using System.Collections.Generic;
//Enable baselines.
ganttControl1.OptionsView.ShowBaselines = true;
//Specify data fields that contain baselines.
ganttControl1.ChartMappings.BaselineDurationFieldName = "BaselineDuration";
ganttControl1.ChartMappings.BaselineStartDateFieldName = "BaselineStartDate";
ganttControl1.ChartMappings.BaselineFinishDateFieldName = "BaselineFinishDate";

ganttControl1.TreeListMappings.KeyFieldName = "Id";
ganttControl1.TreeListMappings.ParentFieldName = "ParentId";
ganttControl1.ChartMappings.TextFieldName = "Name";
ganttControl1.ChartMappings.StartDateFieldName = "StartDate";
ganttControl1.ChartMappings.FinishDateFieldName = "FinishDate";
ganttControl1.ChartMappings.DurationFieldName = "Duration";
ganttControl1.ChartMappings.PredecessorsFieldName = "Predecessors";
ganttControl1.ChartMappings.ProgressFieldName = "Progress";
ganttControl1.DataSource = LoadData();
//...
public static IList<Task> LoadData() {
    var tasks = new List<Task>();
    Task softwareDevelopment = new Task("Software Development", 0, -1, DateTime.Now, 1, 24);
    Task analyseRequirements = new Task("Analyse Requirements", 1, softwareDevelopment.Id, softwareDevelopment.StartDate, 1, 100);
    Task developFunctionalSpecifications = new Task("Develop functional specifications", 2, softwareDevelopment.Id, analyseRequirements.FinishDate, 1, 100, 1);
    Task developSoftware = new Task("Develop software", 3, softwareDevelopment.Id, developFunctionalSpecifications.FinishDate, 5, 40, developFunctionalSpecifications.Id);
    Task developHelpSystem = new Task("Develop help system", 4, softwareDevelopment.Id, developFunctionalSpecifications.FinishDate, 1, 90, developFunctionalSpecifications.Id);
    Task developUserManuals = new Task("Develop user manuals", 5, softwareDevelopment.Id, developHelpSystem.FinishDate, 1, 0, developHelpSystem.Id);
    Task testSoftware = new Task("Test software", 6, softwareDevelopment.Id, developSoftware.FinishDate, 2, 0, developSoftware.Id);
    Task deployBeta = new Task("Deploy Beta", 7, softwareDevelopment.Id, testSoftware.FinishDate, 0, 0, testSoftware.Id);
    Task collectFeedback = new Task("Collect feedback", 8, softwareDevelopment.Id, deployBeta.FinishDate, 2, 0, deployBeta.Id);
    Task fixBugs = new Task("Fix bugs", 9, softwareDevelopment.Id, collectFeedback.FinishDate, 2, 0, collectFeedback.Id);
    Task incorporateFeedBack = new Task("Incorporate feedback", 10, softwareDevelopment.Id, collectFeedback.FinishDate, 3, 0, collectFeedback.Id);
    Task releaseSoftware = new Task("Release software", 11, softwareDevelopment.Id, incorporateFeedBack.FinishDate, 2, 0, fixBugs.Id, incorporateFeedBack.Id);
    Task createSoftwareMaintenanceTeam = new Task("Create software maintenance team", 12, softwareDevelopment.Id, deployBeta.FinishDate, 1, 0, developSoftware.Id);
    Task softwareDevelopmentComplete = new Task("Software development complete", 13, softwareDevelopment.Id, releaseSoftware.FinishDate, 0, 0, releaseSoftware.Id);
    softwareDevelopment.FinishDate = softwareDevelopmentComplete.FinishDate;
    tasks.AddRange(new Task[] {softwareDevelopment, analyseRequirements, developFunctionalSpecifications, developSoftware, developHelpSystem, developUserManuals,
        testSoftware,deployBeta,collectFeedback, fixBugs, incorporateFeedBack, releaseSoftware, createSoftwareMaintenanceTeam, softwareDevelopmentComplete });
    return tasks;
}
//Respond to clicks on the 'Set Baselines' ribbon command (created in the designer).
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    ganttControl1.NodesIterator.DoOperation(node => {
        //Reset baselines.
        node.SetValue("BaselineStartDate", node.GetValue("StartDate"));
        node.SetValue("BaselineFinishDate", node.GetValue("FinishDate"));
    });
    ganttControl1.RefreshDataSource();
}
//...
public class Task{
    public Task(string name, int id, int parentId, DateTime start, int duration, double progress, params int[] predecessors) {
        Name = name;
        Id = id;
        ParentId = parentId;
        StartDate = start;
        Duration = TimeSpan.FromDays(duration);
        FinishDate = start + Duration;
        Progress = progress;
        Predecessors = new BindingList<int>();
        foreach (int predecessor in predecessors) {
           Predecessors.Add(predecessor);
        }
        //Initialize baselines.
        BaselineFinishDate = FinishDate;
        BaselineStartDate = StartDate;
    }
    //Declare baseline data fields.
    public DateTime BaselineStartDate { get; set; }
    public DateTime BaselineFinishDate { get; set; }
    public TimeSpan Duration { get; set; }

    public int Id { get; set; }
    public int ParentId { get; set; }
    public BindingList<int> Predecessors { get; private set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime FinishDate { get; set; }
    public double Progress { get; set; }
}
vb
Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
'Enable baselines.
ganttControl1.OptionsView.ShowBaselines = True
'Specify data fields that contain baselines.
ganttControl1.ChartMappings.BaselineDurationFieldName = "BaselineDuration"
ganttControl1.ChartMappings.BaselineStartDateFieldName = "BaselineStartDate"
ganttControl1.ChartMappings.BaselineFinishDateFieldName = "BaselineFinishDate"

ganttControl1.TreeListMappings.KeyFieldName = "Id"
ganttControl1.TreeListMappings.ParentFieldName = "ParentId"
ganttControl1.ChartMappings.TextFieldName = "Name"
ganttControl1.ChartMappings.StartDateFieldName = "StartDate"
ganttControl1.ChartMappings.FinishDateFieldName = "FinishDate"
ganttControl1.ChartMappings.DurationFieldName = "Duration"
ganttControl1.ChartMappings.PredecessorsFieldName = "Predecessors"
ganttControl1.ChartMappings.ProgressFieldName = "Progress"
ganttControl1.DataSource = LoadData()
'...
Public Shared Function LoadData() As IList(Of Task)
    Dim tasks = New List(Of Task)()
    Dim softwareDevelopment As New Task("Software Development", 0, -1, DateTime.Now, 1, 24)
    Dim analyseRequirements As New Task("Analyse Requirements", 1, softwareDevelopment.Id, softwareDevelopment.StartDate, 1, 100)
    Dim developFunctionalSpecifications As New Task("Develop functional specifications", 2, softwareDevelopment.Id, analyseRequirements.FinishDate, 1, 100, 1)
    Dim developSoftware As New Task("Develop software", 3, softwareDevelopment.Id, developFunctionalSpecifications.FinishDate, 5, 40, developFunctionalSpecifications.Id)
    Dim developHelpSystem As New Task("Develop help system", 4, softwareDevelopment.Id, developFunctionalSpecifications.FinishDate, 1, 90, developFunctionalSpecifications.Id)
    Dim developUserManuals As New Task("Develop user manuals", 5, softwareDevelopment.Id, developHelpSystem.FinishDate, 1, 0, developHelpSystem.Id)
    Dim testSoftware As New Task("Test software", 6, softwareDevelopment.Id, developSoftware.FinishDate, 2, 0, developSoftware.Id)
    Dim deployBeta As New Task("Deploy Beta", 7, softwareDevelopment.Id, testSoftware.FinishDate, 0, 0, testSoftware.Id)
    Dim collectFeedback As New Task("Collect feedback", 8, softwareDevelopment.Id, deployBeta.FinishDate, 2, 0, deployBeta.Id)
    Dim fixBugs As New Task("Fix bugs", 9, softwareDevelopment.Id, collectFeedback.FinishDate, 2, 0, collectFeedback.Id)
    Dim incorporateFeedBack As New Task("Incorporate feedback", 10, softwareDevelopment.Id, collectFeedback.FinishDate, 3, 0, collectFeedback.Id)
    Dim releaseSoftware As New Task("Release software", 11, softwareDevelopment.Id, incorporateFeedBack.FinishDate, 2, 0, fixBugs.Id, incorporateFeedBack.Id)
    Dim createSoftwareMaintenanceTeam As New Task("Create software maintenance team", 12, softwareDevelopment.Id, deployBeta.FinishDate, 1, 0, developSoftware.Id)
    Dim softwareDevelopmentComplete As New Task("Software development complete", 13, softwareDevelopment.Id, releaseSoftware.FinishDate, 0, 0, releaseSoftware.Id)
    softwareDevelopment.FinishDate = softwareDevelopmentComplete.FinishDate
    tasks.AddRange(New Task() {softwareDevelopment, analyseRequirements, developFunctionalSpecifications, developSoftware, developHelpSystem, developUserManuals,
                   testSoftware, deployBeta, collectFeedback, fixBugs, incorporateFeedBack, releaseSoftware, createSoftwareMaintenanceTeam, softwareDevelopmentComplete})
    Return tasks
End Function
'Respond to clicks on the 'Set Baselines' ribbon command (created in the designer).
Private Sub barButtonItem1_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem1.ItemClick
    ganttControl1.NodesIterator.DoOperation(Sub(node)
        'Reset baselines.
        node.SetValue("BaselineStartDate", node.GetValue("StartDate"))
        node.SetValue("BaselineFinishDate", node.GetValue("FinishDate"))
    End Sub)
    ganttControl1.RefreshDataSource()
End Sub
'...
Public Class Task
    Public Sub New(ByVal taskName As String, ByVal taskId As Integer, ByVal taskParentId As Integer, ByVal start As DateTime,
                   ByVal taskDuration As Integer, ByVal taskProgress As Double, ParamArray ByVal predecessorIds() As Integer)
        Me.Name = taskName
        Me.Id = taskId
        Me.ParentId = taskParentId
        StartDate = start
        Me.Duration = TimeSpan.FromDays(taskDuration)
        FinishDate = start.Add(Me.Duration)
        Me.Progress = taskProgress
        Me.Predecessors = New BindingList(Of Integer)()
        For Each predecessor As Integer In predecessorIds
            Me.Predecessors.Add(predecessor)
        Next predecessor
        Me.BaselineFinishDate = Me.FinishDate
        Me.BaselineStartDate = Me.StartDate
    End Sub
    'Declare baseline data fields.
    Public Property BaselineStartDate() As DateTime
    Public Property BaselineFinishDate() As DateTime

    Public Property Id() As Integer
    Public Property ParentId() As Integer
    Private privatePredecessors As BindingList(Of Integer)
    Public Property Predecessors() As BindingList(Of Integer)
        Get
            Return privatePredecessors
        End Get
        Private Set(ByVal value As BindingList(Of Integer))
            privatePredecessors = value
        End Set
    End Property
    Public Property Name() As String
    Public Property StartDate() As DateTime
    Public Property FinishDate() As DateTime
    Public Property Duration() As TimeSpan
    Public Property Progress() As Double
End Class