📄 phplot.php
字号:
}
function FindDataLimits() {
//Text-Data is different than data-data graphs. For them what
// we have, instead of X values, is # of records equally spaced on data.
//text-data is passed in as $data[] = (title,y1,y2,y3,y4,...)
//data-data is passed in as $data[] = (title,x,y1,y2,y3,y4,...)
$this->number_x_points = count($this->data_values);
switch ($this->data_type) {
case "text-data":
$minx = 0; //valid for BAR TYPE GRAPHS ONLY
$maxx = $this->number_x_points - 1 ; //valid for BAR TYPE GRAPHS ONLY
$miny = (double) $this->data_values[0][1];
$maxy = $miny;
if ($this->draw_x_data_labels == "") {
$this->draw_x_data_labels = 1; //labels_note1: prevent both data labels and x-axis labels being both drawn and overlapping
}
break;
default: //Everything else: data-data, etc.
$maxx = $this->data_values[0][1];
$minx = $maxx;
$miny = $this->data_values[0][2];
$maxy = $miny;
$maxy = $miny;
break;
}
$max_records_per_group = 0;
$total_records = 0;
$mine = 0; //Maximum value for the -error bar (assume error bars always > 0)
$maxe = 0; //Maximum value for the +error bar (assume error bars always > 0)
reset($this->data_values);
while (list($dat_key, $dat) = each($this->data_values)) { //for each X barchart setting
//foreach($this->data_values as $dat) //can use foreach only in php4
$tmp = 0;
$total_records += count($dat) - 1; // -1 for label
switch ($this->data_type) {
case "text-data":
//Find the relative Max and Min
while (list($key, $val) = each($dat)) {
if ($key != 0) { //$dat[0] = label
SetType($val,"double");
if ($val > $maxy) {
$maxy = $val ;
}
if ($val < $miny) {
$miny = (double) $val ;
}
}
$tmp++;
}
break;
case "data-data": //X-Y data is passed in as $data[] = (title,x,y,y2,y3,...) which you can use for multi-dimentional plots.
while (list($key, $val) = each($dat)) {
if ($key == 1) { //$dat[0] = label
SetType($val,"double");
if ($val > $maxx) {
$maxx = $val;
} elseif ($val < $minx) {
$minx = $val;
}
} elseif ($key > 1) {
SetType($val,"double");
if ($val > $maxy) {
$maxy = $val ;
} elseif ($val < $miny) {
$miny = $val ;
}
}
$tmp++;
}
$tmp = $tmp - 1; //# records per group
break;
case "data-data-error": //Assume 2-D for now, can go higher
//Regular X-Y data is passed in as $data[] = (title,x,y,error+,error-,y2,error2+,error2-)
while (list($key, $val) = each($dat)) {
if ($key == 1) { //$dat[0] = label
SetType($val,'double');
if ($val > $maxx) {
$maxx = $val;
} elseif ($val < $minx) {
$minx = $val;
}
} elseif ($key%3 == 2) {
SetType($val,'double');
if ($val > $maxy) {
$maxy = $val ;
} elseif ($val < $miny) {
$miny = $val ;
}
} elseif ($key%3 == 0) {
SetType($val,'double');
if ($val > $maxe) {
$maxe = $val ;
}
} elseif ($key%3 == 1) {
SetType($val,'double');
if ($val > $mine) {
$mine = $val ;
}
}
$tmp++;
}
$maxy = $maxy + $maxe;
$miny = $miny - $mine; //assume error bars are always > 0
break;
default:
$this->PrintError('ERROR: unknown chart type');
break;
}
if ($tmp > $max_records_per_group) {
$max_records_per_group = $tmp;
}
}
$this->min_x = $minx;
$this->max_x = $maxx;
$this->min_y = $miny;
$this->max_y = $maxy;
if ($max_records_per_group > 1) {
$this->records_per_group = $max_records_per_group - 1;
} else {
$this->records_per_group = 1;
}
//$this->data_count = $total_records ;
} // function FindDataLimits
function SetMargins() {
/////////////////////////////////////////////////////////////////
// When the image is first created - set the margins
// to be the standard viewport.
// The standard viewport is the full area of the view surface (or panel),
// less a margin of 4 character heights all round for labelling.
// It thus depends on the current character size, set by SetCharacterHeight().
/////////////////////////////////////////////////////////////////
$str = split("\n",$this->title_txt);
$nbLines = count($str);
if ($this->use_ttf == 1) {
$title_size = $this->TTFBBoxSize($this->title_ttffont_size, $this->title_angle, $this->title_ttffont, 'X'); //An array
if ($nbLines == 1) {
$this->y_top_margin = $title_size[1] * 4;
} else {
$this->y_top_margin = $title_size[1] * ($nbLines+3);
}
//ajo working here
//$x_label_size = $this->TTFBBoxSize($this->x_label_ttffont_size, 0, $this->axis_ttffont, $this->x_label_txt);
$this->y_bot_margin = $this->x_label_height ;
$this->x_left_margin = $this->y_label_width * 2 + $this->tick_length;
$this->x_right_margin = 33.0; // distance between right and end of x axis in pixels
} else {
$title_size = array(ImageFontWidth($this->title_font) * strlen($this->title_txt),ImageFontHeight($this->title_font));
//$this->y_top_margin = ($title_size[1] * 4);
if ($nbLines == 1) {
$this->y_top_margin = $title_size[1] * 4;
} else {
$this->y_top_margin = $title_size[1] * ($nbLines+3);
}
if ($this->x_datalabel_angle == 90) {
$this->y_bot_margin = 76.0; // Must be integer
} else {
$this->y_bot_margin = 66.0; // Must be integer
}
$this->x_left_margin = 77.0; // distance between left and start of x axis in pixels
$this->x_right_margin = 33.0; // distance between right and end of x axis in pixels
}
//exit;
$this->x_tot_margin = $this->x_left_margin + $this->x_right_margin;
$this->y_tot_margin = $this->y_top_margin + $this->y_bot_margin;
if ($this->plot_max_x && $this->plot_max_y && $this->plot_area_width ) { //If data has already been analysed then set translation
$this->SetTranslation();
}
}
function SetMarginsPixels($which_lm,$which_rm,$which_tm,$which_bm) {
//Set the plot area using margins in pixels (left, right, top, bottom)
$this->SetNewPlotAreaPixels($which_lm,$which_tm,($this->image_width - $which_rm),($this->image_height - $which_bm));
return true;
}
function SetNewPlotAreaPixels($x1,$y1,$x2,$y2) {
//Like in GD 0,0 is upper left set via pixel Coordinates
$this->plot_area = array($x1,$y1,$x2,$y2);
$this->plot_area_width = $this->plot_area[2] - $this->plot_area[0];
$this->plot_area_height = $this->plot_area[3] - $this->plot_area[1];
$this->y_top_margin = $this->plot_area[1];
if ($this->plot_max_x) {
$this->SetTranslation();
}
return true;
}
function SetPlotAreaPixels($x1,$y1,$x2,$y2) {
//Like in GD 0,0 is upper left
if (!$this->x_tot_margin) {
$this->SetMargins();
}
if ($x2 && $y2) {
$this->plot_area = array($x1,$y1,$x2,$y2);
} else {
$this->plot_area = array($this->x_left_margin, $this->y_top_margin,
$this->image_width - $this->x_right_margin,
$this->image_height - $this->y_bot_margin
);
}
$this->plot_area_width = $this->plot_area[2] - $this->plot_area[0];
$this->plot_area_height = $this->plot_area[3] - $this->plot_area[1];
return true;
}
function SetPlotAreaWorld($xmin,$ymin,$xmax,$ymax) {
if (($xmin == "") && ($xmax == "")) {
//For automatic setting of data we need $this->max_x
if (!$this->max_y) {
$this->FindDataLimits() ;
}
if ($this->data_type == 'text-data') { //labels for text-data is done at data drawing time for speed.
$xmax = $this->max_x + 1 ; //valid for BAR CHART TYPE GRAPHS ONLY
$xmin = 0 ; //valid for BAR CHART TYPE GRAPHS ONLY
} else {
$xmax = $this->max_x * 1.02;
$xmin = $this->min_x;
}
$ymax = ceil($this->max_y * 1.2);
if ($this->min_y < 0) {
$ymin = floor($this->min_y * 1.2);
} else {
$ymin = 0;
}
}
$this->plot_min_x = $xmin;
$this->plot_max_x = $xmax;
if ($ymin == $ymax) {
$ymax += 1;
}
if ($this->yscale_type == "log") {
//extra error checking
if ($ymin <= 0) {
$ymin = 1;
}
if ($ymax <= 0) {
$this->PrintError('Log plots need data greater than 0');
}
}
$this->plot_min_y = $ymin;
$this->plot_max_y = $ymax;
if ($ymax <= $ymin) {
$this->DrawError('Error in Data - max not gt min');
}
//Set the boundaries of the box for plotting in world coord
// if (!$this->x_tot_margin) { //We need to know the margins before we can calculate scale
// $this->SetMargins();
// }
//For this we have to reset the scale
if ($this->plot_area_width) {
$this->SetTranslation();
}
return true;
} //function SetPlotAreaWorld
function PrintError($error_message) {
// prints the error message to stdout and die
echo "<p><b>Fatal error</b>: $error_message<p>";
die;
}
function DrawError($error_message) {
// prints the error message inline into
// the generated image
if (($this->img) == "") { $this->InitImage(); } ;
$ypos = $this->image_height/2;
if ($this->use_ttf == 1) {
ImageRectangle($this->img, 0,0,$this->image_width,$this->image_height,ImageColorAllocate($this->img,255,255,255));
ImageTTFText($this->img, $this->small_ttffont_size, 0, $xpos, $ypos, ImageColorAllocate($this->img,0,0,0), $this->axis_ttffont, $error_message);
} else {
ImageRectangle($this->img, 0,0,$this->image_width,$this->image_height,ImageColorAllocate($this->img,255,255,255));
ImageString($this->img, $this->small_font,1,$ypos,$error_message, ImageColorAllocate($this->img,0,0,0));
}
$this->PrintImage();
return true;
}
function TTFBBoxSize($size, $angle, $font, $string) {
//Assume angle < 90
$arr = ImageTTFBBox($size, 0, $font, $string);
$flat_width = $arr[0] - $arr[2];
$flat_height = abs($arr[3] - $arr[5]);
// for 90deg:
// $height = $arr[5] - $arr[7];
// $width = $arr[2] - $arr[4];
$angle = deg2rad($angle);
$width = ceil(abs($flat_width*cos($angle) + $flat_height*sin($angle))); //Must be integer
$height = ceil(abs($flat_width*sin($angle) + $flat_height*cos($angle))); //Must be integer
return array($width, $height);
}
function SetXLabelHeight() {
if ($this->use_ttf == 1) {
//Space for the X Label
$size = $this->TTFBBoxSize($this->x_label_ttffont_size, 0, $this->axis_ttffont, $this->x_label_txt);
$tmp = $size[1];
//$string = Str_Repeat('w', $this->x_datalabel_maxlength);
$i = 0;
$string = '';
while ($i < $this->x_datalabel_maxlength) {
$string .= 'w';
$i++;
}
//Space for the axis data labels
$size = $this->TTFBBoxSize($this->axis_ttffont_size, $this->x_datalabel_angle, $this->axis_ttffont, $string);
$this->x_label_height = 2*$tmp + $size[1] + 4;
} else {
//For Non-TTF fonts we can have only angles 0 or 90
if ($this->x_datalabel_angle == 90) {
$this->x_label_height = $this->x_datalabel_maxlength * ImageFontWidth($this->small_font) / 1.5;
} else {
$this->x_label_height = 5 * ImageFontHeight($this->small_font);
}
}
$this->SetMargins();
return true;
} //function SetXLabelHeight
function SetYLabelWidth() {
//$ylab = sprintf("%6.1f %s",$i,$si_units[0]); //use for PHP2 compatibility
//the "." is for space. It isn't actually printed
$ylab = number_format($this->max_y, $this->y_precision, ".", ",") . $this->si_units . ".";
if ($this->use_ttf == 1) {
$size = $this->TTFBBoxSize($this->axis_ttffont_size, 0, $this->axis_ttffont, $ylab);
} else {
$size[0] = StrLen($ylab) * $this->small_font_width * .6;
}
$this->y_label_width = $size[0] * 2;
//echo "SYLW: $this->y_label_width<br>";
//exit;
$this->SetMargins();
return true;
}
function SetEqualXCoord() {
//for plots that have equally spaced x variables and multiple bars per x-point.
$space = ($this->plot_area[2] - $this->plot_area[0]) / ($this->number_x_points * 2) * $this->group_frac_width;
$group_width = $space * 2;
$bar_width = $group_width / $this->records_per_group;
//I think that eventually this space variable will be replaced by just graphing x.
$this->data_group_space = $space;
$this->record_bar_width = $bar_width;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -