Resource Group Aggregate Summaries

When scheduling across multiple groups of resources it sometimes is helpful to provide summaries and aggregate calculations on a group level. These dynamically calculated values can be presented as custom templated events in resource parent rows. Whenever new bookings are made, updated or deleted, use the onEventCreated, onEventUpdated and onEventDeleted lifecycle events to update the aggregates for their parent resource. As we are working with variable event heights, make sure to set eventHeight: 'variable' which can be configured under the view. This is necessary if the actual booked events have a different height than the summary events (for parent resources).

Variable event height is an experimental feature.  Learn more about it in the documentation →
Get started with this example

Demo

Loading the live demo

Preview in full screen

Show aggregate totals in parent resource rows on a Timeline — summary values recalculate as bookings are added, changed, or removed.

Code

import { Eventcalendar, formatDate, setOptions  } from '@mobiscroll/react';
import { useCallback, useMemo, useRef, useState } from 'react';

setOptions({
  locale:  ,
  theme: ' ',
  themeVariant: ' '
});

function App() {
  const [myEvents, setEvents] = useState([
    {
      id: 1,
      allDay: true,
      start: '2026-10-21T06:00',
      end: '2026-10-21T19:00',
      resource: 'orlando_1',
      title: 'Sales meeting with JLL',
      name: 'John C. Johnson',
      distance: 120,
      consumption: 35,
    },
    {
      id: 2,
      allDay: true,
      start: '2026-10-22T03:00',
      end: '2026-10-22T20:00',
      resource: 'orlando_1',
      title: 'Client consultation',
      name: 'Mary K. Adams',
      distance: 95,
      consumption: 30,
    },
    {
      id: 3,
      allDay: true,
      start: '2026-10-24T10:00',
      end: '2026-10-24T17:00',
      resource: 'orlando_1',
      title: 'Project review at XYZ',
      name: 'Robert B. Brown',
      distance: 150,
      consumption: 40,
    },
    {
      id: 4,
      allDay: true,
      start: '2026-10-26T09:00',
      end: '2026-10-26T16:00',
      resource: 'orlando_1',
      title: 'Team building workshop',
      name: 'Susan E. Smith',
      distance: 60,
      consumption: 20,
    },
    // Showing partial data. Download full source.
  ]);

  const [eventsWithSummaries, setEventsWithSummaries] = useState(myEvents);

  const firstViewDay = useRef(null);
  const lastViewDay = useRef(null);

  const myView = useMemo(
    () => ({
      timeline: {
        type: 'week',
        eventDisplay: 'fill',
        eventHeight: 'variable',
      },
    }),
    [],
  );

  const myResources = useMemo(
    () => [
      {
        id: 'orlando',
        name: 'CNL Tower II',
        address: '420 S Orange Ave, Orlando, FL 32801',
        eventCreation: false,
        eventDragBetweenResources: false,
        eventDragInTime: false,
        eventResize: false,
        color: 'transparent',
        children: [
          {
            id: 'orlando_1',
            car: 'Lincoln Continental',
            plate: '51-5375',
            type: 'Sedan',
          },
          {
            id: 'orlando_2',
            car: 'Porsche Panamera',
            plate: '72-2590',
            type: 'Sports sedan',
          },
          {
            id: 'orlando_3',
            car: 'Range Rover Velar',
            plate: '41-7701',
            type: 'Compact SUV',
          },
        ],
      },
      // Showing partial data. Download full source.
    ],
    [],
  );

  const handleDefaultEvent = useCallback((args) => {
    const d = args.start;
    const start = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 9);
    const end = new Date(start.getFullYear(), start.getMonth(), start.getDate(), 18);
    return {
      allDay: true,
      start: start,
      end: end,
      title: 'New event',
      name: 'John Doe',
      distance: 42,
      consumption: 6,
    };
  }, []);

  const getAggregateEvents = useCallback(
    (dayEvents) => {
      const aggregateEvents = {};

      myResources.forEach((resource) => {
        for (let date = new Date(firstViewDay.current); date < lastViewDay.current; date.setDate(date.getDate() + 1)) {
          aggregateEvents[resource.id + +date] = {
            id: resource.id + +date,
            isSummary: true,
            allDay: true,
            start: new Date(date),
            end: new Date(date),
            consumption: 0,
            distance: 0,
            resource: resource.id,
          };
        }
      });
      dayEvents.forEach((event) => {
        if (!event.isSummary) {
          const parentResource = event.resource.split('_')[0];
          const dayStart = new Date(event.start).setHours(0, 0, 0, 0);
          if (aggregateEvents[parentResource + dayStart]) {
            aggregateEvents[parentResource + dayStart].consumption += event.consumption;
            aggregateEvents[parentResource + dayStart].distance += event.distance;
          }
        }
      });

      return Object.values(aggregateEvents);
    },
    [firstViewDay, lastViewDay, myResources],
  );

  const updateCalendarEvents = useCallback(
    (inst) => {
      const dailyEvents = inst.getEvents();
      const updatedSummaries = getAggregateEvents(dailyEvents);
      setEventsWithSummaries([...myEvents, ...updatedSummaries]);
    },
    [getAggregateEvents, myEvents],
  );

  const handlePageLoading = useCallback(
    (args, inst) => {
      firstViewDay.current = new Date(args.firstDay);
      lastViewDay.current = new Date(args.lastDay);
      setTimeout(() => updateCalendarEvents(inst));
    },
    [updateCalendarEvents],
  );

  const handleEventUpdated = useCallback(
    (args, inst) => {
      const updatedEvent = args.event;
      const index = myEvents.indexOf(updatedEvent);
      myEvents.splice(index, 1, updatedEvent);
      setTimeout(() => updateCalendarEvents(inst));
    },
    [myEvents, updateCalendarEvents],
  );

  const handleEventCreated = useCallback(
    (args, inst) => {
      setEvents((prevEvents) => [...prevEvents, args.event]);
      setTimeout(() => updateCalendarEvents(inst));
    },
    [updateCalendarEvents],
  );

  const handleEventDeleted = useCallback(
    (args, inst) => {
      setEvents((prevEvents) => prevEvents.filter((item) => item.id !== args.event.id));
      setTimeout(() => updateCalendarEvents(inst));
    },
    [updateCalendarEvents],
  );

  const customResource = useCallback((res) => {
    if (res.isParent) {
      return (
        <>
          <div>{res.name}</div>
          <div className="mds-aggregates-address">{res.address}</div>
        </>
      );
    }
    return (
      <>
        <div>{res.car}</div>
        <div className="mds-aggregates-plate">Plate {res.plate}</div>
        <div className="mds-aggregates-cartype">{res.type}</div>
      </>
    );
  }, []);

  const customScheduleEventContent = useCallback((event) => {
    if (event.currentResource.isParent) {
      return (
        <>
          <div className="mds-aggregates-summary-event mbsc-flex">
            <div className="mds-aggregates-summary-details mbsc-flex mbsc-flex-1-1">
              <div className="mds-aggregates-summary-label">
                Total dist.
                <div className="mds-aggregates-summary-value">{event.original.distance} mi</div>
              </div>
              <div className="mds-aggregates-summary-label">
                Total cons.
                <div className="mds-aggregates-summary-value">{event.original.consumption} gal</div>
              </div>
              <div className="mds-aggregates-summary-label">
                Est. cost
                <div className="mds-aggregates-summary-value">$ {Math.round(event.original.consumption * 3.706 || 0)}</div>
              </div>
            </div>
          </div>
        </>
      );
    }
    return (
      <>
        <div className="mds-aggregates-event">
          <div className="mds-aggregates-title">{event.title}</div>
          <div className="mds-aggregates-name">{event.original.name}</div>
          <div className="mds-aggregates-details">
            <div className="mds-aggregates-label">
              Est. distance:
              <span className="mds-aggregates-value">{event.original.distance} mi</span>
            </div>
            <div className="mds-aggregates-label">
              Est. consumption:
              <span className="mds-aggregates-value">{event.original.consumption} gal</span>
            </div>
          </div>
          <div className="mds-aggregates-label mbsc-flex">
            <div className="mbsc-flex-1-1">
              Pick up: <span className="mds-aggregates-value">{formatDate('hh:mm A', new Date(event.original.start))}</span>
            </div>
            <div>
              Drop off: <span className="mds-aggregates-value">{formatDate('hh:mm A', new Date(event.original.end))}</span>
            </div>
          </div>
        </div>
      </>
    );
  }, []);

  return (
    <Eventcalendar
      cssClass="mds-aggregates"
      clickToCreate={true}
      dragToCreate={false}
      dragToMove={true}
      dragToResize={false}
      eventOverlap={false}
      view={myView}
      data={eventsWithSummaries}
      resources={myResources}
      min={'2026-10-21T00:00'}
      max={'2026-11-03T00:00'}
      extendDefaultEvent={handleDefaultEvent}
      onPageLoading={handlePageLoading}
      onEventUpdated={handleEventUpdated}
      onEventCreated={handleEventCreated}
      onEventDeleted={handleEventDeleted}
      renderResource={customResource}
      renderTimelineEventContent={customScheduleEventContent}
    />
  );
}

export default App;

Duplicate Session

You have signed in on a different machine. To keep everything in sync, you have been signed out of this session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.

Duplicate Session

We noticed you are logged in on a different machine and have closed your old session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.