Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/4661 gantt customize days of weekend #5358

27 changes: 24 additions & 3 deletions cypress/integration/rendering/gantt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ describe('Gantt diagram', () => {
title Adding GANTT diagram to mermaid
excludes weekdays 2014-01-10
todayMarker off

section team's critical event
deadline A :milestone, crit, deadlineA, 2024-02-01, 0
deadline B :milestone, crit, deadlineB, 2024-02-15, 0
boss on leave :bossaway, 2024-01-28, 2024-02-11

section new intern
onboarding :onboarding, 2024-01-02, 1w
literature review :litreview, 2024-01-02, 10d
Expand Down Expand Up @@ -573,7 +573,28 @@ describe('Gantt diagram', () => {
`
);
});

it('should render a gantt diagram exculding friday and saturday', () => {
imgSnapshotTest(
`gantt
title A Gantt Diagram
dateFormat YYYY-MM-DD
excludes weekends
weekend friday
section Section1
A task :a1, 2024-02-28, 10d`
);
});
it('should render a gantt diagram exculding saturday and sunday', () => {
imgSnapshotTest(
`gantt
title A Gantt Diagram
dateFormat YYYY-MM-DD
excludes weekends
weekend saturday
section Section1
A task :a1, 2024-02-28, 10d`
);
});
it('should render when compact is true', () => {
imgSnapshotTest(
`
Expand Down
32 changes: 32 additions & 0 deletions docs/syntax/gantt.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ gantt

The `title` is an _optional_ string to be displayed at the top of the Gantt chart to describe the chart as a whole.

### Excludes

The `excludes` is an _optional_ attribute that accepts specific dates in YYYY-MM-DD format, days of the week ("sunday") or "weekends", but not the word "weekdays".
These date will be marked on the graph, and be excluded from the duration calculation of tasks. Meaning that if there are excluded dates during a task interval, the number of 'skipped' days will be added to the end of the task to ensure the duration is as specified in the code.

#### Weekend (v\<MERMAID_RELEASE_VERSION>+)

When excluding weekends, it is possible to configure the weekends to be either Friday and Saturday or Saturday and Sunday. By default weekends are Saturday and Sunday.
To define the weekend start day, there is an _optional_ attribute `weekend` that can be added in a new line followed by either `friday` or `saturday`.

```mermaid-example
gantt
title A Gantt Diagram Excluding Fri - Sat weekends
dateFormat YYYY-MM-DD
excludes weekends
weekend friday
section Section
A task :a1, 2024-01-01, 30d
Another task :after a1, 20d
```

```mermaid
gantt
title A Gantt Diagram Excluding Fri - Sat weekends
dateFormat YYYY-MM-DD
excludes weekends
weekend friday
section Section
A task :a1, 2024-01-01, 30d
Another task :after a1, 20d
```

### Section statements

You can divide the chart into various sections, for example to separate different parts of a project like development and documentation.
Expand Down
19 changes: 18 additions & 1 deletion packages/mermaid/src/diagrams/gantt/ganttDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
dayjs.extend(dayjsCustomParseFormat);
dayjs.extend(dayjsAdvancedFormat);

const WEEKEND_START_DAY = { friday: 5, saturday: 6 };
let dateFormat = '';
let axisFormat = '';
let tickInterval = undefined;
Expand All @@ -37,6 +38,7 @@
let inclusiveEndDates = false;
let topAxis = false;
let weekday = 'sunday';
let weekend = 'saturday';

// The serial order of the task in the script
let lastOrder = 0;
Expand All @@ -63,6 +65,7 @@
links = {};
commonClear();
weekday = 'sunday';
weekend = 'saturday';
};

export const setAxisFormat = function (txt) {
Expand Down Expand Up @@ -167,7 +170,11 @@
if (includes.includes(date.format(dateFormat.trim()))) {
return false;
}
if (date.isoWeekday() >= 6 && excludes.includes('weekends')) {
if (
excludes.includes('weekends') &&
(date.isoWeekday() == WEEKEND_START_DAY[weekend] ||
date.isoWeekday() == WEEKEND_START_DAY[weekend] + 1)
) {
Ronid1 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
if (excludes.includes(date.format('dddd').toLowerCase())) {
Expand All @@ -184,6 +191,14 @@
return weekday;
};

export const setWeekend = function (startDay) {
weekend = startDay;
};

export const getWeekend = function () {
return weekend;
};

Check warning on line 200 in packages/mermaid/src/diagrams/gantt/ganttDb.js

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagrams/gantt/ganttDb.js#L199-L200

Added lines #L199 - L200 were not covered by tests
Ronid1 marked this conversation as resolved.
Show resolved Hide resolved

/**
* TODO: fully document what this function does and what types it accepts
*
Expand Down Expand Up @@ -781,6 +796,8 @@
isInvalidDate,
setWeekday,
getWeekday,
setWeekend,
getWeekend,
};

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ describe('when using the ganttDb', function () {
expect(tasks[6].task).toEqual('test7');
});

it('should ignore weekends starting on friday', function () {
ganttDb.setDateFormat('YYYY-MM-DD');
ganttDb.setExcludes('weekends');
ganttDb.setWeekend('friday');
ganttDb.addSection('friday-saturday weekends skip test');
ganttDb.addTask('test1', 'id1,2024-02-28, 3d');

const tasks = ganttDb.getTasks();

expect(tasks[0].startTime).toEqual(dayjs('2024-02-28', 'YYYY-MM-DD').toDate());
expect(tasks[0].endTime).toEqual(dayjs('2024-03-04', 'YYYY-MM-DD').toDate());
expect(tasks[0].id).toEqual('id1');
expect(tasks[0].task).toEqual('test1');
});

it('should maintain the order in which tasks are created', function () {
ganttDb.setAccTitle('Project Execution');
ganttDb.setDateFormat('YYYY-MM-DD');
Expand Down
8 changes: 8 additions & 0 deletions packages/mermaid/src/diagrams/gantt/parser/gantt.jison
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ weekday\s+thursday return 'weekday_thursday'
weekday\s+friday return 'weekday_friday'
weekday\s+saturday return 'weekday_saturday'
weekday\s+sunday return 'weekday_sunday'
weekend\s+friday return 'weekend_friday'
weekend\s+saturday return 'weekend_saturday'
\d\d\d\d"-"\d\d"-"\d\d return 'date';
"title"\s[^\n]+ return 'title';
"accDescription"\s[^#\n;]+ return 'accDescription'
Expand Down Expand Up @@ -128,6 +130,11 @@ weekday
| weekday_sunday { yy.setWeekday("sunday");}
;

weekend
: weekend_friday { yy.setWeekend("friday");}
| weekend_saturday { yy.setWeekend("saturday");}
;

statement
: dateFormat {yy.setDateFormat($1.substr(11));$$=$1.substr(11);}
| inclusiveEndDates {yy.enableInclusiveEndDates();$$=$1.substr(18);}
Expand All @@ -138,6 +145,7 @@ statement
| includes {yy.setIncludes($1.substr(9));$$=$1.substr(9);}
| todayMarker {yy.setTodayMarker($1.substr(12));$$=$1.substr(12);}
| weekday
| weekend
| title {yy.setDiagramTitle($1.substr(6));$$=$1.substr(6);}
| acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); }
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }
Expand Down
21 changes: 21 additions & 0 deletions packages/mermaid/src/docs/syntax/gantt.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ gantt

The `title` is an _optional_ string to be displayed at the top of the Gantt chart to describe the chart as a whole.

### Excludes

The `excludes` is an _optional_ attribute that accepts specific dates in YYYY-MM-DD format, days of the week ("sunday") or "weekends", but not the word "weekdays".
These date will be marked on the graph, and be excluded from the duration calculation of tasks. Meaning that if there are excluded dates during a task interval, the number of 'skipped' days will be added to the end of the task to ensure the duration is as specified in the code.

#### Weekend (v\<MERMAID_RELEASE_VERSION>+)

When excluding weekends, it is possible to configure the weekends to be either Friday and Saturday or Saturday and Sunday. By default weekends are Saturday and Sunday.
To define the weekend start day, there is an _optional_ attribute `weekend` that can be added in a new line followed by either `friday` or `saturday`.

```mermaid-example
gantt
title A Gantt Diagram Excluding Fri - Sat weekends
dateFormat YYYY-MM-DD
excludes weekends
weekend friday
section Section
A task :a1, 2024-01-01, 30d
Another task :after a1, 20d
```

### Section statements

You can divide the chart into various sections, for example to separate different parts of a project like development and documentation.
Expand Down
Loading