phpjpgraph

jpgraph gantt chart with non continuous time values


By reading the documentation I managed do draw a plot like this. enter image description here

The data for this plot is as shown below.

$data = array(
    array(0,"  Label 1", "2001-01-26 04:00","2001-01-26 14:00"),
    array(1,"  Label 2", "2001-01-26 10:00","2001-01-26 18:00"),
    array(2,"  Label 3", "2001-01-26","2001-01-26 10:00"),
    array(3,"  Label 3", "2001-01-26 13:20","2001-01-26 16:00")
);


for($i=0; $i<count($data); ++$i) {
    $bar = new GanttBar($data[$i][0],$data[$i][1],$data[$i][2],$data[$i][3],"",10);
    if( count($data[$i])>4 )
        $bar->title->SetFont($data[$i][4],$data[$i][5],$data[$i][6]);

    $graph1->Add($bar);
}

$graph1->Stroke();

How do I change it to have the plot like this.

enter image description here


Solution

  • I have never used gantt but by looking at the code I can give a guess.
    I save previous label and see if it matches the next one, if it does I use the previous values for number and label.

    $data = array(
        array(0,"  Label 1", "2001-01-26 04:00","2001-01-26 14:00"),
        array(1,"  Label 2", "2001-01-26 10:00","2001-01-26 18:00"),
        array(2,"  Label 3", "2001-01-26","2001-01-26 10:00"),
        array(3,"  Label 3", "2001-01-26 13:20","2001-01-26 16:00")
    );
    $prev ="";
    
    for($i=0; $i<count($data); ++$i) {
        If($prev == $data[$i][1]){
            // Grab previous number and label.
            $bar = new GanttBar($data[$i-1][0],$data[$i-1][1],$data[$i][2],$data[$i][3],"",10);
        }Else{
            $bar = new GanttBar($data[$i][0],$data[$i][1],$data[$i][2],$data[$i][3],"",10);
        }
        if( count($data[$i])>4 )
            $bar->title->SetFont($data[$i][4],$data[$i][5],$data[$i][6]);
    
        $graph1->Add($bar);
        $prev = $data[$i][1]; // Label
    }
    
    $graph1->Stroke();
    

    Give it a try and see if it works.