|
Posted by NC on 04/21/06 18:08
Good Man wrote:
>
> Just before I sit down and re-invent the wheel, I was wondering if
> anyone here has coded any Gantt charts (preferably 'week' style as
> opposed to 'month' style) and might have a class to share?
....
> ultimately i'd be looking for html output, as i will need to make
> links out of events, etc.
It should be fairly simple... In PHP 4, you could write:
Class GanttChart {
var $activities;
var $start;
var $end;
var $unit;
function GanttChart ($start = 0, $end = 0, $unit = 'weeks') {
$this->start = $start;
$this->end = $end;
$this->unit = $unit;
$this->activities = array();
}
function AddActivity ($description, $start, $end, $color) {
$this->activities[] = array('start' => $start, 'end' => $end,
'description' => $description, 'color' => $color);
$this->start = min($this->start, $start);
$this->end = max($this->end, $end);
}
function Render ($unit_width = 50) {
$duration = $this->end - $this->start;
echo "<table border=0 cellspacing=0 cellpadding=2>\r\n";
echo "<tr><th colspan=$duration>", strtoupper($this->unit),
"\r\n<tr>\r\n";
for ($i = $this->start; $i <= $this->end; $i++) {
echo "<th width=$unit_width>$i";
}
foreach ($this->activities as $activity) {
$start = $activity['start'];
$end = $activity['end'];
$description = $activity['description'];
$color = $activity['color'];
$before = $start;
$duration = $end - $start + 1;
$after = $this->end - $end;
echo "<tr>\r\n";
if ($before > 0) {
echo "<td colspan=$before> \r\n";
}
echo "<td colspan=$duration align=center
bgcolor=$color>$description\r\n";
if ($after > 0) {
echo "<td colspan=$after> \r\n";
}
}
echo "</table>\r\n";
}
}
Usage example:
$G = new GanttChart();
$G->AddActivity('First Activity', 0, 5, 'red');
$G->AddActivity('Second Activity', 6, 15, 'yellow');
$G->AddActivity('Third Activity', 4, 11, '#40f040');
$G->Render();
HTML has been kept to a minimum for clarity; you can beautify it all
you want...
Cheers,
NC
Navigation:
[Reply to this message]
|