Travailler avec des styles de bar personnalisés
Aperçu
Dans Microsoft Project, la vue du graphique Gantt permet aux utilisateurs de définir les styles de barres personnalisés pour améliorer la visualisation ou mettre l’accent sur des types de tâches spécifiques. Cela inclut la modification de l’apparence des barres (Démarrer, milieu, fin), ajoutant Couleurs personnalisées, ajustant Formes et configurer Étiquettes de texte autour des barres de tâche.
Aspose.tasks pour .NET prend en charge ce niveau de personnalisation via la classe GantTbarstyle
. Avec lui, les développeurs peuvent contrôler par programme comment chaque tâche (ou groupe de tâches) est affichée dans l’image du graphique Gantt rendu, ce qui rend les rapports et l’analyse visuelle plus significatifs.
Définir un style de bar personnalisé
Pour définir un nouveau style pour une tâche ou un ensemble de tâches spécifiques, vous devez:
- Créez une nouvelle instance de «Ganttbarstyle».
- Définissez des propriétés comme
ShowFortaskuid
,« Middleshape »,« StartShape »,« Endshape »,« FillColor »et autres. - Ajoutez le style à la collection `imagesVeOptions.barstyles ‘avant le rendu.
Tip: The
ShowForTaskUid
property specifies which task UID this style applies to. You can also apply a style globally by omitting it.
Exemple: appliquez un style personnalisé à une tâche spécifique
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 Existing Custom Bar Styles
Aspose.Tasks also allows reading the GanttBarStyle
objects from existing MPP files. This can be useful if you need to analyze or replicate the styling applied in Microsoft Project.
Example: Read All Custom Styles from a Project
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())));
Common Use Cases
Custom bar styles are commonly used to:
- Visually distinguish milestones, critical tasks, or summary tasks.
- Apply color-coding for different teams or departments.
- Label tasks dynamically based on custom fields (
Text1
,ResourceNames
, etc.). - Highlight delayed tasks with unique colors or shapes.
Summary
Aspose.Tasks for .NET provides full control over how tasks are visualized in Gantt charts via the GanttBarStyle
class. Whether you’re creating a new report or replicating Microsoft Project output, the API allows for precise control of shapes, colors, text labels, and styling logic.