Example #1
0
 function stroke($callback = false)
 {
     $xs = $this->x_size;
     $ys = $this->y_size;
     # Load the font for this chart.
     if ($this->font_type == "type1") {
         $this->font = imagepsloadfont($this->font_name);
     } elseif ($this->font_type == "ttf") {
         $this->font = imagettfloadfont($this->font_name);
     } else {
         $this->font = $this->font_name;
     }
     if ($xs == 0 || $ys == 0) {
         php3_error(E_ERROR, "Invalid X or Y sizes: (%s, %s)", $xs, $ys);
     }
     $im = imagecreate($xs, $ys);
     $this->image = $im;
     $bgcolor = $this->allocate_color($this->background_color);
     imagefilledrectangle($im, 0, 0, $xs, $ys, $bgcolor);
     list($xmin, $xmax) = $this->get_extrema(2);
     list($ymin, $ymax) = $this->get_extrema(1);
     $grace = ($ymax - $ymin) * 0.01;
     $ymin -= $grace;
     $ymax += $grace;
     if (!is_array($this->y_min)) {
         $ymin = $this->y_min;
     }
     if (!is_array($this->y_max)) {
         $ymax = $this->y_max;
     }
     if (!is_array($this->x_min)) {
         $xmin = $this->x_min;
     }
     if (!is_array($this->x_max)) {
         $xmax = $this->x_max;
     }
     if ($ymax == $ymin) {
         $ymax *= 1.01;
         $ymin *= 0.99;
     }
     if ($xmax == $xmin) {
         $xmax++;
     }
     if ($ymax == $ymin) {
         $ymax++;
     }
     $xoff = $this->left_margin;
     $yoff = $this->top_margin;
     $width = $xs - $this->left_margin - $this->right_margin;
     $height = $ys - $this->top_margin - $this->bottom_margin;
     $axes_color = $this->allocate_color($this->axes_color);
     if ($this->grid_under_plot) {
         # Draw the grid and the axes.
         $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, false, $axes_color);
         $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, false, $axes_color);
     }
     # Go through all the plots and stroke them.
     if ($callback != false) {
         $callback($im, $xmin, $xmax, $ymin, $ymax, $xoff, $yoff, $width, $height);
     } else {
         for ($i = 0; $i < sizeof($this->plots); $i++) {
             $plot = $this->plots[$i];
             $plot->stroke($im, $xmin, $xmax, $ymin, $ymax, $xoff, $yoff, $width, $height);
         }
     }
     if (!$this->grid_under_plot) {
         # Draw the grid and the axes.
         $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, false, $axes_color);
         $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, false, $axes_color);
     }
     # The plotting may have plotted outside of the allocated
     # "framed" area (if autoscaling is not in use), so we
     # blank out the surrounding area.
     $margin = $this->allocate_color($this->margin_color);
     imagefilledrectangle($im, 0, 0, $xs, $this->top_margin - 1, $margin);
     imagefilledrectangle($im, $xs - $this->right_margin + 1, $this->top_margin - 1, $xs, $ys, $margin);
     imagefilledrectangle($im, 0, $ys - $this->bottom_margin + 1, $xs, $ys, $margin);
     imagefilledrectangle($im, 0, 0, $this->left_margin - 1, $ys, $margin);
     if (!$this->frame) {
         imageline($im, $this->left_margin, $this->top_margin, $this->left_margin, $ys - $this->bottom_margin + 3, $axes_color);
         imageline($im, $this->left_margin - 3, $ys - $this->bottom_margin, $xs - $this->right_margin, $ys - $this->bottom_margin, $axes_color);
     } else {
         imagerectangle($im, $this->left_margin, $this->top_margin, $xs - $this->right_margin, $ys - $this->bottom_margin, $this->allocate_color($this->border_color));
     }
     # Put the text onto the axes.
     $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, true, $axes_color);
     $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, true, $axes_color);
     $title_color = $this->allocate_color("black");
     # Draw the labels, if any.
     if ($this->y_label) {
         imagestringup($im, $this->font, 5, $ys / 2 + $this->string_pixels($this->y_label) / 2, $this->y_label, $title_color);
     }
     if ($this->x_label) {
         imagestring($im, $this->font, $xs / 2 - $this->string_pixels($this->x_label) / 2, $ys - 20, $this->x_label, $title_color);
     }
     # Draw the boorder.
     if ($this->border_color) {
         imagerectangle($im, 0, 0, $xs - 1, $ys - 1, $this->allocate_color($this->border_color));
     }
     # Draw the title.
     for ($i = 0; $i < sizeof($this->title_text); $i++) {
         if (!strcmp($this->title_where[$i], "center")) {
             $tx = $xs / 2 - $this->string_pixels($this->title_text[$i]) / 2;
         } else {
             $tx = 0;
         }
         if ($this->font_type == "type1") {
             imagepstext($im, $this->title_text[$i], $this->font, 12, $this->allocate_color($this->title_color[$i]), $this->allocate_color("white"), $tx, 15);
         } elseif ($this->font_type == "internal") {
             imagestring($im, $this->font, $tx, 5, $this->title_text[$i], $this->allocate_color($this->title_color[$i]));
         }
     }
     if ($this->cache) {
         $this->put_cache($im);
     }
     $this->headers();
     imagegif($im);
     imagedestroy($im);
     return true;
 }
Example #2
0
 function stroke($callback = false)
 {
     global $chart_use_png, $type1_font_encoding;
     $xs = $this->x_size;
     $ys = $this->y_size;
     // Load the font for this chart.
     if ($this->font_type == "type1") {
         $this->font = imagepsloadfont($this->font_name);
         imagepsencodefont($this->font, $type1_font_encoding);
     } elseif ($this->font_type == "ttf") {
         $this->font = imagettfloadfont($this->font_name);
     } else {
         $this->font = $this->font_name;
     }
     if ($xs == 0 || $ys == 0) {
         php3_error(E_ERROR, "Invalid X or Y sizes: (%s, %s)", $xs, $ys);
     }
     $im = imagecreate($xs, $ys);
     $this->image = $im;
     $bgcolor = $this->allocate_color($this->background_color);
     imagefilledrectangle($im, 0, 0, $xs, $ys, $bgcolor);
     list($xmin, $xmax) = $this->get_extrema(2);
     list($ymin, $ymax) = $this->get_extrema(1);
     $grace = ($ymax - $ymin) * 0.01;
     $ymin -= $grace;
     $ymax += $grace;
     if (!is_array($this->y_min)) {
         $ymin = $this->y_min;
     }
     if (!is_array($this->y_max)) {
         $ymax = $this->y_max;
     }
     if (!is_array($this->x_min)) {
         $xmin = $this->x_min;
     }
     if (!is_array($this->x_max)) {
         $xmax = $this->x_max;
     }
     if ($ymax == $ymin) {
         $ymax *= 1.01;
         $ymin *= 0.99;
     }
     if ($xmax == $xmin) {
         $xmax++;
     }
     if ($ymax == $ymin) {
         $ymax++;
     }
     $xoff = $this->left_margin;
     $yoff = $this->top_margin;
     $width = $xs - $this->left_margin - $this->right_margin;
     $height = $ys - $this->top_margin - $this->bottom_margin;
     $axes_color = $this->allocate_color($this->axes_color);
     if ($this->grid_under_plot) {
         // Draw the grid and the axes.
         $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, false, $axes_color);
         $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, false, $axes_color);
     }
     if (!$this->cleanup_after_plotting) {
         $margin = $this->allocate_color($this->margin_color);
         imagefilledrectangle($im, 0, 0, $xs, $this->top_margin - 1, $margin);
         imagefilledrectangle($im, $xs - $this->right_margin + 1, $this->top_margin - 1, $xs, $ys, $margin);
         imagefilledrectangle($im, 0, $ys - $this->bottom_margin + 1, $xs, $ys, $margin);
         imagefilledrectangle($im, 0, 0, $this->left_margin - 1, $ys, $margin);
     }
     // Go through all the plots and stroke them.
     if ($callback != false) {
         $callback($im, $xmin, $xmax, $ymin, $ymax, $xoff, $yoff, $width, $height);
     } else {
         for ($i = 0; $i < sizeof($this->plots); $i++) {
             $plot = $this->plots[$i];
             $plot->stroke($im, $xmin, $xmax, $ymin, $ymax, $xoff, $yoff, $width, $height, &$this);
         }
     }
     if (!$this->grid_under_plot) {
         // Draw the grid and the axes.
         $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, false, $axes_color);
         $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, false, $axes_color);
     }
     // The plotting may have plotted outside of the allocated
     // "framed" area (if autoscaling is not in use), so we
     // blank out the surrounding area.
     if ($this->cleanup_after_plotting) {
         $margin = $this->allocate_color($this->margin_color);
         imagefilledrectangle($im, 0, 0, $xs, $this->top_margin - 1, $margin);
         imagefilledrectangle($im, $xs - $this->right_margin + 1, $this->top_margin - 1, $xs, $ys, $margin);
         imagefilledrectangle($im, 0, $ys - $this->bottom_margin + 1, $xs, $ys, $margin);
         imagefilledrectangle($im, 0, 0, $this->left_margin - 1, $ys, $margin);
     }
     if (!$this->frame) {
         imageline($im, $this->left_margin, $this->top_margin, $this->left_margin, $ys - $this->bottom_margin + 3, $axes_color);
         imageline($im, $this->left_margin - 3, $ys - $this->bottom_margin, $xs - $this->right_margin, $ys - $this->bottom_margin, $axes_color);
     } else {
         imagerectangle($im, $this->left_margin, $this->top_margin, $xs - $this->right_margin, $ys - $this->bottom_margin, $this->allocate_color($this->border_color));
     }
     // Put the text onto the axes.
     $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, true, $axes_color);
     $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, true, $axes_color);
     $title_color = $this->allocate_color("black");
     // Draw the labels, if any.
     if ($this->y_label) {
         if ($this->font_type == "type1") {
             imagepstext($im, $this->y_label, $this->font, $this->font_size, $this->allocate_color($title_color), $this->allocate_color("white"), 15, (int) ($ys / 2 + $this->string_pixels($this->y_label) / 2), 0, 0, 90, 16);
         } else {
             imagestringup($im, $this->font, 5, $ys / 2 + $this->string_pixels($this->y_label) / 2, $this->y_label, $title_color);
         }
     }
     if ($this->x_label) {
         imagestring($im, $this->font, $xs / 2 - $this->string_pixels($this->x_label) / 2, $ys - 20, $this->x_label, $title_color);
     }
     // Draw the boorder.
     if ($this->border_color) {
         imagerectangle($im, 0, 0, $xs - 1, $ys - 1, $this->allocate_color($this->border_color));
     }
     // Draw the title.
     $tx = "noval";
     for ($i = 0; $i < sizeof($this->title_text); $i++) {
         if ($this->font_type == "type1") {
             if ($tx == "noval") {
                 if (!strcmp($this->title_where[$i], "center")) {
                     list($llx, $lly, $urx, $ury) = imagepsbbox($this->title_text[$i], $this->font, $this->font_size);
                     $tx = $xs / 2 - ($urx - $llx) / 2;
                 } else {
                     $tx = 0;
                 }
             }
             imagepstext($im, $this->title_text[$i], $this->font, $this->font_size, $this->allocate_color($this->title_color[$i]), $this->allocate_color("white"), (int) $tx, 15, 0, 0, 0, 16);
         } elseif ($this->font_type == "internal") {
             if (!strcmp($this->title_where[$i], "center")) {
                 $tx = $xs / 2 - $this->string_pixels($this->title_text[$i]) / 2;
             } else {
                 $tx = 0;
             }
             imagestring($im, $this->font, $tx, 5, $this->title_text[$i], $this->allocate_color($this->title_color[$i]));
         }
     }
     // Draw the legend.
     if (sizeof($this->legends) != 0) {
         $maxlength = 0;
         foreach ($this->legends as $legend) {
             $length = $this->real_string_pixels($legend[0]);
             if ($length > $maxlength) {
                 $maxlength = $length;
             }
         }
         if ($this->legend_placement == "r") {
             $x = (int) ($this->x_size - $this->right_margin - $maxlength - 20);
             $y = (int) ($this->top_margin + 20);
         } else {
             $x = (int) ($this->right_margin + 40);
             $y = (int) ($this->top_margin + 20);
         }
         $lmargin = $this->legend_margin;
         // Draw a box behind the legend.
         if ($this->legend_background_color) {
             imagefilledrectangle($im, $x - $lmargin, $y - $lmargin, $x + $lmargin + $maxlength, $y + $lmargin + ($this->font_size + 2) * sizeof($this->legends), $this->allocate_color($this->legend_background_color));
         }
         if ($this->legend_border_color) {
             imagerectangle($im, $x - $lmargin, $y - $lmargin, $x + $lmargin + $maxlength, $y + $lmargin + ($this->font_size + 2) * sizeof($this->legends), $this->allocate_color($this->legend_border_color));
         }
         foreach ($this->legends as $legend) {
             $this->draw_text($legend[0], $legend[1], $x, $y);
             $y += $this->font_size + 2;
         }
     }
     // Rescale the image before outputting, if requested.
     if ($this->output_x_size) {
         global $gd2;
         $owidth = $this->output_x_size;
         $oheight = $this->output_x_size;
         $om = imagecreate($owidth, $oheight);
         if ($gd2) {
             imagecopyresampled($om, $im, 0, 0, 0, 0, $owidth, $oheight, $xs, $ys);
         } else {
             imagecopyresized($om, $im, 0, 0, 0, 0, $owidth, $oheight, $xs, $ys);
         }
         $im = $om;
     }
     // This statement usually doesn't return.
     if ($this->cache) {
         $this->put_cache($im);
     }
     $this->headers();
     if ($chart_use_png) {
         imagepng($im);
     } else {
         imagegif($im);
     }
     imagedestroy($im);
     return true;
 }