We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Coloring events based on event data

Thread ID:

Created:

Updated:

Platform:

Replies:

146543 Aug 7,2019 04:05 PM UTC Feb 12,2020 09:16 AM UTC Blazor 6
loading
Tags: Scheduler
Uwe Hein
Asked On August 7, 2019 04:05 PM UTC

Hi everybody,

  how can I color events based on data like RecurrenceRule, Location or Subject or even a colorstring coming from the database ? E.g. all recurring events in green.

      regards

         Uwe Hein

Nevitha Ravi [Syncfusion]
Replied On August 8, 2019 10:05 AM UTC

Hi Uwe, 
  
Greetings from Syncfusion Support. 
  
We have prepared a sample to set colour for events from a colour string provided in a field of the recurring events using EventRendered event for your reference which can be downloaded from the following link. 
  
<EjsSchedule TValue="AppointmentData" Height="650px" SelectedDate="new DateTime(2019, 1, 31)"> 
    <ScheduleEvents TValue="AppointmentData" EventRendered="OnEventRendered"></ScheduleEvents> 
    <ScheduleEventSettings DataSource="@DataSource"></ScheduleEventSettings> 
</EjsSchedule> 
@code{ 
    public string[] CustomClass = { "custom-class" }; 
    public string BackgroundColor; 
    public void OnEventRendered(EventRenderedArgs<AppointmentData> args) 
    { 
        if (args.Data.RecurrenceRule != null) 
        { 
            args.Element.AddClass(CustomClass); 
            bgcolor = args.Data.CategoryColor; 
        } 
    } 
    List<AppointmentData> DataSource = new List<AppointmentData> 
    { 
        new AppointmentData { Id = 1, Subject = "Sprint Meeting", StartTime = new DateTime(2019, 2, 1, 9, 30, 0) , EndTime = new DateTime(2019, 2, 1, 11, 0, 0) }, 
        new AppointmentData { Id = 2, Subject = "Scrum Meeting", StartTime = new DateTime(2019, 1, 27, 9, 30, 0) , EndTime = new DateTime(2019, 1, 27, 11, 0, 0), 
        RecurrenceRule = "FREQ=DAILY;INTERVAL=1;COUNT=5", CategoryColor= "#1aaa55" } 
    }; 
  
    public class AppointmentData 
    { 
        public int Id { get; set; } 
        public string Subject { get; set; } 
        public string Location { get; set; } 
        public DateTime StartTime { get; set; } 
        public DateTime EndTime { get; set; } 
        public string Description { get; set; } 
        public bool IsAllDay { get; set; } 
        public string RecurrenceRule { get; set; } 
        public string RecurrenceException { get; set; } 
        public Nullable<int> RecurrenceID { get; set; } 
        public string CategoryColor { get; set; } 
    } 
} 
<style> 
    .e-schedule .e-vertical-view .e-all-day-appointment-wrapper .e-appointment.custom-class, 
    .e-schedule .e-vertical-view .e-day-wrapper .e-appointment.custom-class, 
    .e-schedule .e-month-view .e-appointment.custom-class { 
        background: @BackgroundColor; 
    } 
</style> 
  
Please try out the sample and let us know if you need any further assistance. 
  
Regards, 
Nevitha 
 


Uwe Hein
Replied On August 8, 2019 12:35 PM UTC

Hi Nevitha, 
  thank you very much for your fast answer - it works as expected. I had to change the style section to :

    .e-schedule .e-vertical-view .e-all-day-appointment-wrapper .e-timeline-view .e-appointment.custom-class,.e-schedule .e-vertical-view .e-day-wrapper .e-appointment.custom-class,.e-schedule .e-month-view .e-appointment.custom-class {    background: @bgcolor;

as I am working with a timeline. Are these the correct classes ?
Is there a way to display more than 2 colors by putting one or more else branches in the OnEventRendered method like:

if (args.Data.RecurrenceRule != null)
{
     args.Element.AddClass(CustomClass);
     bgcolor = args.Data.CategoryColor;
}
else if (args.Data.Subject == "Sprint Meeting")
{
     bgcolor = args.Data.CategoryColor; // color provided for Sprint Meeting
}
else
{
     bgcolor = args.Data.CategoryColor; // color provided for this special event
}
and so on .....
I tried to figure it out but was not successful. Thank you again for your fast and competent support.
regards
  Uwe

Nevitha Ravi [Syncfusion]
Replied On August 9, 2019 05:44 AM UTC

Hi Uwe, 

Thanks for your update. 
 
No, the classes you have mentioned are wrong for timeline views. Please refer the following code for timeline views and timeline month view. 
 
<style> 
    .e-schedule .e-timeline-view .e-appointment.custom-class, 
    .e-schedule .e-timeline-month-view .e-appointment.custom-class { 
        background: @BgColor; 
    } 
    .e-schedule .e-timeline-view .e-appointment.custom-class1, 
    .e-schedule .e-timeline-month-view .e-appointment.custom-class1 { 
        background: @BgColor1; 
    } 
    .e-schedule .e-timeline-view .e-appointment.custom-class2, 
    .e-schedule .e-timeline-month-view .e-appointment.custom-class2 { 
        background: @BgColor2; 
    } 
</style> 
 
 
In your shared code, you haven’t add the custom class for elements of ‘sprint meeting and special event’, we have modified our previous sample with timeline view and added different colours for each events which can be downloadable from the following link. 
 
   public string[] CustomClass = { "custom-class" }; 
    public string[] CustomClass1 = { "custom-class1" }; 
    public string[] CustomClass2 = { "custom-class2" }; 
    public string BgColor, BgColor1, BgColor2; 
    public void OnEventRendered(EventRenderedArgs<AppointmentData> args) 
    { 
        if (args.Data.RecurrenceRule != null) 
        { 
            args.Element.AddClass(CustomClass); 
            BgColor = args.Data.CategoryColor; 
        } 
        else if (args.Data.Subject == "Sprint Meeting") 
        { 
            args.Element.AddClass(CustomClass1); 
            BgColor1 = args.Data.CategoryColor; // color provided for Sprint Meeting 
        } 
        else 
        { 
            args.Element.AddClass(CustomClass2); 
            BgColor2 = args.Data.CategoryColor; // color provided for this special event 
        } 
    } 
    List<AppointmentData> DataSource = new List<AppointmentData> 
    { 
        new AppointmentData { Id = 3, Subject = "Special event", StartTime = new DateTime(2019, 1, 17, 9, 30, 0) , EndTime = new DateTime(2019, 1, 17, 11, 0, 0), CategoryColor= "#df5286" }, 
        new AppointmentData { Id = 1, Subject = "Sprint Meeting", StartTime = new DateTime(2019, 1, 15, 9, 30, 0) , EndTime = new DateTime(2019, 1, 15, 11, 0, 0), CategoryColor= "#7fa900" }, 
        new AppointmentData { Id = 2, Subject = "Scrum Meeting", StartTime = new DateTime(2019, 1, 27, 9, 30, 0) , EndTime = new DateTime(2019, 1, 27, 11, 0, 0), 
        RecurrenceRule = "FREQ=DAILY;INTERVAL=1;COUNT=5", CategoryColor= "#1aaa55" } 
    }; 
 
Please try out the sample and let us know if you need any further assistance. 

Regards, 
Nevitha 


Uwe Hein
Replied On August 9, 2019 10:58 AM UTC

Hi Nevitha,
   
  thank you very much - works like a charm.

        regards

            Uwe
 

Alex
Replied On February 8, 2020 01:30 AM UTC

Hello, I basically want to do the same as Uwe, but with a dynamic number of colors.
I have a series calendar, showing each episode. I want to color each episode according to what series it's from.

My problem is that I can't hardcode the styles as I do not know how many different colors will be needed for a specific month (I'm using the month view).

Is there a way to do this dynamically? 


Here is a picture from my website:

Alagumeena Kalaiselvan [Syncfusion]
Replied On February 12, 2020 09:16 AM UTC

Hi Uwe, 

Thanks for your update! 

Yes, you can add the color to the appointment dynamically by simply customizing the style attribute of the appointment like below code 

@using Syncfusion.EJ2.Blazor.Schedule 
 
<EjsSchedule TValue="AppointmentData" Height="650px" SelectedDate="new DateTime(2019, 1, 17)"> 
    <ScheduleEvents TValue="AppointmentData" EventRendered="OnEventRendered"></ScheduleEvents> 
    <ScheduleViews> 
        <ScheduleView Option="View.Day"></ScheduleView> 
        <ScheduleView Option="View.Week"></ScheduleView> 
        <ScheduleView Option="View.Month"></ScheduleView> 
        <ScheduleView Option="View.WorkWeek"></ScheduleView> 
    </ScheduleViews> 
    <ScheduleEventSettings DataSource="@DataSource"></ScheduleEventSettings> 
</EjsSchedule> 
 
@code{ 
    public async Task OnEventRendered(EventRenderedArgs<AppointmentData> args) 
    { 
        string style = (await args.Element.GetAttribute("style")).ToString();  // Get style attribute of the element 
        style += "background:" + args.Data.CategoryColor;               // concatenate the background color 
        args.Element.SetAttribute("style", style);                      // Reset the values of style attribute 
    } 
    List<AppointmentData> DataSource = new List<AppointmentData> 
{ 
        new AppointmentData { Id = 3, Subject = "South Park", StartTime = new DateTime(2019, 1, 17, 9, 30, 0) , EndTime = new DateTime(2019, 1, 17, 11, 0, 0), CategoryColor= "#df5286" }, 
        new AppointmentData { Id = 1, Subject = "Vikings", StartTime = new DateTime(2019, 1, 15, 9, 30, 0) , EndTime = new DateTime(2019, 1, 15, 11, 0, 0), CategoryColor= "#7fa900" }, 
        new AppointmentData { Id = 2, Subject = "Rick and Morty", StartTime = new DateTime(2019, 1, 27, 9, 30, 0) , EndTime = new DateTime(2019, 1, 27, 11, 0, 0), 
        RecurrenceRule = "FREQ=DAILY;INTERVAL=1;COUNT=5", CategoryColor= "#1aaa55" } 
    }; 
 
    public class AppointmentData 
    { 
        public int Id { get; set; } 
        public string Subject { get; set; } 
        public string Location { get; set; } 
        public DateTime StartTime { get; set; } 
        public DateTime EndTime { get; set; } 
        public string Description { get; set; } 
        public bool IsAllDay { get; set; } 
        public string RecurrenceRule { get; set; } 
        public string RecurrenceException { get; set; } 
        public Nullable<int> RecurrenceID { get; set; } 
        public string CategoryColor { get; set; } 
    } 
} 

Kindly try out with the above sample and get back to us, If you need further assistance. 

Regards 
Alagumeena.K  
  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon