ngx-gantt

repository·master·Indexed 43 days ago

https://github.com/worktile/ngx-gantt

A modern and powerful Gantt chart component for Angular applications (version ≥ 21). It provides high-performance visualization of tasks and timelines, featuring a flexible view system, customizable layouts via ngx-gantt-root, and the ability to create custom time granularities by extending the GanttView abstract class. Version 22.0.0.

Tokens
29.3K
Snippets
81
Records
104
Agent score
53%

What's inside ngx-gantt

  1. Overview of ngx-gantt features

    master

    ngx-gantt is an Angular-based Gantt chart library for task management, time planning, and progress tracking. Key capabilities include:

    • Task Management: Create, edit, and drag tasks to adjust time.
    • Dependencies: Create relationships between tasks (FS, FF, SS, SF) via dragging.
    • Grouping & Hierarchy: Supports infinite nested levels with asynchronous loading of child tasks.
    • Multiple Views: Built-in views for hour, day, week, month, quarter, and year, with support for custom view types.
    • High Performance: Uses virtual scrolling to handle thousands of tasks smoothly.
    • Rich Interactions: Includes bar dragging, table row dragging, selection, and expand/collapse.
    • Extensibility: Supports custom templates, a theming system, and custom views.
    • Multi-language: Supports English, Chinese (Simplified/Traditional), Japanese, German, and Russian.
    • Baseline Comparison: Compare planned vs. actual time.
    • Export: Export the current view as an image.
  2. Use Baselines to track project deviations

    master

    Baselines allow you to display an original schedule in a project plan to compare it against actual execution time. In the Gantt chart, baselines appear as gray bars positioned below the task bars. To implement baselines, pass an array of GanttBaselineItem objects to the [baselineItems] property of the ngx-gantt component. Each baseline item must have an id that matches the id of the corresponding task in your items array.

    import { Component } from '@angular/core';
    import { GanttItem, GanttBaselineItem } from '@worktile/gantt';
    
    @Component({
      template: `
        <ngx-gantt [items="items" [baselineItems="baselineItems">
          <!-- ... -->
        </ngx-gantt>
      `
    })
    export class MyComponent {
      items: GanttItem[] = [
        { id: '1', title: 'Task 1', start: 1627729997, end: 1628421197 }
      ];
    
      baselineItems: GanttBaselineItem[] = [
        { id: '1', start: 1627728888, end: 1628421197 }
      ];
    }
  3. Configure global defaults via GANTT_GLOBAL_CONFIG

    master

    Use the GANTT_GLOBAL_CONFIG injection token to set application-level defaults for the Gantt component. This configuration controls internationalization (locale), date/time settings (timezone, week start), dependency link appearance, and visual styling (colors, heights, themes).

    interface GanttGlobalConfig {
      locale?: GanttI18nLocale | string;
      dateOptions?: {
        timeZone?: string;
        weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
      };
      linkOptions?: {
        dependencyTypes?: GanttLinkType[];
        showArrow?: boolean;
        lineType?: 'curve' | 'straight';
      };
      styleOptions?: {
        primaryColor?: string;
        headerHeight?: number;
        rowHeight?: number;
        barHeight?: number;
        defaultTheme?: string;
        themes?: Record<string, any>;
      };
    }
  4. Understand the core concepts of ngx-gantt

    master

    To use ngx-gantt effectively, you must understand its three foundational pillars:

    1. Data Model: Defines how task data is structured and represented using types like GanttItem and GanttGroup.
    2. View System: Manages how the Gantt chart is displayed, including switching between different view types and configuring ViewOptions.
    3. Time Handling: Governs how the component parses, formats, and operates on time, including timezone management and GanttDate operations.
  5. Organize data in Tree Mode

    master

    Tree Mode uses a hierarchical structure where tasks contain a children array of other GanttItem objects. This allows for nested sub-tasks.

    Example Implementation:

    const items: GanttItem[] = [
      {
        id: '1',
        title: 'Project Kickoff',
        start: 1627729997,
        end: 1633345997,
        children: [
          {
            id: '1-1',
            title: 'Requirements Analysis',
            start: 1627729997,
            end: 1628421197,
            children: [
              { id: '1-1-1', title: 'Requirements Research', start: 1627729997, end: 1627902797 }
            ]
          },
          {
            id: '1-2',
            title: 'Technology Selection',
            start: 1628507597,
            end: 1630667597
          }
        ]
      }
    ];
  6. How to use Group Mode in ngx-gantt

    master

    ngx-gantt allows you to organize tasks into groups (e.g., by department, project, or phase) using two distinct arrays: groups and items.

    • groups: An array of GanttGroup objects. Each group must have an id and a title.
    • items: An array of GanttItem objects. To associate a task with a group, set its group_id property to match the id of the desired group.

    This structure enables hierarchical organization within the Gantt chart.

    import { Component } from '@angular/core';
    import { GanttGroup, GanttItem, GanttViewType } from '@worktile/gantt';
    
    @Component({
      template: `
        <ngx-gantt [groups="groups" [items="items" [viewType="viewType">
          <ngx-gantt-table>
            <ngx-gantt-column name="Task" width="200px">
              <ng-template #cell let-item="item">
                {{ item.title }}
              </ng-template>
            </ngx-gantt-column>
          </ngx-gantt-table>
        </ngx-gantt>
      `
    })
    export class GroupsExampleComponent {
      viewType = GanttViewType.month;
    
      groups: GanttGroup[] = [
        { id: 'dev', title: 'Development Team' },
        { id: 'test', title: 'QA Team' }
      ];
    
      items: GanttItem[] = [
        { id: '1', title: 'Frontend Development', group_id: 'dev', start: 1627729997, end: 1628421197 },
        { id: '2', title: 'Backend Development', group_id: 'dev', start: 1628507597, end: 1633345997 },
        { id: '3', title: 'Functional Testing', group_id: 'test', start: 1633433997, end: 1636035597 }
      ];
    }
  7. Organize data in Group Mode

    master

    In Group Mode, you define a set of GanttGroup objects and a set of GanttItem objects. You link tasks to groups using the group_id property on the GanttItem.

    GanttGroup Interface:

    • id: Unique identifier (required).
    • title: Group title (required).
    • expanded: Whether the group is expanded by default (default true).
    • class: Custom CSS class name.
    • origin: Original business data.

    Example Implementation:

    const groups: GanttGroup[] = [
      { id: 'group1', title: 'Development Team' },
      { id: 'group2', title: 'QA Team' }
    ];
    
    const items: GanttItem[] = [
      { id: '1', title: 'Frontend Development', group_id: 'group1', start: 1627729997, end: 1628421197 },
      { id: '2', title: 'Backend Development', group_id: 'group1', start: 1628507597, end: 1633345997 },
      { id: '3', title: 'Functional Testing', group_id: 'group2', start: 1633433997, end: 1636035597 }
    ];
  8. How to implement a custom Gantt view

    master

    When the built-in 6 view types do not meet your requirements (e.g., you need special time granularity like biweekly/bimonthly views or custom time range calculation logic), you can create a custom view by extending the GanttView abstract class.

    Note: If you only need to adjust view styles, use CSS. If you only need to adjust time formats, use viewOptions.tickFormats instead of implementing a custom view.

    import { GanttView } from '@worktile/gantt';
    
    export class MyCustomView extends GanttView {
      override viewType = 'my-custom-type';
      // Implement required methods like rangeStartOf, rangeEndOf, getPeriodWidth, etc.
    }
  9. Create custom views by extending GanttView

    master
    If you need special time granularity (e.g., biweekly views) or custom tick displays (e.g., custom workday views), you should implement a custom view. This is achieved by extending the GanttView abstract class. To implement this successfully, you must understand the view system, tick calculation logic, and formatting strategies used by ngx-gantt.
  10. Update Gantt data immutably

    master

    ngx-gantt uses the OnPush change detection strategy. To trigger view updates after modifying data (e.g., in a dragEnded event), you must provide a new array reference rather than mutating the existing one.

    Correct (Immutable):

    dragEnded(event: GanttDragEvent) {
      // Create a new array reference to trigger change detection
      this.items = [...this.items];
    }

    Incorrect (Mutable):

    dragEnded(event: GanttDragEvent) {
      // Modifying the object property directly will NOT trigger view updates
      event.item.start = newDate;
      event.item.end = newDate;
    }
    // ✅ Correct
    dragEnded(event: GanttDragEvent) {
      this.items = [...this.items];
    }
  11. Register global configuration in a Standalone application

    master

    In a Standalone Angular application, provide the GANTT_GLOBAL_CONFIG token within the providers array of the bootstrapApplication function.

    import { GANTT_GLOBAL_CONFIG, GanttGlobalConfig } from '@worktile/gantt';
    
    bootstrapApplication(AppComponent, {
      providers: [
        ...,
        {
          provide: GANTT_GLOBAL_CONFIG,
          useValue: {
            dateOptions: {
              timeZone: 'Asia/Shanghai',
              weekStartsOn: 1
            },
            linkOptions: {
              dependencyTypes: [GanttLinkType.fs],
              showArrow: false,
              lineType: 'curve'
            },
            styleOptions: {
              primaryColor: '#6698ff',
              headerHeight: 44,
              rowHeight: 44,
              barHeight: 22
            }
          } as GanttGlobalConfig
        }
      ]
    }).catch(err => console.error(err));
  12. Install @worktile/gantt

    master

    Install the core Gantt chart component using npm or yarn:

    npm install @worktile/gantt
    # or
    yarn add @worktile/gantt

    If you plan to use the GanttPrintService for exporting or printing the chart, you must also install html2canvas as an optional dependency:

    npm install html2canvas
    npm install @worktile/gantt