Task color

How to give color to individual task ,or
How to give color to all tasks according to their layer.

Like Root level tasks got some x color,
then the sub tasks of this task will get y color,
then sub tasks of this previous task will get z color.

Like that. Is it possible to do so ??

@AbirN,
what should be a resulting file ? MPP or another format?

For example, if you want to color tasks in project’s view saved to PDF file, you could add TableTextStyle for each task:


Project project = new Project(@"sample");

GanttChartView view = project.Views.GetByName("&Gantt Chart") as GanttChartView;

if (view == null)
{
    view = new GanttChartView();
}

foreach (var t in project.EnumerateAllChildTasks())
{
    // Implement your color choosing logic here and set TableTextStyle.BackgroundColor accordingly.
    if (t.OutlineLevel == 1)
    {
        view.TableTextStyles.Add(new TableTextStyle(t.Uid)
        {
            BackgroundColor = Color.Green,
            Field = Field.Undefined,
            BackgroundPattern = BackgroundPattern.SolidFill
        });
    }
    else if (t.OutlineLevel == 2)
    {
        view.TableTextStyles.Add(new TableTextStyle(t.Uid)
        {
            BackgroundColor = Color.LightBlue,
            Field = Field.Undefined,
            BackgroundPattern = BackgroundPattern.SolidFill
        });
    }
                
    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.ViewSettings = view;
    project.Save("output.pdf", saveOptions);

Thank you for replying,
PDF & MPP both are resulting file. I have to show result in the both format.
Can i have the solution for ASP.NET (C#) ?

@AbirN,
The example above is for PDF.

In order to save to MPP the following statements should be used:

project.Save("output.mpp", new MPPSaveOptions()
{
    WriteViewData = true
});

You can insert the code from the examples above to your ASP.NET application.

for using this code (TableTextStyles) I got my colors in the table field.
But I want them in Gantt Bars at the orange marked section.
image.png (41.2 KB)

So I tried the below two codes:

  1.                     view.BarStyles.Add(new GanttBarStyle()
                     {
                         StartShapeColor = Color.Yellow
                     });
    
  2.            view.CustomBarStyles.Add(new GanttBarStyle() { StartShapeColor = Color.Yellow, MiddleShapeColor = Color.Aqua });
    

inside

this conditions ,

But for the 1st one I got
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Aspose.Tasks

And for the Second one I got no error also didn’t got any expected output or changes.

Please try the following snippet:


view.CustomBarStyles.Add(new GanttBarStyle()
{
    ShowForTaskUid = t.Uid,
    From = Field.TaskStart,
    To = Field.TaskFinish,
    StartShapeColor = Color.Yellow, 
    StartShape = GanttBarEndShape.ArrowDown,
    MiddleShapeColor = Color.Aqua,
    MiddleShape = GanttBarMiddleShape.LineMiddle,
    Row = 1,
    StartShapeType = GanttBarType.Solid,
    MiddleFillPattern = GanttBarFillPattern.SolidFill,
    EndShapeType = GanttBarType.Solid,
    EndShape = GanttBarEndShape.Circle,
});