Search code examples
javascriptdhtmlx

How to get starting plotted date and ending plotted date from DHTMLX Gantt chart


I am using the DHTMLX Gantt chart, Here I need to get the starting plotted date and ending plotted date from the Gantt. For example here start date is 31 Mar 2019 and the end Date is 7 Apr 2019 in the chart, I want this date without taking it from the JSON. Actually, I want to add some more days to the start date and end date that's why I need it. Here is the code below.

HTML

<!DOCTYPE html>
<html>

<head>
    <link rel='stylesheet' type='text/css' href='http://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css'>
    <script src='http://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js'></script>
    <style>
        .gantt_custom_button {
            background-color: rgb(206, 206, 206);
            position: absolute;
            right: -10px;
            top: 5px;
            width: 20px;
            height: 26px;
            border-radius: 0;
        }
    </style>


</head>
<div id='gantt_here' style='width:100%; height:500px;'></div>

<body>
    <script>
        var task1 = {
            'data': [{
                'id': 1,
                'text': 'Project #1',
                'start_date': '01-04-2019',
                'duration': 2,
                'order': 10,
                'progress': 0.4,
                'open': true
            },
            {
                'id': 2,
                'text': 'Task #1',
                'start_date': '02-04-2019',
                'duration': 1,
                'order': 10,
                'progress': 0.6,
                'parent': 1
            },
            {
                'id': 3,
                'text': 'Task #2',
                'start_date': '03-04-2019',
                'duration': 2,
                'order': 20,
                'progress': 0.6,
                'parent': 1
            },
            {
                'id': 4,
                'text': 'Task #3',
                'start_date': '05-04-2019',
                'duration': 1,
                'order': 10,
                'progress': 0.6,
                'parent': 1
            }

            ],
            'links': [{
                'id': 1,
                'source': 1,
                'target': 2,
                'type': '1'
            },
            {
                'id': 2,
                'source': 2,
                'target': 3,
                'type': '0'
            },
            {
                'id': 3,
                'source': 3,
                'target': 4,
                'type': '0'
            },
            {
                'id': 4,
                'source': 2,
                'target': 5,
                'type': '2'
            }
            ]
        };
        //console.log(task1.data)
        task1.data.forEach(function (val, index) {
            console.log(val.start_date);
            //gantt.config.start_date = gantt.date.add(new Date(val.start_date), -2, 'month');
            //  gantt.config.end_date = gantt.date.add(new Date(val.start_date), 2, 'month');
        })
        //  gantt.config.start_date = gantt.date.add(new Date(2019, 02, 31), -1, 'month');
        //  gantt.config.end_date = gantt.date.add(new Date(2019, 03, 8), 1, 'month');;
        gantt.config['scales'] = [{
            unit: 'day',
            step: 1,
            format: '%d %M'
        },
        { unit: "year", step: 1, format: "%Y" },
        { unit: "month", step: 1, format: "%M" }
        ];
        gantt.config.scale_height = 100;


        gantt.init('gantt_here');
        gantt.parse(task1);
    </script>

</html>

Solution

  • There is an API called getState() for that if you want the starting date and ending date from the Gantt chart.

    gantt.getState().min_date will give you the date that tasks are displayed in the chart from and gantt.getState().max_date will give you the date that tasks are displayed in the chart till. If you want a date before or after the starting date or ending date, you can add/subtract the number of days according to your need by this gantt.date.add(new Date(gantt.getState().max_date), -1, 'day'). Here is the working example:

    <!DOCTYPE html>
    <html>
    
    <head>
        <link rel='stylesheet' type='text/css' href='http://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css'>
        <script src='http://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js'></script>
        <style>
            .gantt_custom_button {
                background-color: rgb(206, 206, 206);
                position: absolute;
                right: -10px;
                top: 5px;
                width: 20px;
                height: 26px;
                border-radius: 0;
            }
        </style>
    
    
    </head>
    <div id='gantt_here' style='width:100%; height:500px;'></div>
    
    <body>
        <script>
            var task1 = {
                'data': [{
                    'id': 1,
                    'text': 'Project #1',
                    'start_date': '01-04-2019',
                    'duration': 2,
                    'order': 10,
                    'progress': 0.4,
                    'open': true
                },
                {
                    'id': 2,
                    'text': 'Task #1',
                    'start_date': '02-04-2019',
                    'duration': 1,
                    'order': 10,
                    'progress': 0.6,
                    'parent': 1
                },
                {
                    'id': 3,
                    'text': 'Task #2',
                    'start_date': '03-04-2019',
                    'duration': 2,
                    'order': 20,
                    'progress': 0.6,
                    'parent': 1
                },
                {
                    'id': 4,
                    'text': 'Task #3',
                    'start_date': '05-04-2019',
                    'duration': 1,
                    'order': 10,
                    'progress': 0.6,
                    'parent': 1
                }
    
                ],
                'links': [{
                    'id': 1,
                    'source': 1,
                    'target': 2,
                    'type': '1'
                },
                {
                    'id': 2,
                    'source': 2,
                    'target': 3,
                    'type': '0'
                },
                {
                    'id': 3,
                    'source': 3,
                    'target': 4,
                    'type': '0'
                },
                {
                    'id': 4,
                    'source': 2,
                    'target': 5,
                    'type': '2'
                }
                ]
            };
            //console.log(task1.data)
            task1.data.forEach(function (val, index) {
                // console.log(val.start_date);
                //gantt.config.start_date = gantt.date.add(new Date(val.start_date), -2, 'month');
                //  gantt.config.end_date = gantt.date.add(new Date(val.start_date), 2, 'month');
            })
            //  gantt.config.start_date = gantt.date.add(new Date(2019, 02, 31), -1, 'month');
            //  gantt.config.end_date = gantt.date.add(new Date(2019, 03, 8), 1, 'month');;
            gantt.config['scales'] = [{
                unit: 'day',
                step: 1,
                format: '%d %M'
            },
            { unit: "year", step: 1, format: "%Y" },
            { unit: "month", step: 1, format: "%M" }
            ];
            gantt.config.scale_height = 100;
    
    
            gantt.init('gantt_here');
            gantt.parse(task1);
    
            console.log(gantt.getState().min_date);
            console.log(gantt.getState().max_date);
    
            console.log("Your custom date");
            console.log(gantt.date.add(new Date(gantt.getState().min_date), 1, 'day'));
            console.log(gantt.date.add(new Date(gantt.getState().max_date), -1, 'day'));
    
        </script>
    
    </html>