From 293eb767ef6f28290121bcf3a355676954537b7c Mon Sep 17 00:00:00 2001 From: Nadezhda Tacheva Date: Fri, 17 May 2024 16:46:06 +0300 Subject: [PATCH] kb(gantt): different colors for tasks --- .../gantt-different-colors-for-tasks.md | 230 ++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 knowledge-base/gantt-different-colors-for-tasks.md diff --git a/knowledge-base/gantt-different-colors-for-tasks.md b/knowledge-base/gantt-different-colors-for-tasks.md new file mode 100644 index 0000000000..55b8259d42 --- /dev/null +++ b/knowledge-base/gantt-different-colors-for-tasks.md @@ -0,0 +1,230 @@ +--- +title: Set Different Colors for the Gantt Tasks +description: How to set different colors for the Gantt tasks? +type: how-to +page_title: Set Different Colors for the Gantt Tasks +slug: gantt-kb-different-colors-for-tasks +position: +tags: +ticketid: 1536209 +res_type: kb +--- + +## Environment + + + + + + + +
ProductGantt for Blazor
+ + +## Description + +I want to set different colors for the Gantt tasks. I store the colors in my data source. How to apply the colors from my data source to the rendered Gantt tasks? + + +## Solution + +Follow these steps to apply the desired colors to the Gantt tasks: + +1. Use a [Task Template]({%slug gantt-task-template%}) to override the default task rendering. +1. In the template, declare a wrapping element that will contain all the task details. Specify the desired task information inside. +1. Add an attribute `style="background-color"` to the wrapper. Set the color from the template `context` or any other desired color. +1. Use custom CSS remove the default padding of the task template element (`
`) and adjust the custom wrapper, so it covers the whole task element. This will ensure that the background color will cover the whole task. + +>caption Set Different Colors for the Gantt Tasks + +````CSHTML + + + @{ + CurrTask = context; + } +
+
@CurrTask.Title
+
Assignee: @CurrTask.Assignee
+
+
+
+ + + + + + + + + + + +
+ +@code { + private List Data { get; set; } + + private FlatModel CurrTask { get; set; } = new FlatModel(); + + private int LastId { get; set; } = 1; + + private List Colors = new List() + { + "lightgreen", + "orange", + "lightblue", + "cyan", + "lightcoral", + "lightpink" + }; + + protected override void OnInitialized() + { + Data = new List(); + var random = new Random(); + + for (int i = 1; i < 6; i++) + { + var newItem = new FlatModel() + { + Id = LastId, + Title = "Task " + i.ToString(), + Start = new DateTime(2021, 7, 5 + i), + End = new DateTime(2021, 7, 11 + i), + PercentComplete = Math.Round(random.NextDouble(), 2), + Color = Colors[i] + }; + + Data.Add(newItem); + var parentId = LastId; + LastId++; + + for (int j = 0; j < 5; j++) + { + Data.Add(new FlatModel() + { + Id = LastId, + ParentId = parentId, + Title = " Task " + i + " : " + j.ToString(), + Start = new DateTime(2021, 7, 5 + j), + End = new DateTime(2021, 7, 6 + i + j), + PercentComplete = Math.Round(random.NextDouble(), 2), + Assignee = " Employee " + j, + Color = Colors[j] + }); + + LastId++; + } + } + + base.OnInitialized(); + } + + private void UpdateItem(GanttUpdateEventArgs args) + { + var item = args.Item as FlatModel; + + var foundItem = Data.FirstOrDefault(i => i.Id.Equals(item.Id)); + + if (foundItem != null) + { + foundItem.Title = item.Title; + foundItem.Start = item.Start; + foundItem.End = item.End; + foundItem.PercentComplete = item.PercentComplete; + } + } + + private void DeleteItem(GanttDeleteEventArgs args) + { + var item = Data.FirstOrDefault(i => i.Id.Equals((args.Item as FlatModel).Id)); + + RemoveChildRecursive(item); + } + + private void RemoveChildRecursive(FlatModel item) + { + var children = Data.Where(i => item.Id.Equals(i.ParentId)).ToList(); + + foreach (var child in children) + { + RemoveChildRecursive(child); + } + + Data.Remove(item); + } + + class FlatModel + { + public int Id { get; set; } + public int? ParentId { get; set; } + public string Title { get; set; } + public double PercentComplete { get; set; } + public DateTime Start { get; set; } + public DateTime End { get; set; } + public string Assignee { get; set; } + public string Color { get; set; } + } +} + + +```` \ No newline at end of file