Ignore:
Timestamp:
Apr 14, 2019, 2:31:40 PM (5 years ago)
Author:
roby
Message:
 
File:
1 edited

Legend:

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

    r265 r267  
    11<?php
    22//=======================================================================
    3 // File:        JPGRAPH_LEGEND.INC.PHP
    4 // Description: Class to handle the legend box in the graph that gives
    5 //              names on the data series. The number of rows and columns
    6 //              in the legend are user specifyable.
    7 // Created:     2001-01-08 (Refactored to separate file 2008-08-01)
    8 // Ver:         $Id: jpgraph_legend.inc.php 1926 2010-01-11 16:33:07Z ljp $
     3// File:        JPGRAPH_LEGEND.INC.PHP
     4// Description: Class to handle the legend box in the graph that gives
     5//              names on the data series. The number of rows and columns
     6//              in the legend are user specifyable.
     7// Created:     2001-01-08 (Refactored to separate file 2008-08-01)
     8// Ver:         $Id: jpgraph_legend.inc.php 1048 2008-08-01 19:56:46Z ljp $
    99//
    10 // Copyright (c) Asial Corporation. All rights reserved.
     10// Copyright (c) Aditus Consulting. All rights reserved.
    1111//========================================================================
    1212
    1313DEFINE('_DEFAULT_LPM_SIZE',8); // Default Legend Plot Mark size
    1414
    15 
     15       
    1616//===================================================
    1717// CLASS Legend
     
    2222class Legend {
    2323    public $txtcol=array();
    24     public $font_family=FF_DEFAULT,$font_style=FS_NORMAL,$font_size=8; // old. 12
    25     private $color=array(120,120,120); // Default frame color
    26     private $fill_color=array(245,245,245); // Default fill color
    27     private $shadow=false; // Shadow around legend "box"
    28     private $shadow_color='darkgray';
     24    private $color=array(0,0,0); // Default fram color
     25    private $fill_color=array(235,235,235); // Default fill color
     26    private $shadow=true; // Shadow around legend "box"
     27    private $shadow_color='darkgray@0.5';
    2928    private $mark_abs_hsize=_DEFAULT_LPM_SIZE,$mark_abs_vsize=_DEFAULT_LPM_SIZE;
    30     private $xmargin=10,$ymargin=0,$shadow_width=2;
    31     private $xlmargin=4;
    32     private $ylinespacing=5;
    33    
    34      // We need a separate margin since the baseline of the last text would coincide with the bottom otherwise
    35     private $ybottom_margin = 8;
    36    
     29    private $xmargin=10,$ymargin=3,$shadow_width=2;
     30    private $xlmargin=2, $ylmargin='';
    3731    private $xpos=0.05, $ypos=0.15, $xabspos=-1, $yabspos=-1;
    3832    private $halign="right", $valign="top";
     33    private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
    3934    private $font_color='black';
    4035    private $hide=false,$layout_n=1;
     
    4237    private $csimareas='';
    4338    private $reverse = false ;
    44     private $bkg_gradtype=-1, $bkg_gradfrom='lightgray', $bkg_gradto='gray';
    45 
    46     //---------------
    47     // CONSTRUCTOR
    48     function __construct() {
    49         // Empty
    50     }
    51     //---------------
    52     // PUBLIC METHODS
     39
     40//---------------
     41// CONSTRUCTOR
     42    function Legend() {
     43        // Empty
     44    }
     45//---------------
     46// PUBLIC METHODS       
    5347    function Hide($aHide=true) {
    54         $this->hide=$aHide;
    55     }
    56 
     48        $this->hide=$aHide;
     49    }
     50       
    5751    function SetHColMargin($aXMarg) {
    58         $this->xmargin = $aXMarg;
     52        $this->xmargin = $aXMarg;
    5953    }
    6054
    6155    function SetVColMargin($aSpacing) {
    62         $this->ylinespacing = $aSpacing ;
     56        $this->ymargin = $aSpacing ;
    6357    }
    6458
    6559    function SetLeftMargin($aXMarg) {
    66         $this->xlmargin = $aXMarg;
     60        $this->xlmargin = $aXMarg;
    6761    }
    6862
    6963    // Synonym
    7064    function SetLineSpacing($aSpacing) {
    71         $this->ylinespacing = $aSpacing ;
    72     }
    73 
    74     function SetShadow($aShow='gray',$aWidth=4) {
    75         if( is_string($aShow) ) {
    76             $this->shadow_color = $aShow;
    77             $this->shadow=true;
    78         }
    79         else {
    80             $this->shadow = $aShow;
    81         }
    82         $this->shadow_width = $aWidth;
     65        $this->ymargin = $aSpacing ;
     66    }
     67
     68    function SetShadow($aShow='gray',$aWidth=2) {
     69        if( is_string($aShow) ) {
     70            $this->shadow_color = $aShow;
     71            $this->shadow=true;
     72        }
     73        else
     74            $this->shadow=$aShow;
     75        $this->shadow_width=$aWidth;
    8376    }
    8477
    8578    function SetMarkAbsSize($aSize) {
    86         $this->mark_abs_vsize = $aSize ;
    87         $this->mark_abs_hsize = $aSize ;
     79        $this->mark_abs_vsize = $aSize ;
     80        $this->mark_abs_hsize = $aSize ;
    8881    }
    8982
    9083    function SetMarkAbsVSize($aSize) {
    91         $this->mark_abs_vsize = $aSize ;
     84        $this->mark_abs_vsize = $aSize ;
    9285    }
    9386
    9487    function SetMarkAbsHSize($aSize) {
    95         $this->mark_abs_hsize = $aSize ;
     88        $this->mark_abs_hsize = $aSize ;
    9689    }
    9790
    9891    function SetLineWeight($aWeight) {
    99         $this->weight = $aWeight;
     92        $this->weight = $aWeight;
    10093    }
    10194
    10295    function SetFrameWeight($aWeight) {
    103         $this->frameweight = $aWeight;
    104     }
    105 
     96        $this->frameweight = $aWeight;
     97    }
     98       
    10699    function SetLayout($aDirection=LEGEND_VERT) {
    107         $this->layout_n = $aDirection==LEGEND_VERT ? 1 : 99 ;
    108     }
    109 
     100        $this->layout_n = $aDirection==LEGEND_VERT ? 1 : 99 ;
     101    }
     102       
    110103    function SetColumns($aCols) {
    111         $this->layout_n = $aCols ;
     104        $this->layout_n = $aCols ;
    112105    }
    113106
    114107    function SetReverse($f=true) {
    115         $this->reverse = $f ;
     108        $this->reverse = $f ;
    116109    }
    117110
    118111    // Set color on frame around box
    119112    function SetColor($aFontColor,$aColor='black') {
    120         $this->font_color=$aFontColor;
    121         $this->color=$aColor;
    122     }
    123 
     113        $this->font_color=$aFontColor;
     114        $this->color=$aColor;
     115    }
     116       
    124117    function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
    125         $this->font_family = $aFamily;
    126         $this->font_style = $aStyle;
    127         $this->font_size = $aSize;
    128     }
    129 
    130     function SetPos($aX,$aY,$aHAlign='right',$aVAlign='top') {
    131         $this->Pos($aX,$aY,$aHAlign,$aVAlign);
    132     }
    133 
    134     function SetAbsPos($aX,$aY,$aHAlign='right',$aVAlign='top') {
    135         $this->xabspos=$aX;
    136         $this->yabspos=$aY;
    137         $this->halign=$aHAlign;
    138         $this->valign=$aVAlign;
    139     }
    140 
    141     function Pos($aX,$aY,$aHAlign='right',$aVAlign='top') {
    142         if( !($aX<1 && $aY<1) ) {
    143             JpGraphError::RaiseL(25120);//(" Position for legend must be given as percentage in range 0-1");
    144         }
    145         $this->xpos=$aX;
    146         $this->ypos=$aY;
    147         $this->halign=$aHAlign;
    148         $this->valign=$aVAlign;
     118        $this->font_family = $aFamily;
     119        $this->font_style = $aStyle;
     120        $this->font_size = $aSize;
     121    }
     122       
     123    function SetPos($aX,$aY,$aHAlign="right",$aVAlign="top") {
     124        $this->Pos($aX,$aY,$aHAlign,$aVAlign);
     125    }
     126
     127    function SetAbsPos($aX,$aY,$aHAlign="right",$aVAlign="top") {
     128        $this->xabspos=$aX;
     129        $this->yabspos=$aY;
     130        $this->halign=$aHAlign;
     131        $this->valign=$aVAlign;
     132    }
     133
     134
     135    function Pos($aX,$aY,$aHAlign="right",$aVAlign="top") {
     136        if( !($aX<1 && $aY<1) )
     137            JpGraphError::RaiseL(25120);//(" Position for legend must be given as percentage in range 0-1");
     138        $this->xpos=$aX;
     139        $this->ypos=$aY;
     140        $this->halign=$aHAlign;
     141        $this->valign=$aVAlign;
    149142    }
    150143
    151144    function SetFillColor($aColor) {
    152         $this->fill_color=$aColor;
    153     }
    154 
    155     function Clear() {
    156         $this->txtcol = array();
    157     }
    158 
     145        $this->fill_color=$aColor;
     146    }
     147       
    159148    function Add($aTxt,$aColor,$aPlotmark='',$aLinestyle=0,$csimtarget='',$csimalt='',$csimwintarget='') {
    160         $this->txtcol[]=array($aTxt,$aColor,$aPlotmark,$aLinestyle,$csimtarget,$csimalt,$csimwintarget);
     149        $this->txtcol[]=array($aTxt,$aColor,$aPlotmark,$aLinestyle,$csimtarget,$csimalt,$csimwintarget);
    161150    }
    162151
    163152    function GetCSIMAreas() {
    164         return $this->csimareas;
    165     }
    166 
    167     function SetBackgroundGradient($aFrom='navy',$aTo='silver',$aGradType=2) {
    168         $this->bkg_gradtype=$aGradType;
    169         $this->bkg_gradfrom = $aFrom;
    170         $this->bkg_gradto = $aTo;
    171     }
    172 
    173     function HasItems() {
    174         return (boolean)(count($this->txtcol));
    175     }
    176 
    177     function Stroke($aImg) {
    178         // Constant
    179         $fillBoxFrameWeight=1;
    180 
    181         if( $this->hide ) return;
    182 
    183         $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
    184 
    185         if( $this->reverse ) {
    186             $this->txtcol = array_reverse($this->txtcol);
    187         }
    188 
    189         $n=count($this->txtcol);
    190         if( $n == 0 ) return;
    191 
    192         // Find out the max width and height of each column to be able
     153        return $this->csimareas;
     154    }
     155       
     156    function Stroke(&$aImg) {
     157        // Constant
     158        $fillBoxFrameWeight=1;
     159
     160        if( $this->hide ) return;
     161
     162        $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);         
     163
     164        if( $this->reverse ) {
     165            $this->txtcol = array_reverse($this->txtcol);
     166        }
     167
     168        $n=count($this->txtcol);
     169        if( $n == 0 ) return;
     170
     171        // Find out the max width and height of each column to be able
    193172        // to size the legend box.
    194         $numcolumns = ($n > $this->layout_n ? $this->layout_n : $n);
    195         for( $i=0; $i < $numcolumns; ++$i ) {
    196             $colwidth[$i] = $aImg->GetTextWidth($this->txtcol[$i][0]) +
    197                             2*$this->xmargin + 2*$this->mark_abs_hsize;
    198             $colheight[$i] = 0;
    199 
    200         }
    201 
    202         // Find our maximum height in each row
    203         $rows = 0 ; $rowheight[0] = 0;
    204         for( $i=0; $i < $n; ++$i ) {
    205             $h = max($this->mark_abs_vsize,$aImg->GetTextHeight($this->txtcol[$i][0]))+$this->ylinespacing;
    206 
    207             // Makes sure we always have a minimum of 1/4 (1/2 on each side) of the mark as space
    208             // between two vertical legend entries
    209             //$h = round(max($h,$this->mark_abs_vsize+$this->ymargin));
    210             //echo "Textheight #$i: tetxheight=".$aImg->GetTextHeight($this->txtcol[$i][0]).', ';
    211             //echo "h=$h ({$this->mark_abs_vsize},{$this->ymargin})<br>";
    212             if( $i % $numcolumns == 0 ) {
    213                 $rows++;
    214                 $rowheight[$rows-1] = 0;
    215             }
    216             $rowheight[$rows-1] = max($rowheight[$rows-1],$h)+1;
    217         }
    218 
    219         $abs_height = 0;
    220         for( $i=0; $i < $rows; ++$i ) {
    221             $abs_height += $rowheight[$i] ;
    222         }
    223 
    224         // Make sure that the height is at least as high as mark size + ymargin
    225         $abs_height = max($abs_height,$this->mark_abs_vsize);
    226         $abs_height += $this->ybottom_margin;
    227 
    228         // Find out the maximum width in each column
    229         for( $i=$numcolumns; $i < $n; ++$i ) {
    230             $colwidth[$i % $numcolumns] = max(
    231                 $aImg->GetTextWidth($this->txtcol[$i][0])+2*$this->xmargin+2*$this->mark_abs_hsize,
    232                 $colwidth[$i % $numcolumns]);
    233         }
    234 
    235         // Get the total width
    236         $mtw = 0;
    237         for( $i=0; $i < $numcolumns; ++$i ) {
    238             $mtw += $colwidth[$i] ;
    239         }
    240 
    241         // remove the last rows interpace margin (since there is no next row)
    242         $abs_height -= $this->ylinespacing;
    243 
    244 
    245         // Find out maximum width we need for legend box
    246         $abs_width = $mtw+$this->xlmargin+($numcolumns-1)*$this->mark_abs_hsize;
    247 
    248         if( $this->xabspos === -1  && $this->yabspos === -1 ) {
    249             $this->xabspos = $this->xpos*$aImg->width ;
    250             $this->yabspos = $this->ypos*$aImg->height ;
    251         }
    252 
    253         // Positioning of the legend box
    254         if( $this->halign == 'left' ) {
    255                 $xp = $this->xabspos;
    256         }
    257         elseif( $this->halign == 'center' ) {
    258                 $xp = $this->xabspos - $abs_width/2;
    259         }
    260         else {
    261                 $xp = $aImg->width - $this->xabspos - $abs_width;
    262         }
    263 
    264         $yp=$this->yabspos;
    265         if( $this->valign == 'center' ) {
    266                 $yp-=$abs_height/2;
    267         }
    268         elseif( $this->valign == 'bottom' ) {
    269                 $yp-=$abs_height;
    270         }
    271 
    272         // Stroke legend box
    273         $aImg->SetColor($this->color);
    274         $aImg->SetLineWeight($this->frameweight);
    275         $aImg->SetLineStyle('solid');
    276 
    277         if( $this->shadow ) {
    278                 $aImg->ShadowRectangle($xp,$yp,
    279                                    $xp+$abs_width+$this->shadow_width+2,
    280                                    $yp+$abs_height+$this->shadow_width+2,
    281                                    $this->fill_color,$this->shadow_width+2,$this->shadow_color);
    282         }
    283         else {
    284             $aImg->SetColor($this->fill_color);
    285             $aImg->FilledRectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
    286             $aImg->SetColor($this->color);
    287             $aImg->Rectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
    288         }
    289 
    290         if( $this->bkg_gradtype >= 0 ) {
    291             $grad = new Gradient($aImg);
    292             $grad->FilledRectangle($xp+1, $yp+1,
    293                                    $xp+$abs_width-3, $yp+$abs_height-3,
    294                                    $this->bkg_gradfrom, $this->bkg_gradto,
    295                                    $this->bkg_gradtype);
    296         }
    297 
    298         // x1,y1 is the position for the legend marker + text
    299         // The vertical position is the baseline position for the text
    300         // and every marker is adjusted acording to that.
    301 
    302         // For multiline texts this get more complicated.
    303 
    304         $x1 = $xp + $this->xlmargin;
    305         $y1 = $yp + $rowheight[0] - $this->ylinespacing + 2 ; // The ymargin is included in rowheight
    306 
    307         // Now, y1 is the bottom vertical position of the first legend, i.e if
    308         // the legend has multiple lines it is the bottom line.
    309 
    310         $grad = new Gradient($aImg);
    311         $patternFactory = null;
    312 
    313         // Now stroke each legend in turn
    314         // Each plot has added the following information to  the legend
    315         // p[0] = Legend text
    316         // p[1] = Color,
    317         // p[2] = For markers a reference to the PlotMark object
    318         // p[3] = For lines the line style, for gradient the negative gradient style
    319         // p[4] = CSIM target
    320         // p[5] = CSIM Alt text
    321         $i = 1 ; $row = 0;
    322         foreach($this->txtcol as $p) {
    323 
    324             // STROKE DEBUG BOX
    325             if( _JPG_DEBUG ) {
    326                 $aImg->SetLineWeight(1);
    327                 $aImg->SetColor('red');
    328                 $aImg->SetLineStyle('solid');
    329                 $aImg->Rectangle($x1,$y1,$xp+$abs_width-1,$y1-$rowheight[$row]);
    330             }
    331 
    332             $aImg->SetLineWeight($this->weight);
    333             $x1 = round($x1)+1; // We add one to not collide with the border
    334             $y1=round($y1);
    335 
    336             // This is the center offset up from the baseline which is
    337             // considered the "center" of the marks. This gets slightly complicated since
    338             // we need to consider if the text is a multiline paragraph or if it is only
    339             // a single line. The reason is that for single line the y1 corresponds to the baseline
    340             // and that is fine. However for a multiline paragraph there is no single baseline
    341             // and in that case the y1 corresponds to the lowest y for the bounding box. In that
    342             // case we center the mark in the middle of the paragraph
    343             if( !preg_match('/\n/',$p[0]) ) {
    344                 // Single line
    345                 $marky = ceil($y1-$this->mark_abs_vsize/2)-1;
    346             } else {
    347                 // Paragraph
    348                 $marky = $y1 - $aImg->GetTextHeight($p[0])/2;
    349 
    350               //  echo "y1=$y1, p[o]={$p[0]}, marky=$marky<br>";
    351             }
    352 
    353             //echo "<br>Mark #$i: marky=$marky<br>";
    354 
    355             $x1 += $this->mark_abs_hsize;
    356    
    357             if ( !empty($p[2]) && $p[2]->GetType() > -1 ) {
    358 
    359 
    360                 // Make a plot mark legend. This is constructed with a mark which
    361                 // is run through with a line
    362 
    363                 // First construct a bit of the line that looks exactly like the
    364                 // line in the plot
    365                 $aImg->SetColor($p[1]);
    366                 if( is_string($p[3]) || $p[3]>0 ) {
    367                     $aImg->SetLineStyle($p[3]);
    368                     $aImg->StyleLine($x1-$this->mark_abs_hsize,$marky,$x1+$this->mark_abs_hsize,$marky);
    369                 }
    370 
    371                 // Stroke a mark using image
    372                 if( $p[2]->GetType() == MARK_IMG ) {
    373                     $p[2]->Stroke($aImg,$x1,$marky);
    374                 }
    375 
    376                 // Stroke a mark with the standard size
    377                 // (As long as it is not an image mark )
    378                 if( $p[2]->GetType() != MARK_IMG ) {
    379 
    380                     // Clear any user callbacks since we ont want them called for
    381                     // the legend marks
    382                     $p[2]->iFormatCallback = '';
    383                     $p[2]->iFormatCallback2 = '';
    384 
    385                     // Since size for circles is specified as the radius
    386                     // this means that we must half the size to make the total
    387                     // width behave as the other marks
    388                     if( $p[2]->GetType() == MARK_FILLEDCIRCLE || $p[2]->GetType() == MARK_CIRCLE ) {
    389                         $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize)/2);
    390                         $p[2]->Stroke($aImg,$x1,$marky);
    391                     }
    392                     else {
    393                         $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize));
    394                         $p[2]->Stroke($aImg,$x1,$marky);
    395                     }
    396                 }
    397             }
    398             elseif ( !empty($p[2]) && (is_string($p[3]) || $p[3]>0 ) ) {
    399                 // Draw a styled line
    400                 $aImg->SetColor($p[1]);
    401                 $aImg->SetLineStyle($p[3]);
    402                 $aImg->StyleLine($x1-$this->mark_abs_hsize,$marky,$x1+$this->mark_abs_hsize,$marky);
    403                 $aImg->StyleLine($x1-$this->mark_abs_hsize,$marky+1,$x1+$this->mark_abs_hsize,$marky+1);
    404             }
    405             else {
    406                 // Draw a colored box
    407                 $color = $p[1] ;
    408 
    409                 // We make boxes slightly larger to better show
    410                 $boxsize = max($this->mark_abs_vsize,$this->mark_abs_hsize) + 2 ;
    411 
    412                 $ym = $marky-ceil($boxsize/2) ; // Marker y-coordinate
    413 
    414                 // We either need to plot a gradient or a
    415                 // pattern. To differentiate we use a kludge.
    416                 // Patterns have a p[3] value of < -100
    417                 if( $p[3] < -100 ) {
    418                     // p[1][0] == iPattern, p[1][1] == iPatternColor, p[1][2] == iPatternDensity
    419                     if( $patternFactory == null ) {
    420                         $patternFactory = new RectPatternFactory();
    421                     }
    422                     $prect = $patternFactory->Create($p[1][0],$p[1][1],1);
    423                     $prect->SetBackground($p[1][3]);
    424                     $prect->SetDensity($p[1][2]+1);
    425                     $prect->SetPos(new Rectangle($x1,$ym,$boxsize,$boxsize));
    426                     $prect->Stroke($aImg);
    427                     $prect=null;
    428                 }
    429                 else {
    430                     if( is_array($color) && count($color)==2 ) {
    431                         // The client want a gradient color
    432                         $grad->FilledRectangle($x1-$boxsize/2,$ym,
    433                                                $x1+$boxsize/2,$ym+$boxsize,
    434                                                $color[0],$color[1],-$p[3]);
    435                     }
    436                     else {
    437                         $aImg->SetColor($p[1]);
    438                         $aImg->FilledRectangle($x1-$boxsize/2,$ym, $x1+$boxsize/2,$ym+$boxsize);
    439                     }
    440 
    441                     // Draw a plot frame line
    442                     $aImg->SetColor($this->color);
    443                     $aImg->SetLineWeight($fillBoxFrameWeight);
    444                     $aImg->Rectangle($x1-$boxsize/2,$ym,
    445                                      $x1+$boxsize/2,$ym+$boxsize);
    446                 }
    447             }
    448             $aImg->SetColor($this->font_color);
    449             $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
    450             $aImg->SetTextAlign('left','baseline');
    451 
    452             $debug=false;
    453             $aImg->StrokeText($x1+$this->mark_abs_hsize+$this->xmargin,$y1,$p[0],
    454                 0,'left',$debug);
    455 
    456             // Add CSIM for Legend if defined
    457             if( !empty($p[4]) ) {
    458 
    459                 $xs = $x1 - $this->mark_abs_hsize ;
    460                 $ys = $y1 + 1 ;
    461                 $xe = $x1 + $aImg->GetTextWidth($p[0]) + $this->mark_abs_hsize + $this->xmargin ;
    462                 $ye = $y1-$rowheight[$row]+1;
    463                 $coords = "$xs,$ys,$xe,$y1,$xe,$ye,$xs,$ye";
    464                 if( ! empty($p[4]) ) {
    465                     $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".htmlentities($p[4])."\"";
    466 
    467                     if( !empty($p[6]) ) {
    468                         $this->csimareas .= " target=\"".$p[6]."\"";
    469                     }
    470 
    471                     if( !empty($p[5]) ) {
    472                         $tmp=sprintf($p[5],$p[0]);
    473                         $this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
    474                     }
    475                     $this->csimareas .= " />\n";
    476                 }
    477             }
    478 
    479             if( $i >= $this->layout_n ) {
    480                 $x1 = $xp+$this->xlmargin;
    481                 $row++;
    482                 if( !empty($rowheight[$row]) )
    483                     $y1 += $rowheight[$row];
    484                 $i = 1;
    485             }
    486             else {
    487                 $x1 += $colwidth[($i-1) % $numcolumns] ;
    488                 ++$i;
    489             }
    490         }
     173        $numcolumns = ($n > $this->layout_n ? $this->layout_n : $n);
     174        for( $i=0; $i < $numcolumns; ++$i ) {
     175            $colwidth[$i] = $aImg->GetTextWidth($this->txtcol[$i][0]) +
     176                            2*$this->xmargin + 2*$this->mark_abs_hsize;
     177            $colheight[$i] = 0;
     178        }
     179
     180        // Find our maximum height in each row
     181        $rows = 0 ; $rowheight[0] = 0;
     182        for( $i=0; $i < $n; ++$i ) {
     183            $h = max($this->mark_abs_vsize,$aImg->GetTextHeight($this->txtcol[$i][0]))+$this->ymargin;
     184            if( $i % $numcolumns == 0 ) {
     185                $rows++;
     186                $rowheight[$rows-1] = 0;
     187            }
     188            $rowheight[$rows-1] = max($rowheight[$rows-1],$h);
     189        }
     190
     191        $abs_height = 0;
     192        for( $i=0; $i < $rows; ++$i ) {
     193            $abs_height += $rowheight[$i] ;
     194        }
     195
     196        // Make sure that the height is at least as high as mark size + ymargin
     197        $abs_height = max($abs_height,$this->mark_abs_vsize);
     198
     199        // We add 3 extra pixels height to compensate for the difficult in
     200        // calculating font height
     201        $abs_height += $this->ymargin+3;
     202                                               
     203        // Find out the maximum width in each column
     204        for( $i=$numcolumns; $i < $n; ++$i ) {
     205            $colwidth[$i % $numcolumns] = max(
     206                $aImg->GetTextWidth($this->txtcol[$i][0])+2*$this->xmargin+2*$this->mark_abs_hsize,$colwidth[$i % $numcolumns]);
     207        }
     208
     209        // Get the total width
     210        $mtw = 0;
     211        for( $i=0; $i < $numcolumns; ++$i ) {
     212            $mtw += $colwidth[$i] ;
     213        }
     214
     215        // Find out maximum width we need for legend box
     216        $abs_width = $mtw+$this->xlmargin;
     217
     218        if( $this->xabspos === -1  && $this->yabspos === -1 ) {
     219            $this->xabspos = $this->xpos*$aImg->width ;
     220            $this->yabspos = $this->ypos*$aImg->height ;
     221        }
     222
     223        // Positioning of the legend box
     224        if( $this->halign == 'left' )
     225            $xp = $this->xabspos;
     226        elseif( $this->halign == 'center' )
     227            $xp = $this->xabspos - $abs_width/2;
     228        else 
     229            $xp = $aImg->width - $this->xabspos - $abs_width;
     230
     231        $yp=$this->yabspos;
     232        if( $this->valign == 'center' )
     233            $yp-=$abs_height/2;
     234        elseif( $this->valign == 'bottom' )
     235            $yp-=$abs_height;
     236                       
     237        // Stroke legend box
     238        $aImg->SetColor($this->color); 
     239        $aImg->SetLineWeight($this->frameweight);
     240        $aImg->SetLineStyle('solid');
     241
     242        if( $this->shadow )
     243            $aImg->ShadowRectangle($xp,$yp,$xp+$abs_width+$this->shadow_width,
     244                                   $yp+$abs_height+$this->shadow_width,
     245                                   $this->fill_color,$this->shadow_width,$this->shadow_color);
     246        else {
     247            $aImg->SetColor($this->fill_color);                         
     248            $aImg->FilledRectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
     249            $aImg->SetColor($this->color);                                                     
     250            $aImg->Rectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
     251        }
     252
     253        // x1,y1 is the position for the legend mark
     254        $x1=$xp+$this->mark_abs_hsize+$this->xlmargin;
     255        $y1=$yp + $this->ymargin;               
     256       
     257        $f2 =  round($aImg->GetTextHeight('X')/2);
     258
     259        $grad = new Gradient($aImg);
     260        $patternFactory = null;
     261
     262        // Now stroke each legend in turn
     263        // Each plot has added the following information to  the legend
     264        // p[0] = Legend text
     265        // p[1] = Color,
     266        // p[2] = For markers a reference to the PlotMark object
     267        // p[3] = For lines the line style, for gradient the negative gradient style
     268        // p[4] = CSIM target
     269        // p[5] = CSIM Alt text
     270        $i = 1 ; $row = 0;
     271        foreach($this->txtcol as $p) {
     272         
     273            // STROKE DEBUG BOX
     274            if( _JPG_DEBUG ) {
     275                $aImg->SetLineWeight(1);
     276                $aImg->SetColor('red');
     277                $aImg->SetLineStyle('solid');
     278                $aImg->Rectangle($xp,$y1,$xp+$abs_width,$y1+$rowheight[$row]);
     279            }
     280
     281            $aImg->SetLineWeight($this->weight);
     282            $x1 = round($x1); $y1=round($y1);
     283            if ( !empty($p[2]) && $p[2]->GetType() > -1 ) {
     284                // Make a plot mark legend
     285                $aImg->SetColor($p[1]);
     286                if( is_string($p[3]) || $p[3]>0 ) {
     287                    $aImg->SetLineStyle($p[3]);
     288                    $aImg->StyleLine($x1-$this->mark_abs_hsize,$y1+$f2,$x1+$this->mark_abs_hsize,$y1+$f2);
     289                }
     290                // Stroke a mark with the standard size
     291                // (As long as it is not an image mark )
     292                if( $p[2]->GetType() != MARK_IMG ) {
     293
     294                    // Clear any user callbacks since we ont want them called for
     295                    // the legend marks
     296                    $p[2]->iFormatCallback = '';
     297                    $p[2]->iFormatCallback2 = '';
     298
     299                    // Since size for circles is specified as the radius
     300                    // this means that we must half the size to make the total
     301                    // width behave as the other marks
     302                    if( $p[2]->GetType() == MARK_FILLEDCIRCLE || $p[2]->GetType() == MARK_CIRCLE ) {
     303                        $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize)/2);
     304                        $p[2]->Stroke($aImg,$x1,$y1+$f2);
     305                    }
     306                    else {
     307                        $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize));
     308                        $p[2]->Stroke($aImg,$x1,$y1+$f2);
     309                    }
     310                }
     311            }
     312            elseif ( !empty($p[2]) && (is_string($p[3]) || $p[3]>0 ) ) {
     313                // Draw a styled line
     314                $aImg->SetColor($p[1]);
     315                $aImg->SetLineStyle($p[3]);
     316                $aImg->StyleLine($x1-1,$y1+$f2,$x1+$this->mark_abs_hsize,$y1+$f2);
     317                $aImg->StyleLine($x1-1,$y1+$f2+1,$x1+$this->mark_abs_hsize,$y1+$f2+1);
     318            }
     319            else {
     320                // Draw a colored box
     321                $color = $p[1] ;
     322                // We make boxes slightly larger to better show
     323                $boxsize = min($this->mark_abs_vsize,$this->mark_abs_hsize) + 2 ;
     324                $ym =  round($y1 + $f2 - $boxsize/2);
     325                // We either need to plot a gradient or a
     326                // pattern. To differentiate we use a kludge.
     327                // Patterns have a p[3] value of < -100
     328                if( $p[3] < -100 ) {
     329                    // p[1][0] == iPattern, p[1][1] == iPatternColor, p[1][2] == iPatternDensity
     330                    if( $patternFactory == null ) {
     331                        $patternFactory = new RectPatternFactory();
     332                    }               
     333                    $prect = $patternFactory->Create($p[1][0],$p[1][1],1);
     334                    $prect->SetBackground($p[1][3]);
     335                    $prect->SetDensity($p[1][2]+1);
     336                    $prect->SetPos(new Rectangle($x1,$ym,$boxsize,$boxsize));
     337                    $prect->Stroke($aImg);
     338                    $prect=null;
     339                }
     340                else {
     341                    if( is_array($color) && count($color)==2 ) {
     342                        // The client want a gradient color
     343                        $grad->FilledRectangle($x1,$ym,
     344                                               $x1+$boxsize,$ym+$boxsize,
     345                                               $color[0],$color[1],-$p[3]);
     346                    }
     347                    else {
     348                        $aImg->SetColor($p[1]);
     349                        $aImg->FilledRectangle($x1,$ym,$x1+$boxsize,$ym+$boxsize);
     350                    }
     351                    $aImg->SetColor($this->color);
     352                    $aImg->SetLineWeight($fillBoxFrameWeight);
     353                    $aImg->Rectangle($x1,$ym,$x1+$boxsize,$ym+$boxsize);
     354                }
     355            }
     356            $aImg->SetColor($this->font_color);
     357            $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);             
     358            $aImg->SetTextAlign("left","top");                 
     359            $aImg->StrokeText(round($x1+$this->mark_abs_hsize+$this->xmargin),$y1,$p[0]);
     360
     361            // Add CSIM for Legend if defined
     362            if( !empty($p[4]) ) {
     363               
     364                $xe = $x1 + $this->xmargin+$this->mark_abs_hsize+$aImg->GetTextWidth($p[0]);
     365                $ye = $y1 + max($this->mark_abs_vsize,$aImg->GetTextHeight($p[0]));
     366                $coords = "$x1,$y1,$xe,$y1,$xe,$ye,$x1,$ye";
     367                if( ! empty($p[4]) ) {
     368                    $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".htmlentities($p[4])."\"";
     369
     370                    if( !empty($p[6]) ) {
     371                        $this->csimareas .= " target=\"".$p[6]."\"";
     372                    }
     373
     374                    if( !empty($p[5]) ) {
     375                        $tmp=sprintf($p[5],$p[0]);
     376                        $this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
     377                    }
     378                    $this->csimareas .= " />\n";
     379                }
     380            }
     381            if( $i >= $this->layout_n ) {
     382                $x1 = $xp+$this->mark_abs_hsize+$this->xlmargin;
     383                $y1 += $rowheight[$row++];
     384                $i = 1;
     385            }
     386            else {
     387                $x1 += $colwidth[($i-1) % $numcolumns] ;
     388                ++$i;
     389            }
     390        }       
    491391    }
    492392} // Class
    493 
     393       
    494394?>
Note: See TracChangeset for help on using the changeset viewer.