Trabajando con vistas

Lectura de datos de vista específica

Vistas proporciona una representación gráfica de un proyecto o algunos de sus aspectos (recursos, tareas, etc.). Por ejemplo, los gráficos de Gantt proporcionan una representación gráfica del progreso y los planes de un proyecto:

Gantt Chart en Microsoft Project

Microsoft Project permite al usuario personalizar las propiedades de diferentes Vistas, como la información al estilo de la barra, las propiedades de la línea de cuadrícula, las propiedades de las líneas de progreso, la información de escala de tiempo, las propiedades de la página, etc. Todas estas propiedades se almacenan en el archivo MPP. Aspose.Tasks para .NET proporciona la capacidad de acceder a todas las propiedades individualmente con la clase View o sus herederos: GantTChartView, TaskUsageView o ResourceusEview clases. Esta característica es compatible con los formatos de archivo MPP de Microsoft Project 2003, 2007, 2010, 2013, 2016, 2019 y 2021.

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.