From 7d7d635923173af2ec34eebeaadd25a21311736e Mon Sep 17 00:00:00 2001 From: Roland Fiala Date: Wed, 9 Jan 2013 20:43:21 +0100 Subject: [PATCH] added milestone lines to gantt chart modified the constructor to accept milestone dates and added a function which draws a line on each milestone date. also removed the whitspaces at the the end of a line which was actually just an accident, but i think it is also useful because of saving bytes :-) --- lib/gantti.php | 120 +++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 43 deletions(-) diff --git a/lib/gantti.php b/lib/gantti.php index 4c13d00..b575b2e 100644 --- a/lib/gantti.php +++ b/lib/gantti.php @@ -1,6 +1,6 @@ false, 'cellwidth' => 40, 'cellheight' => 40, 'today' => true, + 'milestones' => array() ); - - $this->options = array_merge($defaults, $params); + + $this->options = array_merge($defaults, $params); $this->cal = new Calendar(); $this->data = $data; $this->seconds = 60*60*24; $this->cellstyle = 'style="width: ' . $this->options['cellwidth'] . 'px; height: ' . $this->options['cellheight'] . 'px"'; - - // parse data and find first and last date - $this->parse(); - + + // parse data and find first and last date + $this->parse(); + } function parse() { - + foreach($this->data as $d) { - + $this->blocks[] = array( 'label' => $d['label'], 'start' => $start = strtotime($d['start']), 'end' => $end = strtotime($d['end']), 'class' => @$d['class'] ); - + if(!$this->first || $this->first > $start) $this->first = $start; if(!$this->last || $this->last < $end) $this->last = $end; - + } - + $this->first = $this->cal->date($this->first); $this->last = $this->cal->date($this->last); $current = $this->first->month(); $lastDay = $this->last->month()->lastDay()->timestamp; - // build the months + // build the months while($current->lastDay()->timestamp <= $lastDay) { $month = $current->month(); $this->months[] = $month; @@ -67,19 +68,19 @@ function parse() { } $current = $current->next(); } - + } function render() { - + $html = array(); - - // common styles + + // common styles $cellstyle = 'style="line-height: ' . $this->options['cellheight'] . 'px; height: ' . $this->options['cellheight'] . 'px"'; $wrapstyle = 'style="width: ' . $this->options['cellwidth'] . 'px"'; $totalstyle = 'style="width: ' . (count($this->days)*$this->options['cellwidth']) . 'px"'; - // start the diagram - $html[] = '
'; + // start the diagram + $html[] = '
'; // set a title if available if($this->options['title']) { @@ -90,14 +91,14 @@ function render() { $html[] = ''; // data section $html[] = '
'; - + // data header section $html[] = '
'; @@ -105,8 +106,8 @@ function render() { $html[] = '
    '; foreach($this->months as $month) { $html[] = '
  • ' . $month->name() . '
  • '; - } - $html[] = '
'; + } + $html[] = ''; // days headers $html[] = '
    '; @@ -116,19 +117,19 @@ function render() { $today = ($day->isToday()) ? ' today' : ''; $html[] = '
  • ' . $day->padded() . '
  • '; - } - $html[] = '
'; - + } + $html[] = ''; + // end header $html[] = '
'; // main items $html[] = '
    '; - + foreach($this->blocks as $i => $block) { - + $html[] = '
  • '; - + // days $html[] = '
      '; foreach($this->days as $day) { @@ -137,8 +138,8 @@ function render() { $today = ($day->isToday()) ? ' today' : ''; $html[] = '
    • ' . $day . '
    • '; - } - $html[] = '
    '; + } + $html[] = '
'; // the block $days = (($block['end'] - $block['start']) / $this->seconds); @@ -150,34 +151,67 @@ function render() { $class = ($block['class']) ? ' ' . $block['class'] : ''; $html[] = '' . $days . ''; $html[] = ''; - + + } + + $html[] = ''; + + // Add lines for Milestones! + if (!empty($this->options['milestones'])) + { + foreach ($this->options['milestones'] as $milestone_date) + { + $html[] = $this->addMilestone($milestone_date); + } } - - $html[] = ''; - + + if($this->options['today']) { - + // today $today = $this->cal->today(); - $offset = (($today->timestamp - $this->first->month()->timestamp) / $this->seconds); + $offset = (($today->timestamp - $this->first->month()->timestamp) / $this->seconds); $left = round($offset * $this->options['cellwidth']) + round(($this->options['cellwidth'] / 2) - 1); - + if($today->timestamp > $this->first->month()->firstDay()->timestamp && $today->timestamp < $this->last->month()->lastDay()->timestamp) { $html[] = ''; } } - + // end data section - $html[] = '
'; + $html[] = ''; // end diagram $html[] = '
'; return implode('', $html); - + + } + + /** + * adds a milestone as a white line to the chart. + * + * @author rfiala + * @since 2013-01-09 + * + * @param date $milestone_date + * + * @todo add milestone_label for hover effect. + */ + function addMilestone($milestone_date) + { + // get the first day in the gantt chart. + $gantt_start = $this->days[0]->yearINT . "-" . $this->days[0]->monthINT . "-" . $this->days[0]->dayINT; + + // How much days are between the start date and the milestone date? + $offset = (strtotime($milestone_date) - strtotime($gantt_start)) / 60 / 60 / 24; + + $today = $this->cal->today(); + $left = round($offset * $this->options['cellwidth']) + round(($this->options['cellwidth'] / 2) - 1); + return ''; } - + function __toString() { return $this->render(); }