Merge pull request #6693 from mermaid-js/6649-gantt-chart-dateformat

6649: gantt chart date format issue
This commit is contained in:
Knut Sveidqvist
2025-08-08 10:09:59 +00:00
committed by GitHub
3 changed files with 30 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
'mermaid': patch
---
fix: Apply correct dateFormat in Gantt chart to show only day when specified

View File

@@ -565,6 +565,18 @@ describe('Gantt diagram', () => {
); );
}); });
it('should render only the day when using dateFormat D', () => {
imgSnapshotTest(
`
gantt
title Test
dateFormat D
A :a, 1, 1d
`,
{}
);
});
// TODO: fix it // TODO: fix it
// //
// This test is skipped deliberately // This test is skipped deliberately

View File

@@ -609,9 +609,20 @@ export const draw = function (text, id, version, diagObj) {
* @param h * @param h
*/ */
function makeGrid(theSidePad, theTopPad, w, h) { function makeGrid(theSidePad, theTopPad, w, h) {
const dateFormat = diagObj.db.getDateFormat();
const userAxisFormat = diagObj.db.getAxisFormat();
let axisFormat;
if (userAxisFormat) {
axisFormat = userAxisFormat;
} else if (dateFormat === 'D') {
axisFormat = '%d';
} else {
axisFormat = conf.axisFormat ?? '%Y-%m-%d';
}
let bottomXAxis = axisBottom(timeScale) let bottomXAxis = axisBottom(timeScale)
.tickSize(-h + theTopPad + conf.gridLineStartPadding) .tickSize(-h + theTopPad + conf.gridLineStartPadding)
.tickFormat(timeFormat(diagObj.db.getAxisFormat() || conf.axisFormat || '%Y-%m-%d')); .tickFormat(timeFormat(axisFormat));
const reTickInterval = /^([1-9]\d*)(millisecond|second|minute|hour|day|week|month)$/; const reTickInterval = /^([1-9]\d*)(millisecond|second|minute|hour|day|week|month)$/;
const resultTickInterval = reTickInterval.exec( const resultTickInterval = reTickInterval.exec(
@@ -663,7 +674,7 @@ export const draw = function (text, id, version, diagObj) {
if (diagObj.db.topAxisEnabled() || conf.topAxis) { if (diagObj.db.topAxisEnabled() || conf.topAxis) {
let topXAxis = axisTop(timeScale) let topXAxis = axisTop(timeScale)
.tickSize(-h + theTopPad + conf.gridLineStartPadding) .tickSize(-h + theTopPad + conf.gridLineStartPadding)
.tickFormat(timeFormat(diagObj.db.getAxisFormat() || conf.axisFormat || '%Y-%m-%d')); .tickFormat(timeFormat(axisFormat));
if (resultTickInterval !== null) { if (resultTickInterval !== null) {
const every = resultTickInterval[1]; const every = resultTickInterval[1];