Resource Footers & Revenue Sidebar

The resources can be rendered vertically either as plain text or based on a custom grid template. This example utilizes the properties of the resource objects, which are conference rooms with capacities and pricing. Along with the resource template defined using resourceTemplate , an additional sidebar is rendered on the opposite side of the row using sidebarTemplate , displaying the calculated revenue.

Both the resource and sidebar columns have customizable headers and footers, which are set using the resourceHeaderTemplate, resourceFooterTemplate, sidebarHeaderTemplate, sidebarFooterTemplate templates.

The occupancy percentages that can be seen for each day are set through the dayFooterTemplate in a similar way to the day header templates.

The headers are equipped with sorting functionality, allowing data to be sorted on click. Event listeners are attached to the resource, day, and sidebar headers, making them clickable. Each header has a custom icon indicating the current sorting state (ascending, descending, or unordered). Clicking a header cycles through these sorting states, with the corresponding icon reflecting the current state. The initial order in which the resources appear follows the order of the array passed to the component.

The resources column has a default width that may not be sufficient to fit its content. This can be adjusted by overriding it with a CSS rule:

.md-resource-details .mbsc-timeline-resource-col {
            width: 280px;
            }
Get started with this example

Demo

Loading the live demo

Preview in full screen

Add custom resource column footers, a revenue sidebar, and per-day occupancy to a resource timeline — all column headers are sortable.

Code

import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit, ViewChild } from '@angular/core';
import {
  formatDate,
  MbscCalendarEvent,
  MbscEventcalendar,
  MbscEventcalendarView,
  MbscModule,
  MbscResource,
  setOptions ,
} from '@mobiscroll/angular';

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

interface MyResource extends MbscResource {
  busyHours?: number;
  price: number;
  revenue?: number;
  seats: number;
}

@Component({
  selector: 'app-root', 
  templateUrl: './app.component.html',
  standalone: true,
  imports: [CommonModule, MbscModule],
})
export class AppComponent implements OnInit {
  constructor(private http: HttpClient) {}

  @ViewChild('myCalendar', { static: false })
  myCalendar!: MbscEventcalendar;

  formatDate = formatDate;
  myEvents: MbscCalendarEvent[] = [];
  loadedEvents: MbscCalendarEvent[] = [];
  sortColumn = '';
  sortDirection = 'asc';
  sortDay: number | undefined;
  totalRevenue = 0;

  myResources: MyResource[] = [
    { id: 1, name: 'Horizon', seats: 1200, color: '#4a4a4a', price: 1000 },
    { id: 2, name: 'Apex Hall', seats: 90, color: '#fdf500', price: 600 },
    { id: 3, name: 'Jade Room', seats: 700, color: '#00aaff', price: 900 },
    { id: 4, name: 'Dome Arena', seats: 850, color: '#239a21', price: 750 },
    { id: 5, name: 'Forum Plaza', seats: 900, color: '#8f1ed6', price: 700 },
    { id: 6, name: 'Gallery', seats: 300, color: '#0077b6', price: 650 },
    { id: 7, name: 'Icon Hall', seats: 450, color: '#e63946', price: 850 },
    { id: 8, name: 'Broadway', seats: 250, color: '#ff0101', price: 800 },
    { id: 9, name: 'Central Hub', seats: 400, color: '#01adff', price: 1100 },
    { id: 10, name: 'Empire Hall', seats: 550, color: '#ff4600', price: 950 },
  ];

  view: MbscEventcalendarView = {
    timeline: {
      type: 'month',
    },
  };

  getUTCDateOnly(d: Date) {
    return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
  }

  getDayDiff(d1: Date, d2: Date) {
    return Math.round((this.getUTCDateOnly(d2) - this.getUTCDateOnly(d1)) / (60 * 60 * 24 * 1000)) + 1;
  }

  getRevenue(resource: MyResource) {
    if (this.myCalendar) {
      let days = 0;
      for (const event of this.loadedEvents) {
        if (event.resource === resource.id) {
          days += this.getDayDiff(new Date(event.start as Date), new Date(event.end as Date));
        }
      }
      return days * resource.price;
    } else {
      return 0;
    }
  }

  getOccuppancy(events: MbscCalendarEvent[]) {
    let occuppancy = 0;
    if (events) {
      const resourceIds: string[] = [];
      let nr = 0;
      for (const event of events) {
        const resource = event.resource as string;
        if (resourceIds.indexOf(resource) < 0) {
          nr++;
          resourceIds.push(resource);
        }
      }
      occuppancy = (nr * 100) / this.myResources.length;
    }
    return occuppancy.toFixed(0);
  }

  getSortArrow(column: string, day?: number): string {
    if (this.sortColumn === column && day === this.sortDay) {
      return this.sortDirection === 'asc' ? 'asc' : this.sortDirection === 'desc' ? 'desc' : 'def';
    }
    return 'def';
  }

  getBusyHours(resource: MbscResource, timestamp: number): number {
    const startOfDay = new Date(timestamp);
    const endOfDay = new Date(startOfDay.getFullYear(), startOfDay.getMonth(), startOfDay.getDate() + 1);

    return this.loadedEvents.reduce((totalHours, event) => {
      if (event.resource === resource.id) {
        const eventStart = Math.max(+startOfDay, new Date(event.start as Date).getTime());
        const eventEnd = Math.min(+endOfDay, new Date(event.end as Date).getTime());
        return totalHours + (eventStart < eventEnd ? (eventEnd - eventStart) / (60 * 60 * 1000) : 0);
      }
      return totalHours;
    }, 0);
  }

  sortResources(column?: string, day?: number): void {
    if (column) {
      if (this.sortColumn === column && day === this.sortDay) {
        this.sortDirection = this.sortDirection === 'asc' ? 'desc' : this.sortDirection === 'desc' ? 'def' : 'asc';
      } else {
        this.sortColumn = column;
        this.sortDirection = 'asc';
      }
      this.sortDay = day;
    }

    if (day) {
      this.myResources.forEach((resource) => {
        resource.busyHours = this.getBusyHours(resource, this.sortDay!);
      });
    }

    this.myResources = [
      ...this.myResources.sort((a: MbscResource, b: MbscResource) => {
        if (this.sortDirection === 'asc') {
          return a[this.sortColumn] > b[this.sortColumn] ? 1 : -1;
        }
        if (this.sortDirection === 'desc') {
          return a[this.sortColumn] < b[this.sortColumn] ? 1 : -1;
        }
        return +a.id - +b.id;
      }),
    ];
  }

  refreshData() {
    setTimeout(() => {
      this.loadedEvents = this.myCalendar.getEvents();
      this.myResources.forEach((resource) => {
        resource.revenue = this.getRevenue(resource);
      });
      this.totalRevenue = this.myResources.reduce((total, resource) => total + resource.revenue!, 0);
      this.sortResources();
    }, 0);
  }

  ngOnInit(): void {
    this.http.jsonp<MbscCalendarEvent[]>('https://trial.mobiscroll.com/multiday-events/', 'callback').subscribe((resp) => {
      this.myEvents = resp;
      this.refreshData();
    });
  }
}

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.