Working with Custom Bar Styles

In Microsoft Project’s Gantt chart view, tasks’ bar styles can be customized by the user to have text displayed around the bars or to have user-defined appearance of Start, Middle and End parts of a bar. Tasks’ bar styles can be customized for a specific task or for a group of tasks which meet certain criteria. This can also be achieved using Aspose.Tasks for .NET API using the GanttBarStyle. This article shows how to read and add custom bar styles using Aspose.Tasks.

Adding Custom Bar Style for a specific task

 1static void ImplementCustomBarStyle()
 2{
 3    Project project = new Project("New Project.mpp");
 4    project.RootTask.Children.Add("Task");
 5
 6    GanttChartView view = project.DefaultView as GanttChartView;
 7    GanttBarStyle custom = GetCustomBarStyle();
 8
 9    // Add the custom bar style to the custom bar collection of the project view
10    view.CustomBarStyles.Add(custom);
11
12    MPPSaveOptions options = new MPPSaveOptions();
13    options.WriteViewData = true;
14
15    project.Save("ImplementCustomBarStyle_out.mpp", options);
16}
17
18static GanttBarStyle GetCustomBarStyle()
19{
20    GanttBarStyle style = new GanttBarStyle();
21    style.ShowFor = "1";
22    style.MiddleShape = GanttBarMiddleShape.RectangleBottom;
23    style.MiddleFillPattern = GanttBarFillPattern.MediumFill;
24    style.MiddleShapeColor = Color.Blue;
25
26    style.StartShape = GanttBarEndShape.ArrowDown;
27    style.StartShapeColor = Color.Red;
28
29    style.EndShape = GanttBarEndShape.ArrowUp;
30    style.EndShapeColor = Color.Yellow;
31
32    style.LeftField = Field.TaskResourceNames;
33    style.RightField = Field.TaskName;
34    style.TopField = Field.TaskStart;
35    style.BottomField = Field.TaskFinish;
36    style.InsideField = Field.TaskDuration;
37
38    return style;
39}

Reading Custom Bar Style

 1Project project = new Project("New Project.mpp");
 2
 3GanttChartView view = project.DefaultView as GanttChartView;
 4Console.WriteLine("Custom bar styles count: {0}", view.CustomBarStyles.Count);
 5
 6GanttBarStyle style1 = view.CustomBarStyles[0];
 7
 8Console.WriteLine("Style1.LeftField is TaskDurationText : {0}", style1.LeftField.Equals(Field.TaskDurationText));
 9Console.WriteLine("Style1.RightField is TaskResourceNames : {0}", style1.RightField.Equals(Field.TaskResourceNames));
10Console.WriteLine("Style1.TopField is TaskACWP: {0}", style1.TopField.Equals(Field.TaskACWP));
11Console.WriteLine("Style1.BottomField is Undefined : {0}", style1.BottomField.Equals(Field.Undefined));
12Console.WriteLine("Style1.InsideField is Undefined : {0}", style1.InsideField.Equals(Field.Undefined));
13
14GanttBarStyle style2 = view.CustomBarStyles[1];
15Console.WriteLine("Style2.LeftField is TaskActualWork : {0}", style2.LeftField.Equals(Field.TaskActualWork));
16Console.WriteLine("Style2.RightField is TaskActualCost : {0}", style2.RightField.Equals(Field.TaskActualCost));
17Console.WriteLine("Style2.TopField is Undefined : {0}", style2.TopField.Equals(Field.Undefined));
18Console.WriteLine("Style2.BottomField is Undefined : {0}", style2.BottomField.Equals(Field.Undefined));
19Console.WriteLine("Style2.InsideField is Undefined : {0}", style2.InsideField.Equals(Field.Undefined));
20
21GanttBarStyle style3 = view.CustomBarStyles[2];
22Console.WriteLine("Style3.LeftField is TaskPercentComplete : {0}", style3.LeftField.Equals(Field.TaskPercentComplete));
23Console.WriteLine("Style3.RightField is TaskPercentWorkComplete : {0}", style3.RightField.Equals(Field.TaskPercentWorkComplete));
24Console.WriteLine("Style3.TopField is Field.TaskActive : {0}", style3.TopField.Equals(Field.TaskActive));
25Console.WriteLine("Style3.BottomField is TaskActualCost : {0}", style3.BottomField.Equals(Field.TaskActualCost));
26Console.WriteLine("Style3.InsideField is Field.TaskActualDuration : {0}", style3.InsideField.Equals(Field.TaskActualDuration));
27
28Console.WriteLine("Style3.StartShape is HouseDown : {0}", style3.StartShape.Equals(GanttBarEndShape.HouseDown));
29Console.WriteLine("Style3.StartShapeType is Framed : {0}", style3.StartShapeType.Equals(GanttBarType.Framed));
30Console.WriteLine("Style3.StartShapeColor is Red : {0}", style3.StartShapeColor.Equals(Color.FromArgb(Color.Red.ToArgb())));
31
32Console.WriteLine("Style3.EndShape is CircleDiamond : {0}", style3.EndShape.Equals(GanttBarEndShape.CircleDiamond));
33Console.WriteLine("Style3.EndShapeType is Solid : {0}",  style3.EndShapeType.Equals(GanttBarType.Solid) );
34Console.WriteLine("Style3.EndShapeColor is Yellow : {0}",  style3.EndShapeColor.Equals(Color.FromArgb(Color.Yellow.ToArgb())));
35
36Console.WriteLine("Style3.MiddleShape is RectangleTop : {0}", style3.MiddleShape.Equals(GanttBarMiddleShape.RectangleTop));
37Console.WriteLine("Style3.MiddleFillPattern is SolidFill : {0}", style3.MiddleFillPattern.Equals(GanttBarFillPattern.SolidFill));
38Console.WriteLine("Style3.EndShapeColor is Red : {0}", style3.MiddleShapeColor.Equals(Color.FromArgb(Color.Red.ToArgb())));
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.