Ignore:
Timestamp:
Apr 13, 2019, 8:05:15 PM (5 years ago)
Author:
roby
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/client/modules/Elezioni/grafici/jpgraph_log.php

    r2 r265  
    11<?php
    22/*=======================================================================
    3 // File:        JPGRAPH_LOG.PHP
    4 // Description: Log scale plot extension for JpGraph
    5 // Created:     2001-01-08
    6 // Ver:         $Id: jpgraph_log.php 957 2007-12-01 14:00:29Z ljp $
    7 //
    8 // Copyright (c) Aditus Consulting. All rights reserved.
    9 //========================================================================
    10 */
     3 // File:        JPGRAPH_LOG.PHP
     4 // Description: Log scale plot extension for JpGraph
     5 // Created:     2001-01-08
     6 // Ver:         $Id: jpgraph_log.php 1106 2009-02-22 20:16:35Z ljp $
     7 //
     8 // Copyright (c) Asial Corporation. All rights reserved.
     9 //========================================================================
     10 */
    1111
    1212DEFINE('LOGLABELS_PLAIN',0);
     
    1818//===================================================
    1919class LogScale extends LinearScale {
    20 //---------------
    21 // CONSTRUCTOR
     20    //---------------
     21    // CONSTRUCTOR
    2222
    2323    // Log scale is specified using the log of min and max
    24     function LogScale($min,$max,$type="y") {
    25         $this->LinearScale($min,$max,$type);
    26         $this->ticks = new LogTicks();
    27         $this->name = 'log';
    28     }
    29 
    30 //----------------
    31 // PUBLIC METHODS       
     24    function __construct($min,$max,$type="y") {
     25        parent::__construct($min,$max,$type);
     26        $this->ticks = new LogTicks();
     27        $this->name = 'log';
     28    }
     29
     30    //----------------
     31    // PUBLIC METHODS
    3232
    3333    // Translate between world and screen
    3434    function Translate($a) {
    35         if( !is_numeric($a) ) {
    36             if( $a != '' && $a != '-' && $a != 'x' )
    37                 JpGraphError::RaiseL(11001);
    38 //('Your data contains non-numeric values.');
    39             return 1;
    40         }
    41         if( $a < 0 ) {
    42             JpGraphError::RaiseL(11002);
    43 //("Negative data values can not be used in a log scale.");
    44             exit(1);
    45         }
    46         if( $a==0 ) $a=1;
    47         $a=log10($a);
    48         return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor);
     35        if( !is_numeric($a) ) {
     36            if( $a != '' && $a != '-' && $a != 'x' ) {
     37                JpGraphError::RaiseL(11001);
     38                // ('Your data contains non-numeric values.');
     39            }
     40            return 1;
     41        }
     42        if( $a < 0 ) {
     43            JpGraphError::RaiseL(11002);
     44            //("Negative data values can not be used in a log scale.");
     45            exit(1);
     46        }
     47        if( $a==0 ) $a=1;
     48        $a=log10($a);
     49        return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor);
    4950    }
    5051
    5152    // Relative translate (don't include offset) usefull when we just want
    52     // to know the relative position (in pixels) on the axis   
     53    // to know the relative position (in pixels) on the axis
    5354    function RelTranslate($a) {
    54         if( !is_numeric($a) ) {
    55             if( $a != '' && $a != '-' && $a != 'x' )
    56                 JpGraphError::RaiseL(11001);
    57 //('Your data contains non-numeric values.');
    58             return 1;
    59         }
    60         if( $a==0 ) $a=1;
    61         $a=log10($a);
    62         return round(($a*1.0 - $this->scale[0]) * $this->scale_factor);
    63     }
    64                
     55        if( !is_numeric($a) ) {
     56            if( $a != '' && $a != '-' && $a != 'x' ) {
     57                JpGraphError::RaiseL(11001);
     58                //('Your data contains non-numeric values.');
     59            }
     60            return 1;
     61        }
     62        if( $a==0 ) {
     63            $a=1;
     64        }
     65        $a=log10($a);
     66        return round(($a*1.0 - $this->scale[0]) * $this->scale_factor);
     67    }
     68
    6569    // Use bcpow() for increased precision
    6670    function GetMinVal() {
    67         if( function_exists("bcpow") )
    68             return round(bcpow(10,$this->scale[0],15),14);
    69         else
    70             return round(pow(10,$this->scale[0]),14);
    71     }
    72        
     71        if( function_exists("bcpow") ) {
     72            return round(bcpow(10,$this->scale[0],15),14);
     73        }
     74        else {
     75            return round(pow(10,$this->scale[0]),14);
     76        }
     77    }
     78
    7379    function GetMaxVal() {
    74         if( function_exists("bcpow") )
    75             return round(bcpow(10,$this->scale[1],15),14);
    76         else
    77             return round(pow(10,$this->scale[1]),14);
    78     }
    79        
     80        if( function_exists("bcpow") ) {
     81            return round(bcpow(10,$this->scale[1],15),14);
     82        }
     83        else {
     84            return round(pow(10,$this->scale[1]),14);
     85        }
     86    }
     87
    8088    // Logarithmic autoscaling is much simplier since we just
    8189    // set the min and max to logs of the min and max values.
     
    8492    // signature as the linear counterpart.
    8593    function AutoScale($img,$min,$max,$maxsteps,$majend=true) {
    86         if( $min==0 ) $min=1;
    87        
    88         if( $max <= 0 ) {
    89             JpGraphError::RaiseL(11004);
    90 //('Scale error for logarithmic scale. You have a problem with your data values. The max value must be greater than 0. It is mathematically impossible to have 0 in a logarithmic scale.');
    91         }
    92         if( is_numeric($this->autoscale_min) ) {
    93             $smin = round($this->autoscale_min);
    94             $smax = ceil(log10($max));
    95             if( $min >= $max ) {
    96                 JpGraphError::RaiseL(25071);//('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
    97             }
    98         }
    99         else {
    100             $smin = floor(log10($min));
    101             if( is_numeric($this->autoscale_max) ) {
    102                 $smax = round($this->autoscale_max);
    103                 if( $smin >= $smax ) {
    104                     JpGraphError::RaiseL(25072);//('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
    105                 }
    106             }
    107             else
    108                 $smax = ceil(log10($max));
    109         }
    110 
    111         $this->Update($img,$smin,$smax);                                       
    112     }
    113 //---------------
    114 // PRIVATE METHODS     
     94        if( $min==0 ) $min=1;
     95
     96        if( $max <= 0 ) {
     97            JpGraphError::RaiseL(11004);
     98            //('Scale error for logarithmic scale. You have a problem with your data values. The max value must be greater than 0. It is mathematically impossible to have 0 in a logarithmic scale.');
     99        }
     100        if( is_numeric($this->autoscale_min) ) {
     101            $smin = round($this->autoscale_min);
     102            $smax = ceil(log10($max));
     103            if( $min >= $max ) {
     104                JpGraphError::RaiseL(25071);//('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
     105            }
     106        }
     107        else {
     108            $smin = floor(log10($min));
     109            if( is_numeric($this->autoscale_max) ) {
     110                $smax = round($this->autoscale_max);
     111                if( $smin >= $smax ) {
     112                    JpGraphError::RaiseL(25072);//('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
     113                }
     114            }
     115            else
     116            $smax = ceil(log10($max));
     117        }
     118
     119        $this->Update($img,$smin,$smax);
     120    }
     121    //---------------
     122    // PRIVATE METHODS
    115123} // Class
    116124
    117125//===================================================
    118126// CLASS LogTicks
    119 // Description: 
     127// Description:
    120128//===================================================
    121129class LogTicks extends Ticks{
    122130    private $label_logtype=LOGLABELS_MAGNITUDE;
    123131    private $ticklabels_pos = array();
    124 //---------------
    125 // CONSTRUCTOR
    126     function LogTicks() {
    127     }
    128 //---------------
    129 // PUBLIC METHODS       
     132    //---------------
     133    // CONSTRUCTOR
     134    function __construct() {
     135    }
     136    //---------------
     137    // PUBLIC METHODS
    130138    function IsSpecified() {
    131         return true;
     139        return true;
    132140    }
    133141
    134142    function SetLabelLogType($aType) {
    135         $this->label_logtype = $aType;
    136     }
    137        
     143        $this->label_logtype = $aType;
     144    }
     145
    138146    // For log scale it's meaningless to speak about a major step
    139147    // We just return -1 to make the framework happy (specifically
    140148    // StrokeLabels() )
    141149    function GetMajor() {
    142         return -1;
     150        return -1;
    143151    }
    144152
    145153    function SetTextLabelStart($aStart) {
    146         JpGraphError::RaiseL(11005);
    147 //('Specifying tick interval for a logarithmic scale is undefined. Remove any calls to SetTextLabelStart() or SetTextTickInterval() on the logarithmic scale.');
     154        JpGraphError::RaiseL(11005);
     155        //('Specifying tick interval for a logarithmic scale is undefined. Remove any calls to SetTextLabelStart() or SetTextTickInterval() on the logarithmic scale.');
    148156    }
    149157
    150158    function SetXLabelOffset($dummy) {
    151         // For log scales we dont care about XLabel offset
     159        // For log scales we dont care about XLabel offset
    152160    }
    153161
     
    157165    // absolute x-position.
    158166    function Stroke($img,$scale,$pos) {
    159         $start = $scale->GetMinVal();
    160         $limit = $scale->GetMaxVal();
    161         $nextMajor = 10*$start;
    162         $step = $nextMajor / 10.0;
    163                
    164                
    165         $img->SetLineWeight($this->weight);                     
    166                
    167         if( $scale->type == "y" ) {
    168             // member direction specified if the ticks should be on
    169             // left or right side.
    170             $a=$pos + $this->direction*$this->GetMinTickAbsSize();
    171             $a2=$pos + $this->direction*$this->GetMajTickAbsSize();     
    172                        
    173             $count=1;
    174             $this->maj_ticks_pos[0]=$scale->Translate($start);
    175             $this->maj_ticklabels_pos[0]=$scale->Translate($start);
    176             if( $this->supress_first )
    177                 $this->maj_ticks_label[0]="";
    178             else {
    179                 if( $this->label_formfunc != '' ) {
    180                     $f = $this->label_formfunc;
    181                     $this->maj_ticks_label[0]=call_user_func($f,$start);       
    182                 }
    183                 elseif( $this->label_logtype == LOGLABELS_PLAIN )
    184                     $this->maj_ticks_label[0]=$start;   
    185                 else
    186                     $this->maj_ticks_label[0]='10^'.round(log10($start));
    187             }
    188             $i=1;
    189             for($y=$start; $y<=$limit; $y+=$step,++$count  ) {
    190                 $ys=$scale->Translate($y);     
    191                 $this->ticks_pos[]=$ys;
    192                 $this->ticklabels_pos[]=$ys;
    193                 if( $count % 10 == 0 ) {
    194                     if( !$this->supress_tickmarks ) {
    195                         if( $this->majcolor!="" ) {
    196                             $img->PushColor($this->majcolor);
    197                             $img->Line($pos,$ys,$a2,$ys);
    198                             $img->PopColor();
    199                         }
    200                         else
    201                             $img->Line($pos,$ys,$a2,$ys);
    202                     }
    203 
    204                     $this->maj_ticks_pos[$i]=$ys;
    205                     $this->maj_ticklabels_pos[$i]=$ys;
    206                    
    207                     if( $this->label_formfunc != '' ) {
    208                         $f = $this->label_formfunc;
    209                         $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);       
    210                     }
    211                     elseif( $this->label_logtype == 0 )
    212                         $this->maj_ticks_label[$i]=$nextMajor; 
    213                     else
    214                         $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
    215                     ++$i;                                               
    216                     $nextMajor *= 10;
    217                     $step *= 10;       
    218                     $count=1;                           
    219                 }
    220                 else {
    221                     if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
    222                         if( $this->mincolor!="" ) $img->PushColor($this->mincolor);
    223                         $img->Line($pos,$ys,$a,$ys);           
    224                         if( $this->mincolor!="" ) $img->PopColor();
    225                     }
    226                 }
    227             }           
    228         }
    229         else {
    230             $a=$pos - $this->direction*$this->GetMinTickAbsSize();
    231             $a2=$pos - $this->direction*$this->GetMajTickAbsSize();     
    232             $count=1;
    233             $this->maj_ticks_pos[0]=$scale->Translate($start);
    234             $this->maj_ticklabels_pos[0]=$scale->Translate($start);
    235             if( $this->supress_first )
    236                 $this->maj_ticks_label[0]="";
    237             else {
    238                 if( $this->label_formfunc != '' ) {
    239                     $f = $this->label_formfunc;
    240                     $this->maj_ticks_label[0]=call_user_func($f,$start);       
    241                 }
    242                 elseif( $this->label_logtype == 0 )
    243                     $this->maj_ticks_label[0]=$start;   
    244                 else
    245                     $this->maj_ticks_label[0]='10^'.round(log10($start));
    246             }
    247             $i=1;                       
    248             for($x=$start; $x<=$limit; $x+=$step,++$count  ) {
    249                 $xs=$scale->Translate($x);     
    250                 $this->ticks_pos[]=$xs;
    251                 $this->ticklabels_pos[]=$xs;
    252                 if( $count % 10 == 0 ) {
    253                     if( !$this->supress_tickmarks ) {
    254                         $img->Line($xs,$pos,$xs,$a2);
    255                     }
    256                     $this->maj_ticks_pos[$i]=$xs;
    257                     $this->maj_ticklabels_pos[$i]=$xs;
    258 
    259                     if( $this->label_formfunc != '' ) {
    260                         $f = $this->label_formfunc;
    261                         $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);       
    262                     }
    263                     elseif( $this->label_logtype == 0 )
    264                         $this->maj_ticks_label[$i]=$nextMajor; 
    265                     else
    266                         $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
    267                     ++$i;
    268                     $nextMajor *= 10;
    269                     $step *= 10;       
    270                     $count=1;                           
    271                 }
    272                 else {
    273                     if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
    274                         $img->Line($xs,$pos,$xs,$a);           
    275                     }
    276                 }
    277             }           
    278         }
    279         return true;
     167        $start = $scale->GetMinVal();
     168        $limit = $scale->GetMaxVal();
     169        $nextMajor = 10*$start;
     170        $step = $nextMajor / 10.0;
     171
     172
     173        $img->SetLineWeight($this->weight);
     174
     175        if( $scale->type == "y" ) {
     176            // member direction specified if the ticks should be on
     177            // left or right side.
     178            $a=$pos + $this->direction*$this->GetMinTickAbsSize();
     179            $a2=$pos + $this->direction*$this->GetMajTickAbsSize();
     180             
     181            $count=1;
     182            $this->maj_ticks_pos[0]=$scale->Translate($start);
     183            $this->maj_ticklabels_pos[0]=$scale->Translate($start);
     184            if( $this->supress_first )
     185            $this->maj_ticks_label[0]="";
     186            else {
     187                if( $this->label_formfunc != '' ) {
     188                    $f = $this->label_formfunc;
     189                    $this->maj_ticks_label[0]=call_user_func($f,$start);
     190                }
     191                elseif( $this->label_logtype == LOGLABELS_PLAIN ) {
     192                    $this->maj_ticks_label[0]=$start;
     193                }
     194                else {
     195                    $this->maj_ticks_label[0]='10^'.round(log10($start));
     196                }
     197            }
     198            $i=1;
     199            for($y=$start; $y<=$limit; $y+=$step,++$count  ) {
     200                $ys=$scale->Translate($y);
     201                $this->ticks_pos[]=$ys;
     202                $this->ticklabels_pos[]=$ys;
     203                if( $count % 10 == 0 ) {
     204                    if( !$this->supress_tickmarks ) {
     205                        if( $this->majcolor!="" ) {
     206                            $img->PushColor($this->majcolor);
     207                            $img->Line($pos,$ys,$a2,$ys);
     208                            $img->PopColor();
     209                        }
     210                        else {
     211                            $img->Line($pos,$ys,$a2,$ys);
     212                        }
     213                    }
     214
     215                    $this->maj_ticks_pos[$i]=$ys;
     216                    $this->maj_ticklabels_pos[$i]=$ys;
     217
     218                    if( $this->label_formfunc != '' ) {
     219                        $f = $this->label_formfunc;
     220                        $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
     221                    }
     222                    elseif( $this->label_logtype == 0 ) {
     223                        $this->maj_ticks_label[$i]=$nextMajor;
     224                    }
     225                    else {
     226                        $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
     227                    }
     228                    ++$i;
     229                    $nextMajor *= 10;
     230                    $step *= 10;
     231                    $count=1;
     232                }
     233                else {
     234                    if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
     235                        if( $this->mincolor!="" ) {
     236                            $img->PushColor($this->mincolor);
     237                        }
     238                        $img->Line($pos,$ys,$a,$ys);
     239                        if( $this->mincolor!="" ) {
     240                            $img->PopColor();
     241                        }
     242                    }
     243                }
     244            }
     245        }
     246        else {
     247            $a=$pos - $this->direction*$this->GetMinTickAbsSize();
     248            $a2=$pos - $this->direction*$this->GetMajTickAbsSize();
     249            $count=1;
     250            $this->maj_ticks_pos[0]=$scale->Translate($start);
     251            $this->maj_ticklabels_pos[0]=$scale->Translate($start);
     252            if( $this->supress_first ) {
     253                $this->maj_ticks_label[0]="";
     254            }
     255            else {
     256                if( $this->label_formfunc != '' ) {
     257                    $f = $this->label_formfunc;
     258                    $this->maj_ticks_label[0]=call_user_func($f,$start);
     259                }
     260                elseif( $this->label_logtype == 0 ) {
     261                    $this->maj_ticks_label[0]=$start;
     262                }
     263                else {
     264                    $this->maj_ticks_label[0]='10^'.round(log10($start));
     265                }
     266            }
     267            $i=1;
     268            for($x=$start; $x<=$limit; $x+=$step,++$count  ) {
     269                $xs=$scale->Translate($x);
     270                $this->ticks_pos[]=$xs;
     271                $this->ticklabels_pos[]=$xs;
     272                if( $count % 10 == 0 ) {
     273                    if( !$this->supress_tickmarks ) {
     274                        $img->Line($xs,$pos,$xs,$a2);
     275                    }
     276                    $this->maj_ticks_pos[$i]=$xs;
     277                    $this->maj_ticklabels_pos[$i]=$xs;
     278
     279                    if( $this->label_formfunc != '' ) {
     280                        $f = $this->label_formfunc;
     281                        $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
     282                    }
     283                    elseif( $this->label_logtype == 0 ) {
     284                        $this->maj_ticks_label[$i]=$nextMajor;
     285                    }
     286                    else {
     287                        $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
     288                    }
     289                    ++$i;
     290                    $nextMajor *= 10;
     291                    $step *= 10;
     292                    $count=1;
     293                }
     294                else {
     295                    if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
     296                        $img->Line($xs,$pos,$xs,$a);
     297                    }
     298                }
     299            }
     300        }
     301        return true;
    280302    }
    281303} // Class
Note: See TracChangeset for help on using the changeset viewer.