Travailler avec des vues

Lire les données de la vue spécifique

Les vues fournissent une représentation graphique d’un projet ou de certains de ses aspects (ressources, tâches, etc.). Par exemple, les graphiques Gantt fournissent une représentation graphique des progrès et des plans d’un projet:

Gantt Tableau dans le projet Microsoft

Microsoft Project permet à l’utilisateur de personnaliser les propriétés de différentes vues telles que les informations de style BAR, les propriétés de grille, les propriétés des lignes de progression, les informations d’échelle de temps, les propriétés de page, etc. Toutes ces propriétés sont stockées dans le fichier MPP. Aspose.Tasks pour .NET offre la capacité d’accéder à toutes les propriétés individuellement avec la classe View ou ses héritiers: GanttchartView, TaskUsageView ou ResourceUsageView. Cette fonctionnalité est prise en charge pour Microsoft Project 2003, 2007, 2010, 2013, 2016, 2019 et 2021 Formats de fichiers MPP.

Configure Gantt Chart View by Showing Selected Custom Fields

Extended attributes added to a project may be assigned to a task. It may be desirable to add this custom field to a saved MPP file’s default view, or you might want selected custom fields to be shown automatically rather than selecting them manually. This article describes how to achieve this by customizing the Table of a Project.

Customizing Timescale Tier Labels according to Current Culture Information

Aspose.Tasks for .NET API provides the capability to customize Timescale tier labels according to the current CultureInfo of the machine. The DateTimeConverter(DateTime date) delegate provides a converter method to convert date to a string in view timescale tiers. In addition, TimescaleTier.DateTimeConverter enables rendering of date in the desired format.

Customizing Timescale settings for a specific view and render a view using these settings

The TimeScaleTier class makes it possible to set the Timescale settings for a view of a project. The below example shows how to achieve this objective using the Aspose.Tasks for .NET API

 1Project project = new Project();
 2
 3// Retrieve project's Gantt chart view
 4GanttChartView view = (GanttChartView)project.Views.First(v => v.Name == "&Gantt Chart");
 5
 6// Set Time Scale count
 7view.BottomTimescaleTier.Count = 2;
 8view.BottomTimescaleTier.ShowTicks = false; 
 9view.BottomTimescaleTier.Unit = TimescaleUnit.Days;
10view.BottomTimescaleTier.Label = DateLabel.DayDdd;
11
12view.MiddleTimescaleTier.Count = 1;
13view.MiddleTimescaleTier.ShowTicks = false;
14view.MiddleTimescaleTier.Unit = TimescaleUnit.Weeks;
15view.MiddleTimescaleTier.Label = DateLabel.WeekDddDd;
16
17// Add some test data to project
18Task task1 = project.RootTask.Children.Add("Task 1");
19Task task2 = project.RootTask.Children.Add("Task 2");
20task1.Set(Tsk.Duration, task1.ParentProject.GetDuration(24, TimeUnitType.Hour));
21task2.Set(Tsk.Duration, task1.ParentProject.GetDuration(40, TimeUnitType.Hour));
22
23PdfSaveOptions saveOptions = new PdfSaveOptions();
24saveOptions.ViewSettings = view;
25
26// Timescale.DefinedInView options should be specified in order to render project using timescale settings 
27// defined in view (in view.BottomTimescaleTier and view.MiddleTimescaleTier properties in this example).
28// Otherwise predefined settings are used when one of Timescale.Days, Timescale.Months, Timescale.ThirdsOfMonths values is used.
29saveOptions.Timescale = Timescale.DefinedInView;
30project.Save("SetTimeScaleCount_out.pdf", saveOptions); 

Support for Text Styling

Text styling can be applied to a Gantt Chart View using TableTextStyle as shown in the following code sample.

Save MPP using the specific Project’s view

The following code example demonstrates the ability to save the MPP to graphical format using the specific view from project’s views. The feature can be useful when several views were defined for the same View screen in MPP project.

1Project project = new Project(Paths.TestdataPath + "TestViews.mpp");
2var view = project.Views.First(v => v.Name == "Customized Resource &Sheet");
3// We can set view's properties before use.
4view.PageInfo.PageSettings.IsPortrait = false;
5PdfSaveOptions saveOptions = new PdfSaveOptions();
6saveOptions.ViewSettings = view;
7project.Save("output.pdf", saveOptions);

Working with bar styles of Gantt chart view

The following code example demonstrates how to modify task’s bar styles for Gantt chart view.

 1Project project = new Project(Paths.TestdataPath + "TestGanttChartView.mpp");
 2var ganttChartView = (GanttChartView)project.Views.First(v => v.Name == "Gantt &Chart");
 3PdfSaveOptions saveOptions = new PdfSaveOptions();
 4saveOptions.Timescale = Timescale.DefinedInView;
 5saveOptions.ViewSettings = ganttChartView;
 6
 7// Bar styles can be either task-specific (located in GanttChartView.CustomBarStyles)
 8// of category-specific (located in GanttChartView.BarStyles)
 9
10foreach (GanttBarStyle ganttBarStyle in ganttChartView.CustomBarStyles)
11{
12    if (ganttBarStyle.ShowForTaskUid != 11)
13    {
14        continue;
15    }
16
17    // For demonstration purposes we are modifying style for Task with Unique ID = 11
18 
19    ganttBarStyle.LeftField = Field.TaskName;
20    // Here we set custom converter to control which text should be rendered inside the task bar.
21    ganttBarStyle.InsideBarTextConverter = task => "Hours rem.: " + (int)task.Get(Tsk.RemainingWork).TimeSpan.TotalHours;
22}
23
24foreach (GanttBarStyle ganttBarStyle in ganttChartView.BarStyles)
25{
26    if (!ganttBarStyle.ShowForCategories.Contains(GanttBarShowFor.Milestone))
27    {
28        continue;
29    }
30
31    // For demonstration purposes we are modifying styles applicable to milestone tasks.
32
33    ganttBarStyle.RightField = Field.TaskActualFinish;
34    // Here we can customize text drawn at the top of task's bar using user provided delegate.
35    ganttBarStyle.TopBarTextConverter = task => task.Get(Tsk.ActualStart).Day.ToString();
36}
37
38project.Save("output.pdf", saveOptions);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.