데이터 로딩
dhtmlxGantt는 정보를 로드할 때 두 가지 데이터 형식을 지원합니다:
Gantt 차트에 데이터를 채우려면 parse 또는 load 메서드 중 하나를 사용할 수 있습니다.
gantt.init("gantt_here");
gantt.load("tasks.json");
잘못된 데이터를 Gantt에 제공하면 트리 구조 가 순환 구조로 변할 수 있으며, 이는 cyclic reference error를 발생시킬 수 있습니다.
객체에서 데이터 로딩
데이터를 객체에서 직접 로드하려면 parse 메서드를 사용하세요:
Loading from an inline data source
var data = {
tasks:[
{id:1, text:"Project #1", start_date:"01-04-2020", duration:18},
{id:2, text:"Task #1", start_date:"02-04-2020", duration:8, parent:1},
{id:3, text:"Task #2", start_date:"11-04-2020", duration:8, parent:1}
]
};
gantt.init("gantt_here");
gantt.parse(data); /*!*/
데이터 객체에 "start_date"와 "end_date"가 모두 포함되어 있고 날짜 값이 날짜 부분만 포함(예: 01-12-2021, 시간 없음)되어 있다면 추가 설정이 필요할 수 있습니다. 자세한 내용은 Task end date display & Inclusive end dates를 참고하세요.
서버에서 데이터 로딩
클라이언트 측
서버에서 데이터를 가져오려면 load 메서드를 사용할 수 있습니 다:
gantt.html
gantt.init("gantt_here");
gantt.load("data.json"); /*!*/
load 메서드는 지정된 URL로 AJAX 요청을 보내고, 지원되는 형식 중 하나로 된 응답 데이터를 기대합니다. 예를 들어:
data.json
{
"tasks":[
{"id":1, "text":"Project #1", "start_date":"01-04-2020", "duration":18},
{"id":2, "text":"Task #1", "start_date":"02-04-2020","duration":8, "parent":1},
{"id":3, "text":"Task #2", "start_date":"11-04-2020","duration":8, "parent":1}
],
"links":[
{"id":1, "source":1, "target":2, "type":"1"},
{"id":2, "source":2, "target":3, "type":"0"}
]
}
두 번째 인자로 "json", "xml" 또는 "oldxml"과 같이 형식을 지정할 수 있습니다.
gantt.load("data.xml", "xml");
서버 측
서버에서는 데이터가 담긴 정적 파일이나, 데이터 소스에서 정보를 수집해 응답으로 보내는 스크립트가 있을 수 있습니다. 서버 측 설정은 사용하는 프레임워크에 따라 다릅니다.
다양한 플랫폼에서의 자세한 설정 방법과 코드 예제는 Server-Side Integration를 참고하세요.
예를 들어, Node.js를 사용할 경우, Gantt가 AJAX 데이터 요청을 보내는 URL을 처리하는 서버 라우트를 설정할 수 있습니다.
gantt.load("/data");
이 라우트는 다음과 같은 JSON 응답을 생성합니다:
app.get("/data", function(req, res){
db.query("SELECT * FROM gantt_tasks", function(err, rows){
if (err) console.log(err);
db.query("SELECT * FROM gantt_links", function(err, links){
if (err) console.log(err);
for (var i = 0; i < rows.length; i++){
rows[i].start_date = rows[i].start_date.format("YYYY-MM-DD");
rows[i].open = true;
}
res.send({ tasks:rows, links : links });
});
});
});
지원되는 모든 데이터 형식은 지원되는 데이터 형식에서 확인할 수 있습니다.
작업 날짜 로딩
작업 일정 정의
데이터에서 작업의 일정을 지정하는 방법은 세 가지가 있습니다:
- start_date + duration
- start_date + end_date
- duration + end_date
제공되지 않은 속성은 나머지 두 개의 값으로 계산됩니다.
end_date는 duration보다 우선합니다. 세 가지 속성이 모두 있으면 Gantt는 duration을 무시하고 시작일과 종료일로 기간을 계산합니다. 예시:
{
"id":"20", "text":"Project #2",
"start_date":"01-04-2025",
"duration":3,
"end_date":"05-04-2025",
"order":10,"progress":0.4,
"type": "project", "open": true
}
// 위 작업은 실제로 시작일과 종료일로 계산된 기간으로 로드됩니다:
{
"id":"20", "text":"Project #2",
"start_date":"01-04-2025",
"duration":4,
"end_date":"05-04-2025",
"order":10,"progress":0.4,
"type": "project", "open": true
}
ISO 날짜 형식 사용
Gantt는 ISO 날짜 형식을 지원합니다. 이를 활성화하려면 날짜를 파싱하고 포맷하는 함수를 오버라이드해야 합니다:
gantt.templates.parse_date = function(date) {
return new Date(date);
};
gantt.templates.format_date = function(date) {
return date.toISOString();
};
날짜 형식 동적 변경
날짜 형식을 동적으로 변경하려면 parse_date 템플릿을 다음과 같이 업데이트하세요:
var cfg = gantt.config;
var strToDate = gantt.date.str_to_date(cfg.date_format, cfg.server_utc);
gantt.templates.parse_date = function(date){
return strToDate (date);
};
작업 종료일 표시 및 포함 종료일
이 섹션에서는 작업의 종료일을 올바르게 저장하고 표시하는 방법을 설명합니다.
먼저, 작업 날짜를 다룰 때 자주 발생하는 두 가지 시나리오를 살펴보겠습니다: