Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:01:59+00:00 2026-06-07T01:01:59+00:00

I need your help with an algorithm (it will be developed on client side

  • 0

I need your help with an algorithm (it will be developed on client side with javascript, but doesn’t really matter, I’m mostly interested in the algorithm itself) laying out calendar events so that each event box has maximum width. Please see the following picture:

calendar events layout

Y axis is time. So if "Test event" starts at noon (for example) and nothing more intersects with it, it takes the whole 100% width. "Weekly review" intersects with "Tumbling YMCA" and "Anna/Amelia", but the latter two are not intersecting, so they all fill up 50%. Test3, Test4 and Test5 are all intersecting, so max width is 33.3% for each. But Test7 is 66% since Test3 is 33% fixed (see above) , so it takes all available space , which is 66%.

I need an algorithm how to lay this out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:02:00+00:00Added an answer on June 7, 2026 at 1:02 am
    1. Think of an unlimited grid with just a left edge.
    2. Each event is one cell wide, and the height and vertical position is fixed based on starting and ending times.
    3. Try to place each event in a column as far left as possible, without it intersecting any earlier event in that column.
    4. Then, when each connected group of events is placed, their actual widths will be 1/n of the maximum number of columns used by the group.
    5. You could also expand the events at the far left and right to use up any remaining space.
    /// Pick the left and right positions of each event, such that there are no overlap.
    /// Step 3 in the algorithm.
    void LayoutEvents(IEnumerable<Event> events)
    {
        var columns = new List<List<Event>>();
        DateTime? lastEventEnding = null;
        foreach (var ev in events.OrderBy(ev => ev.Start).ThenBy(ev => ev.End))
        {
            if (ev.Start >= lastEventEnding)
            {
                PackEvents(columns);
                columns.Clear();
                lastEventEnding = null;
            }
            bool placed = false;
            foreach (var col in columns)
            {
                if (!col.Last().CollidesWith(ev))
                {
                    col.Add(ev);
                    placed = true;
                    break;
                }
            }
            if (!placed)
            {
                columns.Add(new List<Event> { ev });
            }
            if (lastEventEnding == null || ev.End > lastEventEnding.Value)
            {
                lastEventEnding = ev.End;
            }
        }
        if (columns.Count > 0)
        {
            PackEvents(columns);
        }
    }
    
    /// Set the left and right positions for each event in the connected group.
    /// Step 4 in the algorithm.
    void PackEvents(List<List<Event>> columns)
    {
        float numColumns = columns.Count;
        int iColumn = 0;
        foreach (var col in columns)
        {
            foreach (var ev in col)
            {
                int colSpan = ExpandEvent(ev, iColumn, columns);
                ev.Left = iColumn / numColumns;
                ev.Right = (iColumn + colSpan) / numColumns;
            }
            iColumn++;
        }
    }
    
    /// Checks how many columns the event can expand into, without colliding with
    /// other events.
    /// Step 5 in the algorithm.
    int ExpandEvent(Event ev, int iColumn, List<List<Event>> columns)
    {
        int colSpan = 1;
        foreach (var col in columns.Skip(iColumn + 1))
        {
            foreach (var ev1 in col)
            {
                if (ev1.CollidesWith(ev))
                {
                    return colSpan;
                }
            }
            colSpan++;
        }
        return colSpan;
    }
    

    Edit: Now sorts the events, instead of assuming they is sorted.

    Edit2: Now expands the events to the right, if there are enough space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need your help. I tried myself but cant solve it. I have a table
Need your help to get the max of CAP_PRICE based on certain criteria in
I need your help in JAVA (with some sample code if possible) regarding to
I need your help. I tried to play an audio file stored in Assets
I need your help with display of some comma separated enteries from my database.
I need your help with my script. What I'm trying t do is insert
I need your help with some sample code for a situation I could not
I need your help. Say I have a code inside a for statement similar
I need your help again :) I'm trying to do a plugin with jQuery
I need your help. I'm trying to install php_apc.dll into my php directory. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.