* @license MIT Style License */ class PieChartHelper extends Helper { var $helpers = array('Html'); var $total = 0; var $data = null; var $path = ''; var $eq_pie = null; /** * Create eq_pie class instance * * @param array $data Data for pie chart * @return bool Successful */ function create($data = array()) { if (empty($data)) { return false; } foreach($data as $eq_data) { $this->total += $eq_data[1]; } if ($this->total == 0) { return false; } $this->data = $data; $this->path = WWW_ROOT.'img'.DS.'eq_pie'; if (!is_dir($this->path)) { mkdir($this->path, 0777); if (!is_dir($this->path)) { debug("Error, Unable to create folder at $this->path (check permissions)."); return false; } } vendor('class_eq_pie'); $this->eq_pie = new eq_pie; return true; } /** * Draw a pie chart * * @param int $width Width of pie chart graphic in pixels * @param int $height Height of pie chart graphic in pixels (controls perspective, does not include $shadow) * @param int $shadow How "tall" the pie chart is * @param string $backgroundColor Background color as hexidecimal * @param bool $legend Draw legend too? * @return string HTML image */ function draw($width = 100, $height = 100, $shadow = 10, $backgroundColor = '#ffffff', $legend = 1) { $hash = md5($this->_implode_r(array($this->data, $width, $height, $shadow, $backgroundColor, $legend))); $alt = array(); foreach($this->data as $eq_data) { $alt[] = $eq_data[0].' '.number_format($eq_data[1] / $this->total * 100, 1).'%'; } $alt = join(', ', $alt); $filename = $this->path.DS.$hash.'.png'; if (is_readable($filename)) { list($w, $h) = getimagesize($filename); return $this->Html->image(('eq_pie'.DS.$hash.'.png'), array('alt' => $alt, 'width' => $w, 'height' => $h)); } $this->eq_pie->MakePie($filename, $width, $height, $shadow, $backgroundColor, $this->data, $legend); list($w, $h) = getimagesize($filename); return $this->Html->image(('eq_pie'.DS.$hash.'.png'), array('width' => $w, 'height' => $h)); } /** * Create an HTML legend * * @param string $class Name of CSS class to apply to legend div * @return string HTML div and unordered list */ function legend($class = 'legend') { $out = array(); $out[] = "