Show
Ignore:
Timestamp:
10/26/2004 05:24:02 PM (8 years ago)
Author:
kstemen
Message:

Added categorization.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/matml/webselector/php/plot.php

    r79 r121  
    33include("dbquery.php.inc"); 
    44 
     5//by "likavcan at NOSPAN sturak nospan dot sk" at from php.net  
     6function 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 
     30function 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 
    578//Request arguments 
    679 
    7 $plottype = $_REQUEST['plot']; 
     80if(isset($_REQUEST['plot'])) 
     81    $plottype = $_REQUEST['plot']; 
     82else 
     83    $plottype = 'EvsYield'; 
    884if(isset($_REQUEST['log'])) 
    985    $log = $_REQUEST['log']; 
    1086else 
    1187    $log = 0; 
    12  
     88if(isset($_REQUEST['selected'])) 
     89    $selected = $_REQUEST['selected']; 
     90else 
     91    $selected = ''; 
     92$categorizations=array(array(""=>0),array("Metals"=>0,"Polymers"=>1,"Ceramics"=>2,"Fibers"=>1)); 
     93if(isset($_REQUEST['categorization'])) 
     94    $categorization=$categorizations[$_REQUEST['categorization']]; 
     95else 
     96    $categorization=$categorizations[0]; 
    1397 
    1498//get the values from the database with these function 
    15 list($valuesX, $valuesY, $names, $xlabel, $ylabel) = db_get_plot_values($plottype, $log); 
     99list($valuesX, $valuesY, $names, $xlabel, $ylabel, $categories) = db_get_plot_values($plottype, $log, $categorization); 
    16100 
    17101$number_mat = count($names); 
    18102 
    19103 
    20 $Isize = 450; 
    21  
    22 $image = ImageCreate($Isize, $Isize); 
     104$Iwidth=600; 
     105$Iheight=450; 
     106 
     107$left_border=35; 
     108$top_border=1; 
     109$right_border=1; 
     110$bottom_border=50; 
     111 
     112$inside_border=5; 
     113$tick_length=4; 
     114$axis_value_label_size=12; 
     115 
     116$image = ImageCreate($Iwidth, $Iheight); 
    23117 
    24118$white  = ImageColorAllocate($image, 255, 255, 255); 
    25119$grey   = ImageColorAllocate($image, 220, 220, 220); 
    26120$black  = ImageColorAllocate($image, 0, 0, 0); 
    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); 
    33125 
    34126// For using a nice font 
    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 
     131if($log == 1){ 
     132} 
     133else{ 
     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 
     150ImageFilledRectangle($image, 0, 0, $Iwidth, $Iheight, $bgcolor); 
     151//ImageRectangle($image, 0, 0, 449, 449, $black); 
     152ImageFilledRectangle($image, $left_border, $top_border, $Iwidth-$right_border-1, $Iheight-$bottom_border-1, $grey); 
     153//ImageRectangle($image, 20, 20, 430, 430, $black); 
     154arrow($image,$left_border,$Iheight-$bottom_border,$Iwidth-$right_border,$Iheight-$bottom_border,4,4,$black); 
     155arrow($image,$left_border,$Iheight-$bottom_border,$left_border,0,4,4,$black); 
    48156 
    49157// Broken Log scale Attempt 
     
    62170} 
    63171else{ 
    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    } 
    66217} 
    67218 
     
    76227  } 
    77228 else{ 
    78     $x = $x_initial + $width_bar * $valuesX[$i]; 
    79     $y = $y_initial - $height_bar * $valuesY[$i]; 
     229    $x = $left_border + $inside_border + $xscale * $valuesX[$i]; 
     230    $y = $Iheight - $bottom_border - $inside_border - $yscale * $valuesY[$i]; 
    80231  } 
    81232   
     
    84235  // echo "X: $valuesX[$i] Y: $valuesY[$i]<br>"; 
    85236 
    86  
     237    if($selected==$names[$i]) 
     238        $color=$selected_color; 
     239    else 
     240        $color=$category_colors[$categories[$i]]; 
    87241  ImageFilledRectangle($image, $x - 2, $y - 2, $x + 2,  
    88                        $y + 2, $blue); 
     242                       $y + 2, $color); 
    89243  ImageRectangle($image, $x - 2, $y - 2, $x + 2,  
    90244                 $y + 2, $black); 
    91245   
    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]; 
     250imagettftext($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]; 
     254imagettftext($image, 14, 90, 17 , ($Iheight-$top_border-$bottom_border)/2+$label_width/2, $black, $font, $ylabel); 
    96255 
    97256Header("Content-Type: image/png");