How To Use phpGanttChart

Creating A Gantt Chart Using Our Software

Basic Usage Flowchart

A simple understanding of best practices when creating a Gantt chart is strongly recommended. Our software uses an object based interface for creating and populating your new Gantt chart. All features and objects are accessible through a singular "ganttChart" object. Once initiate, this object provides the means for setting custom configuration values, creating tasks and groups, and returns any additional objects needed to assign links or constraints. Many of the methods called on the "ganttChart" class utilize method linking which allows you to chain multiple method calls one after another.

Basic Code Example
phpGanttChart Example
Fig. 1

Let us break down the above listed flowchart and explain in more detail each step. This example will recreate the Gantt chart shown as Fig. 1. The first step in all Gantt chart creations is to initiate the "ganttChart" class and create your chart object. This object will provide all the functionality you need to complete the chart. You can see that we pass the chart title to the class constructor method.

$objChart = new ganttChart('Basic Example A');

We continue the initiation by setting custom configuration variables. This is done using the setConfig() method of the "ganttChart" object. This method can accept the configuration variables in three different formats: file, array or string. The file and array format allows for bulk setting of the class properties of which we will discuss at a later time. For now, using it's simplest format, the string, we can set individual variables with each method call.

$objChart->setConfig('chartWidth', 300);
$objChart->setConfig('chartHeight', 250);

All subsequently created tasks and milestones must be added to the chart object $objChart. Tasks and milestones, which are extended from tasks, cannot stand alone. Each must become a child of a group. The "ganttChart" object being the parent group of everything. Task groups may be nested within one another, as long as, all tasks eventually make it to the parent $objChart object. We start with the simplest format, adding a simple task to the parent chart object. Creation of a new task is always performed via the $objChart object. We pass the task label, the duration, and an optional start date. The start date is optional. It can be omitted if the initiated task is later assigned a link or constraint from which the software can determine a valid start date.

$objTask_1 = $objChart->newTask('Task 1', 4, new DateTime('12/04/2018'));
$objChart->addTask($objTask_1);

Two more tasks are created but this time the start time is omitted. We understand that we will be assigning links to these tasks that will allow the software to calculate a valid start date for each task. The software will throw a catchable exception if any task is added that cannot determine it's own start date. Each new task added only passes that task label and a task duration. The next step requires the addition of valid links providing a means to a calculated start date.

$objTask_2 = $objChart->newTask('Task 2', 10);
$objTask_3 = $objChart->newTask('Task 3', 6);

Associating a task with a valid link is fairly simple. The addLink() method is called from the ending task and includes as arguments the starting task, the link type and the lead/lag duration. There are four link types available within the software and they are as follows: Start To Start; Start To Finish; Finish To Start; Finish To Start. Each link type describes the relationship between the starting task, listed first, and the ending task, listed last. These types are represented using class constants of the "ganttChart" class.

Each established task is now assigned a link that must reference a previously initiated task object. For this reason, it is highly suggested that all chart tasks are initiated at first within the script. This allows for free reference to all tasks while assigning links and constraints. Along with the associated task, the link creation method is also passed the link type and a lead/lag value. This value will adjust the delay between the start dates of the associated tasks. The example used in Fig. 1 shows two links; one between Task 1 & Task 2 and another between Task 2 & Task 3. These links are created using the code below and each method call is summarized within the comments.

//The start date of Task 2 is linked to the end date of Task 1.
//The start date of Task 2 should begin two (2) days after the end of Task 1.
$objTask_2->addLink($objTask_1, ganttChart::FINISH2START, 2);

//The end date of Task 3 is linked to the end date of Task 2.
//The end date of Task 3 should finishing one (-1) day before the end of Task 2.
$objTask_3->addLink($objTask_2, ganttChart::FINISH2FINISH, -1);

Further restrictions on when a task can take place can be created using task constraints. Task constraints tell the software to follow strict instructions on the start or end dates of a particular task. There are six constraint types available for use: Start No Later Than; Start No Earlier Than; Finish No Later Than; Finish No Earlier Than; Must Start On; Must Finish On. Each type is fairly self explanatory and each is also represented using class constants of the "ganttChart" class.

Our basic example seen in Fig. 1 uses a task constraint on Task 3 and is represented by a green arrow. The green color symbolizes the restriction is set on the task start time and the direction displays that the restriction is "Earlier Than". Task constraints will override any assigned task links. If it is impossible for the software to create a start and end date for a task that follows all it's assigned links and constraints, then the constraints will take precedence. If this happens, any overridden links will be displayed using a dashed line symbolizing that it is no longer being used for task date calculations. The following code snippet shows how we add a constraint to Task 3.

$objTask_3->addConstraint(ganttChart::SNLT, new DateTime('12/16/2018'));

Task grouping allows for related tasks to be displayed together. Each task within a group will be displayed using the same colors and placed under a visual group umbrella. Using the example in Fig. 1, we can see that Task 2 and Task 3 are grouped together under the generic group name "Group". This group is shown using a colored bar that represents that collective start and end time for all tasks within the group. Once a group object is created by passing the method a group name, we need to add each task to the group that belongs to it and then add the group to the parent chart. This is done using the code below.

$objGroup = $objChart->newGroup('Group');
$objGroup->addTask($objTask_2);
$objGroup->addTask($objTask_3);
$objChart->addGroup($objGroup);

The last step with all Gantt chart creations is to draw the chart. Obviously, this method should only be called once all the chart data has been added. The "drawChart" method requires no arguments although a filename may be passed. If the filename does not exist and the directory is writable, the method will pass the chart results to the file as an image.

$objChart->drawChart();
The complete code is shown below.
require_once 'path_to_dir/phpGanttChart/class.ganttChart.php';

try {
	$objChart = new ganttChart('Basic Example A');
	$objChart->setConfig('chartWidth', 300);
	$objChart->setConfig('chartHeight', 250);

	$objTask_1 = $objChart->newTask('Task 1', 4, new DateTime('now - 5 days'));
	$objChart->addTask($objTask_1);

	$objTask_2 = $objChart->newTask('Task 2', 10);
	$objTask_3 = $objChart->newTask('Task 3', 6);

	//The start date of Task 2 is linked to the end date of Task 1.
	//The start date of Task 2 should begin two (2) days after the end of Task 1.
	$objTask_2->addLink($objTask_1, ganttChart::FINISH2START, 2);

	//The end date of Task 3 is linked to the end date of Task 2.
	//The end date of Task 3 should finishing one (-1) day before the end of Task 2.
	$objTask_3->addLink($objTask_2, ganttChart::FINISH2FINISH, -1);

	$objTask_3->addConstraint(ganttChart::SNLT, new DateTime('now + 7 days'));

	$objGroup = $objChart->newGroup('Group');
	$objGroup->addTask($objTask_2);
	$objGroup->addTask($objTask_3);
	$objChart->addGroup($objGroup);

	$objChart->drawChart();

} catch (Exception $e) {
	die($e->getMessage().' File: '.$e->getFile().' (Line: '.$e->getLine().')');
}