Agent-readable demo data
- Title
- JavaScript Gantt Chart | DayPilot Pro for JavaScript Demo
- Tree
- DayPilot JavaScript Demo
- Catalog root
- Default
- Description
- Read more about the Gantt chart client-side image export.
Inline demo script 1
const dp = new DayPilot.Gantt("dp", {
days: 30,
columns: [
{
title: "Name",
width: 50,
property: "text"
},
{
title: "Info",
width: 50,
property: "info"
},
{
title: "Duration",
width: 50
}
],
onBeforeRowHeaderRender: (args) => {
const duration = args.task.duration();
const html = `${duration.toString("d")}d ${duration.toString("h")}h`;
args.row.columns[2].html = html;
}
});
dp.init();
const app = {
elements: {
area: document.getElementById("area"),
downloadButton: document.getElementById("download-button"),
exportButton: document.getElementById("export-button"),
preview: document.getElementById("export"),
zoom: document.getElementById("zoom"),
zoomIn: document.getElementById("zoomin"),
zoomOut: document.getElementById("zoomout")
},
init() {
this.loadTaskData();
this.addEventHandlers();
},
loadTaskData() {
const tasks = [];
const links = [];
// generate and load sample tasks
let start = DayPilot.Date.today();
let last = null;
for (let i = 0; i < 5; i++) {
const duration = Math.floor(Math.random() * 3) + 1; // 1 to 3
const end = start.addDays(duration);
const e = {
start: start,
end: end,
id: DayPilot.guid(),
text: "Task " + i,
tags: {
info: "info text"
},
children: [
{start: start, end: end, id: DayPilot.guid(), text: "Subtask 1", complete: Math.floor(Math.random() * 101)},
{start: start, end: end, id: DayPilot.guid(), text: "Subtask 2", complete: Math.floor(Math.random() * 101)},
{start: end, id: DayPilot.guid(), text: "Milestone 1", type: "Milestone"}
]
};
tasks.push(e);
if (last) {
links.push({
id: DayPilot.guid(), // optional
from: last,
to: e.id,
type: "FinishToStart"
});
}
last = e.id;
start = end;
}
dp.update({
tasks: tasks,
links: links
});
},
addEventHandlers() {
this.elements.exportButton.addEventListener("click", ev => {
ev.preventDefault();
this.showExport();
});
this.elements.downloadButton.addEventListener("click", ev => {
ev.preventDefault();
this.download();
});
this.elements.zoomIn.addEventListener("click", ev => {
ev.preventDefault();
this.zoom(1.1);
});
this.elements.zoomOut.addEventListener("click", ev => {
ev.preventDefault();
this.zoom(1 / 1.1);
});
},
createExport() {
const area = this.elements.area.value;
const options = {area};
if (area === "range") {
const first = dp.tasks.list[0];
const children = first?.children || [];
const last = children[children.length - 1] || first;
options.dateFrom = DayPilot.Date.today();
options.dateTo = options.dateFrom.addDays(10);
options.resourceFrom = first?.id;
options.resourceTo = last?.id;
}
return dp.exportAs("svg", options);
},
showExport() {
this.elements.zoom.style.display = "";
const image = this.createExport();
const element = image.toElement();
element.style.height = `${image.dimensions().height}px`;
element.style.width = `${image.dimensions().width}px`;
this.elements.preview.replaceChildren(element);
},
download() {
this.createExport().download();
},
zoom(factor) {
for (const svg of this.elements.preview.querySelectorAll("svg")) {
svg.style.height = `${svg.clientHeight * factor}px`;
svg.style.width = `${svg.clientWidth * factor}px`;
}
}
};
app.init();