date; $thisY = $datum->weightedAverage; $chartData[$thisX] = $thisY; if (!is_null($previousValue)) { $absoluteDeltas[] = abs($thisY - $previousValue); } if ($thisY < $yMin) { $yMin = $thisY; $yMinX = $thisX; } if ($thisY > $yMax) { $yMax = $thisY; $yMaxX = $thisX; } $previousValue = $thisY; } $yRange = $yMax - $yMin; end($chartData); $xMax = key($chartData); reset($chartData); $xMin = key($chartData); $xRange = $xMax - $xMin; $count = count($chartData); $medianDelta = abs(Util::median($absoluteDeltas)); $width = 700; /* We want the height of the median y-delta to be the same as the width of one x-delta, which puts the median slope at 45 degrees. This improves comprehension. http://vis4.net/blog/posts/doing-the-line-charts-right/ */ $aspectRatio = $yRange / $medianDelta / $count; $height = floor($aspectRatio * $width); function labelFormat($float, $sigFigs = 4, $minPlaces = 1) { return number_format($float, max( $minPlaces, $sigFigs + floor(-log($float, 10))) /* this floor(log) thing is the first significant place value */ ); } /* Transform data coords to chart coords */ function transformX($x) { global $xMin, $xRange, $width; return round( ($x - $xMin) / $xRange * $width , 2); } function transformY($y) { global $yMax, $yRange, $height; return round( // SVG has y axis reversed, 0 is at the top ($yMax - $y) / $yRange * $height , 2); } $chartPoints = "M"; foreach ($chartData as $x => $y) { $chartPoints .= transformX($x) . ',' . transformY($y) . ' '; } $numLabels = floor($height / 50); $labelModulation = 2 * 10 ** (1 + floor(-log($yRange / $numLabels, 10))); $labelInterval = round($yRange / $numLabels * $labelModulation) / $labelModulation; $gridLines = "M0,0 ".$width.",0 M0,".$height.",".$width.",".$height." "; $gridText = ''.labelFormat($yMax).'' . ''.labelFormat($yMin).''; for ( $labelY = $yMin + 0.5 * $labelInterval - fmod($yMin + 0.5 * $labelInterval , $labelInterval) + $labelInterval; $labelY < $yMax - 0.5 * $labelInterval; $labelY += $labelInterval ) { $labelHeight = transformY($labelY); $gridLines .= "M0,".$labelHeight." ".$width.",".$labelHeight." "; $gridText .= ''.labelFormat($labelY).''; } // in case some dingbat has PHP short tags enabled echo '<'.'?xml version="1.0" standalone="no"?'.'>'; ?>