| | 5 | //by "likavcan at NOSPAN sturak nospan dot sk" at from php.net |
| | 6 | function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color) { |
| | 7 | |
| | 8 | $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2)); |
| | 9 | |
| | 10 | $dx = $x2 + ($x1 - $x2) * $alength / $distance; |
| | 11 | $dy = $y2 + ($y1 - $y2) * $alength / $distance; |
| | 12 | $k = $awidth / $alength; |
| | 13 | |
| | 14 | $x2o = $x2 - $dx; |
| | 15 | $y2o = $dy - $y2; |
| | 16 | |
| | 17 | $x3 = $y2o * $k + $dx; |
| | 18 | $y3 = $x2o * $k + $dy; |
| | 19 | |
| | 20 | $x4 = $dx - $y2o * $k; |
| | 21 | $y4 = $dy - $x2o * $k; |
| | 22 | |
| | 23 | imageline($im, $x1, $y1, $dx, $dy, $color); |
| | 24 | imageline($im, $x3, $y3, $x4, $y4, $color); |
| | 25 | imageline($im, $x3, $y3, $x2, $y2, $color); |
| | 26 | imageline($im, $x2, $y2, $x4, $y4, $color); |
| | 27 | |
| | 28 | } |
| | 29 | |
| | 30 | function calc_tickscale($max_value,$min_ticks,$max_labels) |
| | 31 | { |
| | 32 | //First see how many labels we can get in |
| | 33 | $tickscale=pow(10,floor(log($max_value,10))); |
| | 34 | while($max_value/$tickscale*10<=$max_labels) |
| | 35 | $tickscale/=10; |
| | 36 | //That's as far as we can break up the labels for powers of 10. |
| | 37 | //Maybe we can break it up by 5 or 2 |
| | 38 | $ticks=$max_value/$tickscale; |
| | 39 | $divisions=1; |
| | 40 | if($ticks*5<=$max_labels) |
| | 41 | $divisions=5; |
| | 42 | else if($ticks*2<=$max_labels) |
| | 43 | $divisions=2; |
| | 44 | $tickscale/=$divisions; |
| | 45 | $ticks*=$divisions; |
| | 46 | |
| | 47 | //Label interval is how many ticks to have before a label |
| | 48 | $label_interval=1; |
| | 49 | if($ticks*$divisions<$min_ticks&&$divisions!=1) |
| | 50 | { |
| | 51 | //the tickscale needs to be broken up more for the ticks |
| | 52 | //(but not labels). Gotta undo the last factor then for the ticks. |
| | 53 | $label_interval=10/$divisions; |
| | 54 | $tickscale/=10/$divisions; |
| | 55 | } |
| | 56 | |
| | 57 | //Do we still need to break the ticks up more? |
| | 58 | while($max_value/$tickscale<$min_ticks) |
| | 59 | { |
| | 60 | if($max_value/$tickscale*2>=$min_ticks) |
| | 61 | { |
| | 62 | $label_interval*=2; |
| | 63 | $tickscale/=2; |
| | 64 | break; |
| | 65 | } |
| | 66 | if($max_value/$tickscale*5>=$min_ticks) |
| | 67 | { |
| | 68 | $label_interval*=5; |
| | 69 | $tickscale/=5; |
| | 70 | break; |
| | 71 | } |
| | 72 | $label_interval*=10; |
| | 73 | $tickscale/=10; |
| | 74 | } |
| | 75 | return array($tickscale,$label_interval); |
| | 76 | } |
| | 77 | |
| 27 | | $blue = ImageColorAllocate($image, 0, 123, 255); |
| 28 | | |
| 29 | | |
| 30 | | ImageRectangle($image, 0, 0, 449, 449, $black); |
| 31 | | ImageFilledRectangle($image, 20, 20, 430, 430, $grey); |
| 32 | | ImageRectangle($image, 20, 20, 430, 430, $black); |
| | 121 | $category_colors=array(ImageColorAllocate($image, 0, 0, 255),ImageColorAllocate($image, 255, 0, 0),ImageColorAllocate($image, 0, 255, 0)); |
| | 122 | $selected_color = ImageColorAllocate($image, 255, 255, 255); |
| | 123 | |
| | 124 | $bgcolor = ImageColorAllocate($image, 0xe5, 0xe8, 0xb2); |
| 35 | | |
| 36 | | $font = "vixar.ttf"; |
| 37 | | |
| 38 | | //$valuesX = array(0,10,20,30); |
| 39 | | //$valuesY = array(0,10,20,30); |
| 40 | | //$number_mat =array(1,10,20,30); |
| 41 | | // Hardcoded sizes for the plot, better to be left alone |
| 42 | | |
| 43 | | $width = 380; |
| 44 | | $height = 380; |
| 45 | | |
| 46 | | $x_initial = 20; |
| 47 | | $y_initial = 380; |
| | 127 | $axis_value_font = "helcr.ttf"; |
| | 128 | $font = "CaslonBold.ttf"; |
| | 129 | |
| | 130 | //Figure out extra left border for y axis values |
| | 131 | if($log == 1){ |
| | 132 | } |
| | 133 | else{ |
| | 134 | $ymax=max($valuesY); |
| | 135 | $yscale = ($Iheight-$top_border-$bottom_border-2*$inside_border) / $ymax; |
| | 136 | |
| | 137 | list($ytickscale,$ylabel_interval)=calc_tickscale($ymax,20,25); |
| | 138 | |
| | 139 | $widest_label_width=0; |
| | 140 | for($valueY=0; $valueY<$ymax; $valueY+=$ylabel_interval*$ytickscale) |
| | 141 | { |
| | 142 | $label_width=imagettfbbox($axis_value_label_size,0,$axis_value_font,$valueY); |
| | 143 | $label_width=$label_width[2]; |
| | 144 | if($label_width>$widest_label_width) |
| | 145 | $widest_label_width=$label_width; |
| | 146 | } |
| | 147 | $left_border+=$widest_label_width; |
| | 148 | } |
| | 149 | |
| | 150 | ImageFilledRectangle($image, 0, 0, $Iwidth, $Iheight, $bgcolor); |
| | 151 | //ImageRectangle($image, 0, 0, 449, 449, $black); |
| | 152 | ImageFilledRectangle($image, $left_border, $top_border, $Iwidth-$right_border-1, $Iheight-$bottom_border-1, $grey); |
| | 153 | //ImageRectangle($image, 20, 20, 430, 430, $black); |
| | 154 | arrow($image,$left_border,$Iheight-$bottom_border,$Iwidth-$right_border,$Iheight-$bottom_border,4,4,$black); |
| | 155 | arrow($image,$left_border,$Iheight-$bottom_border,$left_border,0,4,4,$black); |
| 64 | | $width_bar = 0.90 * $width / max($valuesX); |
| 65 | | $height_bar = 0.90 * $height / max($valuesY); |
| | 172 | $xmax=max($valuesX); |
| | 173 | $xscale = ($Iwidth-$left_border-$right_border-2*$inside_border) / $xmax; |
| | 174 | |
| | 175 | //Draw in the scale ticks on the axes |
| | 176 | |
| | 177 | //We'll estimate the widest label value as the width of xmax |
| | 178 | //This isn't entirely correct because a decimal point can occur based |
| | 179 | //on the divisions |
| | 180 | $label_width=imagettfbbox($axis_value_label_size,0,$axis_value_font,floor($xmax)); |
| | 181 | $label_width=$label_width[2]+15; //+15 for padding |
| | 182 | list($xtickscale,$xlabel_interval)=calc_tickscale($xmax,20,($Iwidth-$left_border-$right_border)/$label_width); |
| | 183 | |
| | 184 | for($i=0,$valueX=0; $valueX<$xmax; $i++,$valueX+=$xtickscale) |
| | 185 | { |
| | 186 | $x = $left_border + $inside_border + $xscale * $valueX; |
| | 187 | $y = $Iheight - $bottom_border; |
| | 188 | $label_width=imagettfbbox($axis_value_label_size,0,$axis_value_font,$valueY); |
| | 189 | $label_width=$label_width[2]; |
| | 190 | if($i%$xlabel_interval==0) |
| | 191 | { |
| | 192 | imageline($image, $x, $y-$tick_length/2, $x, $y+$tick_length, $black); |
| | 193 | $label_width=imagettfbbox($axis_value_label_size,0,$axis_value_font,$valueX); |
| | 194 | $label_height=-$label_width[7]; |
| | 195 | $label_width=$label_width[2]; |
| | 196 | imagettftext($image,$axis_value_label_size,0,$x-$label_width/2-1,$Iheight-$bottom_border+$label_height+$tick_length/2+4,$black,$axis_value_font,$valueX); |
| | 197 | } |
| | 198 | else |
| | 199 | imageline($image, $x, $y-.5*$tick_length/2, $x, $y+.5*$tick_length, $black); |
| | 200 | } |
| | 201 | for($i=0,$valueY=0; $valueY<$ymax; $i++,$valueY+=$ytickscale) |
| | 202 | { |
| | 203 | $x = $left_border; |
| | 204 | $y = $Iheight - $bottom_border - $inside_border - $yscale * $valueY; |
| | 205 | if($y<10) |
| | 206 | break; |
| | 207 | if($i%$ylabel_interval==0) |
| | 208 | { |
| | 209 | imageline($image, $x+$tick_length/2, $y, $x-$tick_length, $y, $black); |
| | 210 | $label_width=imagettfbbox($axis_value_label_size,0,$axis_value_font,$valueY); |
| | 211 | $label_width=$label_width[2]; |
| | 212 | imagettftext($image,$axis_value_label_size,0,$left_border-$label_width-$tick_length/2-4,$y+6,$black,$axis_value_font,$valueY); |
| | 213 | } |
| | 214 | else |
| | 215 | imageline($image, $x+.5*$tick_length/2, $y, $x-.5*$tick_length, $y, $black); |
| | 216 | } |
| 92 | | imagettftext($image, 14, 0, $x-25, $y-3, $black, $font, $names[$i]); |
| 93 | | } |
| 94 | | imagettftext($image, 14, 0, .75 * $Isize/2, $Isize * 0.99, $black, $font, $xlabel); |
| 95 | | imagettftext($image, 14, 90, 15 , 1.25*($Isize)/2, $black, $font, $ylabel); |
| | 246 | //imagettftext($image, 14, 0, $x-25, $y-3, $black, $font, $names[$i]); |
| | 247 | } |
| | 248 | $label_width=imagettfbbox(14,0,$font,$xlabel); |
| | 249 | $label_width=$label_width[2]; |
| | 250 | imagettftext($image, 14, 0, $left_border+($Iwidth-$left_border-$right_border)/2-$label_width/2, $Iheight-5, $black, $font, $xlabel); |
| | 251 | |
| | 252 | $label_width=imagettfbbox(14,0,$font,$ylabel); |
| | 253 | $label_width=$label_width[2]; |
| | 254 | imagettftext($image, 14, 90, 17 , ($Iheight-$top_border-$bottom_border)/2+$label_width/2, $black, $font, $ylabel); |