/** * creates a line chart * @param array $data data input * @param int $width graphic width * @param int $height graphic height * @param array $color lines colors * @param array $style lines style (filled or line) * @param int $zLine a "zero" line * @param array $legende caption */ public function lineChart($data, $width, $height, $color = null, $style = null, $zLine = null, $legende = null) { // error if datatype is wrong if (!is_array($data)) { echo 'wrong datatype for variable $data'; return false; } else { if (!is_numeric($width)) { echo 'wrong datatype for variable $width'; return false; } else { if (!is_numeric($height)) { echo 'wrong datatype for variable $height'; return false; } } } // globalize some optional variables if (isset($zLine)) { $this->zLine = $zLine; } else { unset($this->zLine); } if (isset($legende)) { $this->legende = $legende; } else { unset($this->legende); } if (isset($color)) { $this->lColors = $color; } else { unset($this->lColors); } if (isset($style)) { $this->fills = $style; } else { unset($this->fills); } // globalize some required variables $pad = 8; // standard top and bottom padding $this->width = $width; $this->height = $height; $this->imgh = $height + $pad; // add padding to prevent elements from being ouside the image $this->data = $data; $this->numLines = count($this->data); // number of graphs = number of datasets in $data $this->lenSet = 0; $this->yMax = 0; foreach ($this->data as $set) { if (count($set) > $this->lenSet) { $this->lenSet = count($set) - 1; // find out the number of datapoints in a set } if (max($set) > $this->yMax) { $this->yMax = max($set); // find out the highest value } if (!isset($this->yMin)) { // cant set this one before, since a sets lowest value may be 2 insted of -2 $this->yMin = min($set); // find out the lowest value } else { if ($this->yMin > min($set)) { $this->yMin = min($set); } } } if ($this->yMin < 0) { // if the range of data values goes beyond zero ... $pl = abs($this->yMin); // we have to determine the whole range by this } else { $pl = 0; // otherwise we start by zero (which may not be a good idea ?!) } $h = $this->yMax + $pl; // $h effectively is the datasets range $h = $h === 0 ? 1 : $h; // (modified by zhenglei: to set range to 1 when range is zero) // initiate drawing class parent::Init($this->width, $this->imgh, "lines"); parent::imgFill("white", 1, 1); $dsnum = 0; if ($this->lenSet > 0) { // (modified by zhenglei: if lenSet smaller than 0, just plot a white picture) foreach ($this->data as $set) { // we are on a set level $i = 0; unset($lx, $ly); foreach ($set as $dp) { // we are on single data value level // X of course is the image width divided by set length multiplied by index of current value in set $x = $i * ($this->width / $this->lenSet); $yp = $dp + $pl; // since we tricked in getting the whol set range, we must add the $pl we added above to the Y value $y = $this->height - $yp * ($this->height / $h); // ..and continue analogue to X $y += $pad / 2; // applies padding if (isset($lx)) { // if we are not at the first value in the set we start drawing the line from LX / LY to X / Y parent::imgLine($lx, $ly, $x, $y, $this->lColors[$dsnum]); } self::featPoint($x, $y, $dsnum, $i); // add feature point here if they were given $lx = $x; // saves the current values x and y ... $ly = $y; // since we will need it to draw from there to the next values XY $i++; } if (isset($this->fills)) { $s = $this->fills[$dsnum]; if ($s == "filled") { // fill the space beneath the graph if needed parent::imgFill($this->lColors[$dsnum], 1, $this->height, $this->lColors[$dsnum]); } } $dsnum++; } } // add zero line if given if (isset($this->zLine)) { $y = $this->height - ($this->zLine + $pl) * ($this->height / $h); parent::imgLine(0, $y, $this->width, $y, "black"); } }