/**
  * Process the values
  */
 public function Values($values)
 {
     if (!empty($values)) {
         parent::Values($values);
         $values = array();
         // find min, max, strip out nulls
         $min = $max = NULL;
         foreach ($this->values[0] as $item) {
             if (!is_null($item->value)) {
                 if (is_null($min) || $item->value < $min) {
                     $min = $item->value;
                 }
                 if (is_null($max) || $item->value > $max) {
                     $max = $item->value;
                 }
                 $values[] = $item->value;
             }
         }
         // calculate increment?
         if ($this->increment <= 0) {
             $diff = $max - $min;
             if ($diff <= 0) {
                 $inc = 1;
             } else {
                 $inc = pow(10, floor(log10($diff)));
                 $d1 = $diff / $inc;
                 if (($inc != 1 || !is_integer($diff)) && $d1 < 4) {
                     if ($d1 < 3) {
                         $inc *= 0.2;
                     } else {
                         $inc *= 0.5;
                     }
                 }
             }
             $this->increment = $inc;
         }
         // prefill the map with nulls
         $map = array();
         $start = $this->Interval($min);
         $end = $this->Interval($max, true) + $this->increment / 2;
         Graph::SetNumStringOptions($this->settings['decimal'], $this->settings['thousands']);
         for ($i = $start; $i < $end; $i += $this->increment) {
             $key = (int) $i;
             $map[$key] = null;
         }
         foreach ($values as $val) {
             $k = (int) $this->Interval($val);
             if (!array_key_exists($k, $map)) {
                 $map[$k] = 1;
             } else {
                 $map[$k]++;
             }
         }
         if ($this->percentage) {
             $total = count($values);
             $pmap = array();
             foreach ($map as $k => $v) {
                 $pmap[$k] = 100 * $v / $total;
             }
             $values = $pmap;
         } else {
             $values = $map;
         }
         // turn off structured data
         $this->structure = NULL;
         $this->structured_data = FALSE;
         // set up options to make bar graph class draw the histogram properly
         $this->minimum_units_y = 1;
         $this->subdivision_h = $this->increment;
         // no subdiv below bar size
         $this->grid_division_h = max($this->increment, $this->grid_division_h);
         $amh = $this->axis_min_h;
         if (empty($amh)) {
             $this->axis_min_h = $start;
         }
     }
     parent::Values($values);
 }
Example #2
0
 /**
  * Returns the SVG document
  */
 public function Fetch($header = TRUE, $defer_javascript = TRUE)
 {
     $content = '';
     if ($header) {
         $content .= '<?xml version="1.0"';
         // encoding comes before standalone
         if (strlen($this->encoding) > 0) {
             $content .= " encoding=\"{$this->encoding}\"";
         }
         // '>' is with \n so as not to confuse syntax highlighting
         $content .= " standalone=\"no\"?" . ">\n";
         if ($this->doctype) {
             $content .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
         }
     }
     // set the precision - PHP default is 14 digits!
     Graph::$precision = $this->settings['precision'];
     $old_precision = ini_set('precision', Graph::$precision);
     // set decimal and thousands for NumString
     Graph::SetNumStringOptions($this->settings['decimal'], $this->settings['thousands']);
     // display title and description if available
     $heading = '';
     if ($this->title) {
         $heading .= $this->Element('title', NULL, NULL, $this->title);
     }
     if ($this->description) {
         $heading .= $this->Element('desc', NULL, NULL, $this->description);
     }
     try {
         $this->CheckValues($this->values);
         if ($this->show_tooltips) {
             $this->LoadJavascript();
         }
         // get the body content from the subclass
         $body = $this->DrawGraph();
     } catch (Exception $e) {
         $err = $e->getMessage();
         if ($this->exception_details) {
             $err .= " [" . basename($e->getFile()) . ' #' . $e->getLine() . ']';
         }
         $body = $this->ErrorText($err);
     }
     $svg = array('width' => $this->width, 'height' => $this->height, 'version' => '1.1', 'xmlns:xlink' => 'http://www.w3.org/1999/xlink');
     if ($this->auto_fit) {
         $svg['viewBox'] = "0 0 {$this->width} {$this->height}";
         $svg['width'] = $svg['height'] = '100%';
     }
     if ($this->svg_class) {
         $svg['class'] = $this->svg_class;
     }
     if (!$defer_javascript) {
         $js = $this->FetchJavascript();
         if ($js != '') {
             $heading .= $js;
             $onload = Graph::$javascript->GetOnload();
             if ($onload != '') {
                 $svg['onload'] = $onload;
             }
         }
     }
     // insert any gradients that are used
     foreach ($this->gradients as $key => $gradient) {
         $this->defs[] = $this->MakeLinearGradient($key);
     }
     // and any patterns
     if (!is_null($this->pattern_list)) {
         $this->pattern_list->MakePatterns($this->defs);
     }
     // show defs and body content
     if (count($this->defs)) {
         $heading .= $this->Element('defs', NULL, NULL, implode('', $this->defs));
     }
     if ($this->namespace) {
         $svg['xmlns:svg'] = "http://www.w3.org/2000/svg";
     } else {
         $svg['xmlns'] = "http://www.w3.org/2000/svg";
     }
     // add any extra namespaces
     foreach ($this->namespaces as $ns => $url) {
         $svg['xmlns:' . $ns] = $url;
     }
     // display version string
     if ($this->show_version) {
         $text = array('x' => $this->pad_left, 'y' => $this->height - 3);
         $style = array('font-family' => 'monospace', 'font-size' => '12px', 'font-weight' => 'bold');
         $body .= $this->ContrastText($text['x'], $text['y'], SVGGRAPH_VERSION, 'blue', 'white', $style);
     }
     $content .= $this->Element('svg', $svg, NULL, $heading . $body);
     // replace PHP's precision
     ini_set('precision', $old_precision);
     if ($this->minify) {
         $content = preg_replace('/\\>\\s+\\</', '><', $content);
     }
     return $content;
 }