Working with Project Views
Reading Specific Gantt Chart View Data
Gantt charts provide a graphical representation of project plans. Sometimes it is necessary to read particular data associated with a Gantt chart view such as the bar-style information, gridline properties, progress lines properties and time scale information. Aspose.Tasks for Java API provides the capability to access all the properties individually using the GanttChartView class that inherits from View. This feature is supported for Microsoft Project 2003, 2007, 2010, 2013, 2016 and 2019 MPP file formats.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(ReadSpecificGanttChartData.class);
4Project project = new Project(dataDir + "Project2003.mpp");
5GanttChartView view = (GanttChartView) project.getViews().toList().get(1);
6System.out.println("View.BarRounding: " + view.getBarRounding());
7System.out.println("view.ShowBarSplits: " + view.getShowBarSplits());
8System.out.println("view.ShowDrawings: " + view.getShowDrawings());
9System.out.println("view.RollUpGanttBars: " + view.getRollUpGanttBars());
10System.out.println("view.HideRollupBarsWhenSummaryExpanded: " + view.getHideRollupBarsWhenSummaryExpanded());
11System.out.println("view.BarSize: " + view.getBarSize());
12System.out.println("view.BarStyles count: " + view.getBarStyles().size());
13
14int i = 0;
15for (GanttBarStyle barStyle : view.getBarStyles()) {
16 System.out.println(barStylesNames[i] + ": " + barStyle.getName());
17 System.out.println(barStylesShowFor[i] + ":" + barStyle.getShowFor());
18 System.out.println(barStylesFrom[i] + ":" + barStyle.getFrom());
19 System.out.println(barStylesTo[i] + ":" + barStyle.getTo());
20 System.out.println(barStylesMiddleShape[i] + ":" + barStyle.getMiddleShape());
21 System.out.println(barStylesMiddleColor[i] + ":" + barStyle.getMiddleShapeColor());
22 System.out.println(barStylesStartShape[i] + ":" + barStyle.getStartShape());
23 System.out.println(barStylesEndShape[i] + ":" + barStyle.getEndShape());
24 System.out.println(barStylesEndShapeColor[i] + ":" + barStyle.getEndShapeColor());
25 i++;
26}
27
28System.out.println("Gridlines count: " + view.getGridlines().size());
29Gridlines gridlines = view.getGridlines().get(0);
30
31System.out.println("Gridlines.Type: " + gridlines.getType());
32System.out.println("Gridlines interval: " + gridlines.getInterval());
33System.out.println("Gridlines.NormalColor: " + gridlines.getNormalColor());
34System.out.println("Gridlines.NormalPattern: " + gridlines.getNormalPattern());
35System.out.println("Gridlines.IntervalPattern: " + gridlines.getIntervalPattern());
36System.out.println("Gridlines.IntervalColor: " + gridlines.getIntervalColor());
37
38System.out.println("View Text Styles: " + view.getTextStyles());
39
40System.out.println("ProgressLInes.BeginAtDate: " + view.getProgressLines().getBeginAtDate().toString());
41System.out.println("ProgressLines.isBaselinePlan: " + view.getProgressLines().isBaselinePlan());
42System.out.println("ProgressLines.DisplaySelected: " + view.getProgressLines().getDisplaySelected());
43System.out.println("ProgressLines.SelectedDates.Count: " + view.getProgressLines().getSelectedDates().size());
44
45System.out.println("ProgressLines.DisplayAtRecurringIntervals: "
46 + view.getProgressLines().getDisplayAtRecurringIntervals());
47System.out.println("ProgressLines.RecurringInterval.Interval: "
48 + view.getProgressLines().getRecurringInterval().getInterval());
49System.out.println("ProgressLines.RecurringInterval.WeeklyDays.Count"
50 + view.getProgressLines().getRecurringInterval().getWeeklyDays().size());
51System.out.println("RecurringInterval.DayType.Friday: "
52 + (int) view.getProgressLines().getRecurringInterval().getWeeklyDays().get(1));
53System.out.println("RecurringInterval.DayType.Saturday: "
54 + (int) view.getProgressLines().getRecurringInterval().getWeeklyDays().get(2));
55System.out.println("RecurringInterval.DayType.Sunday: "
56 + (int) view.getProgressLines().getRecurringInterval().getWeeklyDays().get(0));
57
58System.out.println("ProgressLines.ShowDate" + view.getProgressLines().getShowDate());
59
60System.out.println("ProgressLines.ProgressPointShape: " + view.getProgressLines().getProgressPointShape());
61System.out.println("ProgressLines.ProgressPointColor: " + view.getProgressLines().getProgressPointColor());
62System.out.println("ProgressLines.LineColor: " + view.getProgressLines().getLineColor());
63System.out.println("ProgressLines.LinePattern" + view.getProgressLines().getLinePattern());
64
65System.out.println(
66 "ProgressLines.OtherProgressPointShape: " + view.getProgressLines().getOtherProgressPointShape());
67System.out.println("ProgressLines.OtherProgressPointColor: "
68 + view.getProgressLines().getOtherProgressPointColor().toString());
69System.out.println("ProgressLines.OtherLineColor: " + view.getProgressLines().getOtherLineColor());
70
71System.out.println("BottomTimescaleTier" + view.getBottomTimescaleTier());
72
73System.out.println("BottomTimescaleTier.Count: " + view.getBottomTimescaleTier().getCount());
74System.out.println("BottomTimescaleTier.Unit: " + view.getBottomTimescaleTier().getUnit());
75System.out.println("BottomTimescaleTier.UsesFiscalYear: " + view.getBottomTimescaleTier().getUsesFiscalYear());
76System.out.println("BottomTimescaleTier.Alignment: " + view.getBottomTimescaleTier().getAlignment());
77System.out.println("BottomTimescaleTier.ShowTicks: " + view.getBottomTimescaleTier().getShowTicks());
78System.out.println("BottomTimescaleTier.Label: " + view.getBottomTimescaleTier().getLabel());
79
80System.out.println("MiddleTimescaleTier.Count: " + view.getMiddleTimescaleTier().getCount());
81System.out.println("TimescaleUnit.Weeks: " + view.getMiddleTimescaleTier().getUnit());
82System.out.println("MiddleTimescaleTier.Alignment: " + view.getMiddleTimescaleTier().getAlignment());
83System.out.println("MiddleTimescaleTier.ShowTicks: " + view.getMiddleTimescaleTier().getShowTicks());
84System.out.println("MiddleTimescaleTier.Label: " + view.getMiddleTimescaleTier().getLabel());
85
86System.out.println("TopTimescaleTier.Unit: " + view.getTopTimescaleTier().getUnit());
87System.out.println("TopTimescaleTier.UsesFiscalYear: " + view.getTopTimescaleTier().getUsesFiscalYear());
88System.out.println("TopTimescaleTier.Alignment: " + view.getTopTimescaleTier().getAlignment());
89System.out.println("TopTimescaleTier.ShowTicks: " + view.getTopTimescaleTier().getShowTicks());
90System.out.println("TopTimescaleTier.Label" + view.getTopTimescaleTier().getLabel());
Configure Gantt Chart View by Showing Selected Custom Fields
Extended attributes added to a project may be assigned to a task and it is often desirable to add custom fields to the default view of an MPP file, or you might want selected custom fields to be shown automatically rather than selected manually. This article describes how to achieve this by customizing a Project’s table.
The code below shows how to configure a Gantt chart view with custom fields.
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(ConfigureGanttChartView.class);
3
4Project project = new Project(dataDir + "New project 2010.mpp");
5
6// Create a new project task
7Task task = project.getRootTask().getChildren().add("New Activity");
8
9// Define new custom attribute
10ExtendedAttributeDefinition text1Definition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Text1, null);
11project.getExtendedAttributes().add(text1Definition);
12// Add custom text attribute to created task.
13task.getExtendedAttributes().add(text1Definition.createExtendedAttribute("Activity attribute"));
14
15// Customize table by adding text attribute field
16TableField attrField = new TableField();
17attrField.setField(Field.TaskText1);
18attrField.setWidth(20);
19attrField.setTitle("Custom attribute");
20attrField.setAlignTitle(StringAlignment.Center);
21attrField.setAlignData(StringAlignment.Center);
22
23Table table = project.getTables().toList().get(0);
24table.getTableFields().add(3, attrField);
25
26// The result of opening of saved project in MSP2010 is in attached screenshot
27project.save("saved.mpp", SaveFileFormat.MPP);
Customize Timescale Tier Labels according to Local Culture
Aspose.Tasks for Java API can be used to customer timescale tier labels according to local culture information.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(DefaultProjectProperties.class);
4Project project = new Project(dataDir + "New Project 2007.mpp");
5
6project.getTaskLinks().add(project.getRootTask().getChildren()
7 .add("Task 1"), project.getRootTask().getChildren()
8 .add("Task 2"));
9
10GanttChartView view = (GanttChartView)project.getDefaultView();
11view.getMiddleTimescaleTier().setUnit(TimescaleUnit.Months);
12project.set(Prj.TIMESCALE_START, new Date(112, 7, 6));
13
14view.getMiddleTimescaleTier().setDateTimeConverter( new DateTimeConverter() {
15 public String invoke(java.util.Date date)
16 {
17 Calendar cal = Calendar.getInstance();
18 cal.setTime(date);
19 int month = cal.get(Calendar.MONTH);
20 return new String[] { "Jan.", "Feb.", "Mar.", "Apr.","May.", "Jun.", "July.","Aug.", "Sep.", "Oct.", "Nov.", "Dec." }[month];
21 }
22 });
23
24project.save("saved.pdf", SaveFileFormat.PDF);
Setting TimeScale Count for Project
The TimeScaleTier class makes it possible to set the time scale count information for a Project. This topic shows how to achieve this objective using Aspose.Tasks for Java API.
The code below shows how to set the time scale count.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(SetTimeScaleCount.class);
4
5Project project = new Project();
6GanttChartView view = new GanttChartView();
7
8view.getBottomTimescaleTier().setCount(2);
9view.getBottomTimescaleTier().setShowTicks(false);
10
11view.getMiddleTimescaleTier().setCount(2);
12view.getMiddleTimescaleTier().setShowTicks(false);
13
14project.getViews().add(view);
15
16// Add some test data to project
17Task task1 = project.getRootTask().getChildren().add("Task 1");
18Task task2 = project.getRootTask().getChildren().add("Task 2");
19task1.set(Tsk.DURATION, task1.getParentProject().getDuration(24, TimeUnitType.Hour));
20task2.set(Tsk.DURATION, task1.getParentProject().getDuration(40, TimeUnitType.Hour));
21
22project.save(dataDir + "temp.pdf", SaveFileFormat.PDF); // Result is in
23// attached
24// screenshot