### Install Peer Dependencies
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/getting-started.md
Install the required peer dependencies if they are not already present.
```bash
npm install @angular/cdk date-fns @date-fns/tz --save
```
--------------------------------
### Full Gantt Chart Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/getting-started.md
A complete example demonstrating a Gantt chart with items and configuration.
```typescript
import { Component } from '@angular/core';
import { NgxGanttComponent, NgxGanttTableComponent, NgxGanttTableColumnComponent, GanttItem, GanttViewType } from '@worktile/gantt';
@Component({
selector: 'app-gantt-example',
standalone: true,
imports: [NgxGanttComponent, NgxGanttTableComponent, NgxGanttTableColumnComponent],
template: `
{{ item.title }}
`
})
export class GanttExampleComponent {
viewType = GanttViewType.day;
items: GanttItem[] = [
{
id: '1',
title: 'Design Phase',
start: 1627729997,
end: 1628421197
},
{
id: '2',
title: 'Development Phase',
start: 1628507597,
end: 1633345997
},
{
id: '3',
title: 'Testing Phase',
start: 1633433997,
end: 1636035597
}
];
}
```
--------------------------------
### Install Core Package
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/getting-started.md
Install the ngx-gantt core package using npm or yarn.
```bash
npm install @worktile/gantt --save
# or
yarn add @worktile/gantt
```
--------------------------------
### Development Setup
Source: https://github.com/worktile/ngx-gantt/blob/master/README.md
Commands to clone the repository, install dependencies, and start the development server.
```bash
git clone git@github.com:worktile/ngx-gantt.git
cd ngx-gantt
npm ci
npm run start
```
--------------------------------
### Custom Handling Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/print.md
An example showing how to get the canvas, convert it to a blob, and upload it to a server using HttpClient.
```typescript
import { HttpClient } from '@angular/common/http';
@Component({
providers: [GanttPrintService]
})
export class GanttPrintComponent {
constructor(
private printService: GanttPrintService,
private http: HttpClient
) {}
async exportAndUpload() {
try {
const canvas = await this.printService.html2canvas('toolbar');
const blob = await new Promise((resolve) => {
canvas.toBlob((blob) => resolve(blob), 'image/png');
});
// Upload to the server
const formData = new FormData();
formData.append('image', blob, 'gantt-chart.png');
this.http.post('/api/upload', formData).subscribe({
next: (response) => {
console.log('Upload successful', response);
},
error: (error) => {
console.error('Upload failed', error);
}
});
} catch (error) {
console.error('Export failed', error);
}
}
}
```
--------------------------------
### View Configuration Migration Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/upgrade-to-21.md
Demonstrates the transformation of old v20 view configuration options to the new v21 structure, highlighting field renaming and the integration of scroll-loading logic.
```typescript
// ❌ Old v20 configuration
const viewOptions = {
cellWidth: 40,
addAmount: 1,
addUnit: 'month',
hoilday: { isHoliday: true },
datePrecisionUnit: 'day'
};
// ✅ New v21 configuration
const viewOptions = {
unitWidth: 40,
loadDuration: { amount: 1, unit: 'month' }, // Structured merge
holiday: { isHoliday: true }, // Spelling correction
precisionUnit: 'day'
};
```
--------------------------------
### NgModule Import
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/getting-started.md
Example of importing ngx-gantt module in a traditional Angular NgModule.
```typescript
import { NgModule } from '@angular/core';
import { NgxGanttModule } from '@worktile/gantt';
@NgModule({
imports: [
NgxGanttModule
// ... other modules
]
// ...
})
export class AppModule {}
```
--------------------------------
### Minimal Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/toolbar.md
A minimal example demonstrating the toolbar functionality.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType, GanttToolbarOptions, GanttView } from '@worktile/gantt';
@Component({
selector: 'app-gantt-toolbar',
template: `
{{ item.title }}
`
})
export class GanttToolbarComponent {
viewType = GanttViewType.day;
toolbarOptions: GanttToolbarOptions = {
viewTypes: [GanttViewType.day, GanttViewType.week, GanttViewType.month]
};
items: GanttItem[] = [{ id: '1', title: 'Task 1', start: 1627729997, end: 1628421197 }];
onViewChange(event: GanttView) {
this.viewType = event.viewType;
}
}
```
--------------------------------
### Configure Styles in angular.json
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/getting-started.md
Recommended method to import ngx-gantt styles by adding the path to the styles array in angular.json.
```json
{
"styles": ["node_modules/@worktile/gantt/styles/index.scss"]
}
```
--------------------------------
### Standalone Component Import
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/getting-started.md
Example of importing ngx-gantt components in a standalone Angular component.
```typescript
import { Component } from '@angular/core';
import { NgxGanttComponent, NgxGanttTableComponent, NgxGanttTableColumnComponent, GanttItem, GanttViewType } from '@worktile/gantt';
@Component({
selector: 'app-gantt-example',
standalone: true,
imports: [NgxGanttComponent, NgxGanttTableComponent, NgxGanttTableColumnComponent],
template: `
{{ item.title }}
`
})
export class GanttExampleComponent {
viewType = GanttViewType.day;
items: GanttItem[] = [
{
id: '1',
title: 'Design Phase',
start: 1627729997, // Unix timestamp (seconds)
end: 1628421197
},
{
id: '2',
title: 'Development Phase',
start: 1628507597,
end: 1633345997
},
{
id: '3',
title: 'Testing Phase',
start: 1633433997,
end: 1636035597
}
];
}
```
--------------------------------
### New Theme Configuration Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/upgrade-to-21.md
Shows the updated theme configuration in v21, which supports defining multiple color schemes via the `themes` field and includes a renamed `rowHeight` option.
```typescript
styleOptions: {
rowHeight: 44,
defaultTheme: 'default',
primaryColor: '#1890ff', // Quickly override the primary tone
themes: {
default: {
primary: '#1890ff',
background: '#ffffff',
text: { main: '#333333' },
gray: { 100: '#fafafa', 500: '#d9d9d9' }
}
}
}
```
--------------------------------
### Minimal Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/print.md
A complete minimal example demonstrating how to set up and export a Gantt chart, including registering the Gantt root element.
```typescript
import { Component, viewChild, afterNextRender } from '@angular/core';
import { GanttItem, GanttViewType, NgxGanttComponent, GanttPrintService } from '@worktile/gantt';
@Component({
selector: 'app-gantt-print',
template: `
{{ item.title }}
`,
providers: [GanttPrintService]
})
export class GanttPrintComponent {
gantt = viewChild('gantt');
viewType = GanttViewType.day;
items: GanttItem[] = [{ id: '1', title: 'Task 1', start: 1627729997, end: 1628421197 }];
constructor(private printService: GanttPrintService) {
afterNextRender(() => {
// Register the Gantt chart root element
const gantt = this.gantt();
if (gantt) {
const ganttRoot = gantt.ganttRoot();
if (ganttRoot) {
this.printService.register(ganttRoot.elementRef);
}
}
});
}
exportImage() {
// Ignore the toolbar
this.printService.print('gantt-chart', 'toolbar');
}
}
```
--------------------------------
### Custom Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/baseline.md
Example of how to use a custom template for baselines.
```html
```
```scss
.custom-baseline {
position: absolute;
bottom: 2px;
height: 4px;
background: #ff6b6b;
opacity: 0.6;
border-radius: 2px;
z-index: 2;
}
```
--------------------------------
### Minimal Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/bar.md
A basic example demonstrating how to set up ngx-gantt with a table column and sample data.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType } from '@worktile/gantt';
@Component({
selector: 'app-gantt-bar',
template: `
{{ item.title }}
`
})
export class GanttBarComponent {
viewType = GanttViewType.day;
items: GanttItem[] = [
{ id: '1', title: 'Task 1', start: 1627729997, end: 1628421197 },
{ id: '2', title: 'Task 2', start: 1628507597, end: 1633345997 }
];
}
```
--------------------------------
### Installation
Source: https://github.com/worktile/ngx-gantt/blob/master/README.md
Install the ngx-gantt package using npm or yarn.
```bash
npm install @worktile/gantt
# or
yarn add @worktile/gantt
```
--------------------------------
### Basic Table Structure
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/table.md
This example shows the basic structure of the ngx-gantt table with two columns: 'Task Name' and 'Start Time'. It demonstrates how to define columns and customize cell content using templates.
```html
{{ item.title }} {{ item.start | date: 'yyyy-MM-dd' }}
```
--------------------------------
### Minimal Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/table.md
A minimal example demonstrating the ngx-gantt table with draggable functionality and handling the dragDropped event.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType, GanttTableDragDroppedEvent } from '@worktile/gantt';
@Component({
selector: 'app-gantt-table',
template: `
{{ item.title }}
`
})
export class GanttTableComponent {
viewType = GanttViewType.day;
items: GanttItem[] = [
{ id: '1', title: 'Task 1', start: 1627729997, end: 1628421197 },
{ id: '2', title: 'Task 2', start: 1628507597, end: 1633345997 }
];
onDragDropped(event: GanttTableDragDroppedEvent) {
// Handle drag drop logic
this.reorganizeItems(event);
// Immutable update
this.items = [...this.items];
}
private reorganizeItems(event: GanttTableDragDroppedEvent) {
// Implement drag-and-drop data reorganization logic
}
}
```
--------------------------------
### Full Example with Link Drag and Drop
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/links.md
An example demonstrating how to enable link creation via drag and drop and handle the `linkDragEnded` event.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType, GanttLinkDragEvent, GanttLinkType } from '@worktile/gantt';
@Component({
template: `
{{ item.title }}
`
})
export class GanttLinksComponent {
viewType = GanttViewType.day;
items: GanttItem[] = [
{ id: '1', title: 'Task 1', start: 1627729997, end: 1628421197 },
{ id: '2', title: 'Task 2', start: 1628507597, end: 1633345997 }
];
onLinkDragEnded(event: GanttLinkDragEvent) {
this.items = this.items.map((item) => {
if (item.id === event.source.id) {
return {
...item,
links: [...(item.links || []), { type: event.type, link: event.target.id }]
};
}
return item;
});
}
}
```
--------------------------------
### v21 i18n Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/getting-started/upgrade-to-21.md
Illustrates the new i18n structure in v21, where time-axis text formatting has moved from `dateFormats` to `tickFormats`.
```typescript
// v21 i18n example
const localeConfig = {
id: 'zh-cn',
views: {
day: {
label: 'Day',
tickFormats: {
period: 'YYYY-MM', // corresponds to original primary
unit: 'D' // corresponds to original secondary
}
}
}
};
```
--------------------------------
### Minimal Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/views.md
A basic example demonstrating how to set up a Gantt chart with a custom view configuration, including date ranges, unit width, tick formatting, drag tooltips, scroll loading, and holiday settings.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType, GanttViewOptions, GanttDate } from '@worktile/gantt';
@Component({
selector: 'app-gantt-view',
template: `
{{ item.title }}
`
})
export class GanttViewComponent {
viewType = GanttViewType.day;
viewOptions: GanttViewOptions = {
// Custom time range
start: new GanttDate('2021-01-01'),
end: new GanttDate('2021-12-31'),
minBoundary: new GanttDate('2020-01-01'),
maxBoundary: new GanttDate('2022-12-31'),
// Unit width
unitWidth: 35,
// Tick formatting
tickFormats: {
period: 'yyyy-MM',
unit: 'dd'
},
// Drag tooltip format
dragTooltipFormat: 'MM-dd HH:mm',
// Scroll loading configuration
loadDuration: {
amount: 3,
unit: 'month'
},
// Holiday configuration (supported in day view only)
holiday: {
isHoliday: (date: GanttDate) => {
return date.isWeekend();
},
hideHoliday: false
}
};
items: GanttItem[] = [
{
id: '1',
title: 'Task 1',
start: 1627729997,
end: 1628421197
}
];
}
```
--------------------------------
### Optional: html2canvas installation
Source: https://github.com/worktile/ngx-gantt/blob/master/README.md
Install html2canvas if you plan to use export or print features.
```bash
npm install html2canvas
```
--------------------------------
### Global Timezone and Week Start Configuration
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/date-timezone.md
Example of configuring the global timezone and the start day of the week for the Gantt chart using GANTT_GLOBAL_CONFIG.
```typescript
import { bootstrapApplication } from '@angular/platform-browser';
import { GANTT_GLOBAL_CONFIG, GanttGlobalConfig } from '@worktile/gantt';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, {
providers: [
{
provide: GANTT_GLOBAL_CONFIG,
useValue: {
dateOptions: {
timeZone: 'Asia/Shanghai', // Timezone
weekStartsOn: 1 // Week starts on: 0=Sunday, 1=Monday
}
} as GanttGlobalConfig
}
]
}).catch((err) => console.error(err));
```
--------------------------------
### Minimal ngx-gantt-root Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/advanced/advanced-layout.md
A basic example demonstrating the usage of ngx-gantt-root with custom side and main templates, including task bar rendering.
```typescript
import { Component, OnInit } from '@angular/core';
import { GANTT_UPPER_TOKEN, GanttUpper, NgxGanttRootComponent, NgxGanttBarComponent, GanttItemInternal } from '@worktile/gantt';
@Component({
selector: 'app-custom-gantt',
template: `
@for (item of customItems; track item.id) {
{{ item.title }}
}
@for (item of customItems; track item.id) {
}
`,
providers: [
{
provide: GANTT_UPPER_TOKEN,
useExisting: AppCustomGanttComponent
}
],
imports: [NgxGanttRootComponent, NgxGanttBarComponent]
})
export class AppCustomGanttComponent extends GanttUpper implements OnInit {
customItems: GanttItemInternal[] = [];
override ngOnInit() {
super.ngOnInit();
this.buildItems();
}
private buildItems() {
// Custom data processing logic
}
}
```
--------------------------------
### Minimal Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/range.md
A minimal example demonstrating the usage of range type tasks with a custom template.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType } from '@worktile/gantt';
@Component({
selector: 'app-gantt-range',
template: `
{{ item.title }}
{{ item.title }}
`
})
export class GanttRangeComponent {
viewType = GanttViewType.month;
items: GanttItem[] = [
{
id: '1',
title: 'Version V1.0',
start: 1627729997,
type: 'range'
},
{
id: '2',
title: 'Version V2.0',
start: 1630421197,
type: 'range'
}
];
}
```
--------------------------------
### Time Input Formats for GanttItem
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/date-timezone.md
Examples of how to provide start and end times for Gantt items using Unix timestamps and Date objects.
```typescript
const items1: GanttItem[] = [
{
id: '1',
title: 'Task',
start: 1627729997, // 10-digit Unix timestamp (seconds)
end: 1627729997000 // 13-digit Unix timestamp (milliseconds)
}
];
const items2: GanttItem[] = [
{
id: '2',
title: 'Task',
start: new Date(1627729997 * 1000),
end: new Date(1628421197 * 1000)
}
];
```
--------------------------------
### Holiday Configuration Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/views.md
Example demonstrating how to configure holiday display in the day view using the `holiday` option, including custom `isHoliday` logic and `hideHoliday` setting.
```typescript
const viewOptions: GanttViewOptions = {
holiday: {
isHoliday: (date: GanttDate) => {
// Determine whether it is a holiday
return date.isWeekend() || isHoliday(date.value);
},
hideHoliday: false // Whether to hide the holiday column; if true, it won’t be displayed but still takes space
}
};
```
--------------------------------
### Dependency Types Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/links.md
Illustrates the usage of different dependency types (`fs`, `ff`, `ss`, `sf`) with examples of how to define them in the `links` array.
```typescript
const items: GanttItem[] = [
{ id: 'A', title: 'Design', start: 1627729997, end: 1628421197 },
{
id: 'B',
title: 'Development',
links: [
{ type: GanttLinkType.fs, link: 'A' } // FS: Task A finishes → Task B can start
// { type: GanttLinkType.ff, link: 'A' }, // FF: Task A finishes → Task B can finish
// { type: GanttLinkType.ss, link: 'A' }, // SS: Task A starts → Task B can start
// { type: GanttLinkType.sf, link: 'A' } // SF: Task A starts → Task B can finish
]
}
];
```
--------------------------------
### Tick Formatting Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/views.md
Example demonstrating how to customize the format of period and unit ticks using `date-fns` format strings within `viewOptions`.
```typescript
const viewOptions: GanttViewOptions = {
tickFormats: {
period: 'yyyy-MM', // Period ticks (e.g., 2021-07)
unit: 'dd' // Unit ticks (e.g., 31)
}
};
```
--------------------------------
### Using origin to attach business data
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/data-model.md
Example demonstrating how to attach custom business data to a GanttItem and access it.
```typescript
interface MyBusinessData {
taskId: string;
assignee: string;
priority: number;
// ... other business fields
}
const items: GanttItem[] = [
{
id: '1',
title: 'Task 1',
start: 1627729997,
end: 1628421197,
origin: {
taskId: 'TASK-001',
assignee: 'John Doe',
priority: 1
} as MyBusinessData
}
];
// Access in event callbacks
dragEnded(event: GanttDragEvent) {
const businessData = event.item.origin as MyBusinessData;
console.log(businessData.assignee); // 'John Doe'
}
```
--------------------------------
### Drag Tooltip Format Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/views.md
Example showing how to customize the date format displayed in the drag tooltip using the `dragTooltipFormat` option.
```typescript
const viewOptions: GanttViewOptions = {
dragTooltipFormat: 'MM-dd HH:mm' // Default 'MM-dd'
};
```
--------------------------------
### Scroll Loading Configuration Example
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/core/views.md
Example of configuring the `loadDuration` to control the time span loaded during horizontal scrolling.
```typescript
const viewOptions: GanttViewOptions = {
loadDuration: {
amount: 1,
unit: 'month' // 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'
}
};
```
--------------------------------
### Complete Example - Basic Tree Structure
Source: https://github.com/worktile/ngx-gantt/blob/master/docs/en-us/guides/features/tree.md
A complete example of a basic tree structure in ngx-gantt, including a custom table column.
```typescript
import { Component } from '@angular/core';
import { GanttItem, GanttViewType } from '@worktile/gantt';
@Component({
template: `
{{ item.title }}
`
})
export class GanttTreeComponent {
viewType = GanttViewType.day;
items: GanttItem[] = [
{
id: '1',
title: 'Project',
start: 1627729997,
end: 1633345997,
expanded: true,
children: [
{
id: '1-1',
title: 'Stage 1',
start: 1627729997,
end: 1627902797
},
{
id: '1-2',
title: 'Stage 2',
start: 1628507597,
end: 1630667597
}
]
}
];
}
```
--------------------------------
### Gantt Component HTML
Source: https://github.com/worktile/ngx-gantt/blob/master/example/src/app/gantt-virtual-scroll/gantt.component.html
This HTML template shows how to use the Ngx-Gantt component with virtual scrolling enabled by default. It includes item titles, start and end dates, and column headers. It also demonstrates iterating through view units and conditionally rendering content.
```html