Hello,
Unfortunately, there is no built-in way to achieve this, which is why you need a custom solution. If you want to display custom HTML elements inside the timeline, you can use the addTaskLayer method to draw custom links.
Here’s an example of how it might be implemented:
- Here we store dependencies separately in an array:
const customDependencies = [
{
id: 1,
from: { task: 1, start_date: '03-01-2020' },
to: { task: 1, end_date: '03-06-2020' },
}
];
- Then we use
gantt.addTaskLayer
to render custom dependencies. Each dependency is drawn as an SVG path with vertical + horizontal segments, so it looks closer to a regular Gantt link:
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
const d = [
`M ${fromX} ${fromY}`, // start point
`V ${midY}`, // vertical down
`H ${toX}`, // horizontal line
`V ${toY}` // vertical up to the target
].join(' ');
path.setAttribute('d', d);
- To make these links interactive, we add draggable handles (circle elements) at the start and end of the line:
const fromHandle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
fromHandle.setAttribute('cx', fromX);
fromHandle.setAttribute('cy', fromY);
fromHandle.setAttribute('r', 4);
fromHandle.setAttribute('fill', 'var(--dhx-gantt-link-background)');
fromHandle.style.cursor = 'ew-resize';
fromHandle.addEventListener('mousedown', (e) => {
e.stopPropagation();
dragContext = { depId: dep.id, end: 'from' };
});
svg.appendChild(fromHandle);
const toHandle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
toHandle.setAttribute('cx', toX);
toHandle.setAttribute('cy', toY);
toHandle.setAttribute('r', 4);
toHandle.setAttribute('fill', 'var(--dhx-gantt-link-background)');
toHandle.style.cursor = 'ew-resize';
toHandle.addEventListener('mousedown', (e) => {
e.stopPropagation();
dragContext = { depId: dep.id, end: 'to' };
});
svg.appendChild(toHandle);
});
- On mouse move, we recalculate the date from the current cursor position and update the dependency data. Finally, we re-render the Gantt chart so the link redraws at the new position:
document.addEventListener('mousemove', (e) => {
if (!dragContext) return;
const posX = e.clientX - gantt.$task_data.getBoundingClientRect().left;
const date = gantt.dateFromPos(posX);
const dep = customDependencies.find(d => d.id === dragContext.depId);
if (dragContext.end === 'from') {
dep.from.start_date = dateToStr(date);
} else {
dep.to.end_date = dateToStr(date);
}
gantt.render();
});
Please check the full example: DHTMLX Snippet Tool.
This allows you to define dependencies between arbitrary dates inside the timeline, render them visually as custom SVG paths, and update these dependencies by dragging the handles at the start or end of the link.
Best regards,
Valeria Ivashkevich
DHTMLX Support Engineer