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_scatter.php

    r265 r267  
    1 <?php
     1<?php 
    22/*=======================================================================
    3  // File:        JPGRAPH_SCATTER.PHP
    4  // Description: Scatter (and impuls) plot extension for JpGraph
    5  // Created:     2001-02-11
    6  // Ver:         $Id: jpgraph_scatter.php 1397 2009-06-27 21:34:14Z ljp $
    7  //
    8  // Copyright (c) Asial Corporation. All rights reserved.
    9  //========================================================================
    10  */
     3// File:        JPGRAPH_SCATTER.PHP
     4// Description: Scatter (and impuls) plot extension for JpGraph
     5// Created:     2001-02-11
     6// Ver:         $Id: jpgraph_scatter.php 957 2007-12-01 14:00:29Z ljp $
     7//
     8// Copyright (c) Aditus Consulting. All rights reserved.
     9//========================================================================
     10*/
    1111require_once ('jpgraph_plotmark.inc.php');
    1212
     
    2020    public $iArrowSize = 2;
    2121    private $isizespec = array(
    22         array(2,1),array(3,2),array(4,3),array(6,4),array(7,4),array(8,5),array(10,6),array(12,7),array(16,8),array(20,10)
    23         );
    24     function __construct() {
    25         // Empty
     22        array(2,1),array(3,2),array(4,3),array(6,4),array(7,4),array(8,5),array(10,6),array(12,7),array(16,8),array(20,10));
     23    function FieldArrow() {
    2624    }
    2725
    2826    function SetSize($aSize,$aArrowSize=2) {
    29         $this->iSize = $aSize;
    30         $this->iArrowSize = $aArrowSize;
     27        $this->iSize = $aSize;
     28        $this->iArrowSize = $aArrowSize;
    3129    }
    3230
    3331    function SetColor($aColor) {
    34         $this->iColor = $aColor;
     32        $this->iColor = $aColor;
    3533    }
    3634
    3735    function Stroke($aImg,$x,$y,$a) {
    38         // First rotate the center coordinates
    39         list($x,$y) = $aImg->Rotate($x,$y);
    40 
    41         $old_origin = $aImg->SetCenter($x,$y);
    42         $old_a = $aImg->a;
    43         $aImg->SetAngle(-$a+$old_a);
    44 
    45         $dx = round($this->iSize/2);
    46         $c = array($x-$dx,$y,$x+$dx,$y);
    47         $x += $dx;
    48 
    49         list($dx,$dy) = $this->isizespec[$this->iArrowSize];
    50         $ca = array($x,$y,$x-$dx,$y-$dy,$x-$dx,$y+$dy,$x,$y);
    51 
    52         $aImg->SetColor($this->iColor);
    53         $aImg->Polygon($c);
    54         $aImg->FilledPolygon($ca);
    55 
    56         $aImg->SetCenter($old_origin[0],$old_origin[1]);
    57         $aImg->SetAngle($old_a);
     36        // First rotate the center coordinates
     37        list($x,$y) = $aImg->Rotate($x,$y);
     38
     39        $old_origin = $aImg->SetCenter($x,$y);
     40        $old_a = $aImg->a;
     41        $aImg->SetAngle(-$a+$old_a);
     42
     43        $dx = round($this->iSize/2);
     44        $c = array($x-$dx,$y,$x+$dx,$y);
     45        $x += $dx;
     46
     47        list($dx,$dy) = $this->isizespec[$this->iArrowSize];
     48        $ca = array($x,$y,$x-$dx,$y-$dy,$x-$dx,$y+$dy,$x,$y);
     49
     50        $aImg->SetColor($this->iColor);
     51        $aImg->Polygon($c);
     52        $aImg->FilledPolygon($ca);
     53
     54        $aImg->SetCenter($old_origin[0],$old_origin[1]);
     55        $aImg->SetAngle($old_a);
    5856    }
    5957}
     
    6765    private $iAngles = array();
    6866    private $iCallback = '';
    69 
    70     function __construct($datay,$datax,$angles) {
    71         if( (count($datax) != count($datay)) )
    72         JpGraphError::RaiseL(20001);//("Fieldplots must have equal number of X and Y points.");
    73         if( (count($datax) != count($angles)) )
    74         JpGraphError::RaiseL(20002);//("Fieldplots must have an angle specified for each X and Y points.");
    75 
    76         $this->iAngles = $angles;
    77 
    78         parent::__construct($datay,$datax);
    79         $this->value->SetAlign('center','center');
    80         $this->value->SetMargin(15);
    81 
    82         $this->arrow = new FieldArrow();
     67   
     68    function FieldPlot($datay,$datax,$angles) {
     69        if( (count($datax) != count($datay)) )
     70            JpGraphError::RaiseL(20001);//("Fieldplots must have equal number of X and Y points.");
     71        if( (count($datax) != count($angles)) )
     72            JpGraphError::RaiseL(20002);//("Fieldplots must have an angle specified for each X and Y points.");
     73       
     74        $this->iAngles = $angles;
     75
     76        $this->Plot($datay,$datax);
     77        $this->value->SetAlign('center','center');
     78        $this->value->SetMargin(15);
     79
     80        $this->arrow = new FieldArrow();
    8381    }
    8482
    8583    function SetCallback($aFunc) {
    86         $this->iCallback = $aFunc;
     84        $this->iCallback = $aFunc;
    8785    }
    8886
    8987    function Stroke($img,$xscale,$yscale) {
    9088
    91         // Remeber base color and size
    92         $bc = $this->arrow->iColor;
    93         $bs = $this->arrow->iSize;
    94         $bas = $this->arrow->iArrowSize;
    95 
    96         for( $i=0; $i<$this->numpoints; ++$i ) {
    97             // Skip null values
    98             if( $this->coords[0][$i]==="" )
    99             continue;
    100 
    101             $f = $this->iCallback;
    102             if( $f != "" ) {
    103                 list($cc,$cs,$cas) = call_user_func($f,$this->coords[1][$i],$this->coords[0][$i],$this->iAngles[$i]);
    104                 // Fall back on global data if the callback isn't set
    105                 if( $cc  == "" ) $cc = $bc;
    106                 if( $cs  == "" ) $cs = $bs;
    107                 if( $cas == "" ) $cas = $bas;
    108                 $this->arrow->SetColor($cc);
    109                 $this->arrow->SetSize($cs,$cas);
    110             }
    111 
    112             $xt = $xscale->Translate($this->coords[1][$i]);
    113             $yt = $yscale->Translate($this->coords[0][$i]);
    114 
    115             $this->arrow->Stroke($img,$xt,$yt,$this->iAngles[$i]);
    116             $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
    117         }
    118     }
    119 
     89        // Remeber base color and size
     90        $bc = $this->arrow->iColor;
     91        $bs = $this->arrow->iSize;
     92        $bas = $this->arrow->iArrowSize;
     93
     94        for( $i=0; $i<$this->numpoints; ++$i ) {
     95            // Skip null values
     96            if( $this->coords[0][$i]==="" )
     97                continue;
     98
     99            $f = $this->iCallback;
     100            if( $f != "" ) {
     101                list($cc,$cs,$cas) = call_user_func($f,$this->coords[1][$i],$this->coords[0][$i],$this->iAngles[$i]);
     102                // Fall back on global data if the callback isn't set
     103                if( $cc  == "" ) $cc = $bc;
     104                if( $cs  == "" ) $cs = $bs;
     105                if( $cas == "" ) $cas = $bas;
     106                $this->arrow->SetColor($cc);       
     107                $this->arrow->SetSize($cs,$cas);
     108            }
     109
     110            $xt = $xscale->Translate($this->coords[1][$i]);
     111            $yt = $yscale->Translate($this->coords[0][$i]);     
     112
     113            $this->arrow->Stroke($img,$xt,$yt,$this->iAngles[$i]);
     114            $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
     115        }
     116    }
     117       
    120118    // Framework function
    121119    function Legend($aGraph) {
    122         if( $this->legend != "" ) {
    123             $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
    124             $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
    125         }
    126     }
     120        if( $this->legend != "" ) {
     121            $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
     122                                $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
     123        }
     124    }   
    127125}
    128126
     
    132130//===================================================
    133131class ScatterPlot extends Plot {
    134     public $mark,$link;
     132    public $mark = '';
    135133    private $impuls = false;
    136     //---------------
    137     // CONSTRUCTOR
    138     function __construct($datay,$datax=false) {
    139         if( is_array($datax) && (count($datax) != count($datay)) ) {
    140                 JpGraphError::RaiseL(20003);//("Scatterplot must have equal number of X and Y points.");
    141         }
    142         parent::__construct($datay,$datax);
    143         $this->mark = new PlotMark();
    144         $this->mark->SetType(MARK_SQUARE);
    145         $this->mark->SetColor($this->color);
    146         $this->value->SetAlign('center','center');
    147         $this->value->SetMargin(0);
    148         $this->link = new LineProperty(1,'black','solid');
    149         $this->link->iShow = false;
    150     }
    151 
    152     //---------------
    153     // PUBLIC METHODS
     134    private $linkpoints = false, $linkpointweight=1, $linkpointcolor="black";
     135//---------------
     136// CONSTRUCTOR
     137    function ScatterPlot($datay,$datax=false) {
     138        if( (count($datax) != count($datay)) && is_array($datax))
     139            JpGraphError::RaiseL(20003);//("Scatterplot must have equal number of X and Y points.");
     140        $this->Plot($datay,$datax);
     141        $this->mark = new PlotMark();
     142        $this->mark->SetType(MARK_SQUARE);
     143        $this->mark->SetColor($this->color);
     144        $this->value->SetAlign('center','center');
     145        $this->value->SetMargin(0);
     146    }
     147
     148//---------------
     149// PUBLIC METHODS       
    154150    function SetImpuls($f=true) {
    155         $this->impuls = $f;
    156     }
    157 
    158     function SetStem($f=true) {
    159         $this->impuls = $f;
    160     }
     151        $this->impuls = $f;
     152    }   
    161153
    162154    // Combine the scatter plot points with a line
    163     function SetLinkPoints($aFlag=true,$aColor="black",$aWeight=1,$aStyle='solid') {
    164         $this->link->iShow = $aFlag;
    165         $this->link->iColor = $aColor;
    166         $this->link->iWeight = $aWeight;
    167         $this->link->iStyle = $aStyle;
     155    function SetLinkPoints($aFlag=true,$aColor="black",$aWeight=1) {
     156        $this->linkpoints=$aFlag;
     157        $this->linkpointcolor=$aColor;
     158        $this->linkpointweight=$aWeight;
    168159    }
    169160
    170161    function Stroke($img,$xscale,$yscale) {
    171162
    172         $ymin=$yscale->scale_abs[0];
    173         if( $yscale->scale[0] < 0 )
    174         $yzero=$yscale->Translate(0);
    175         else
    176         $yzero=$yscale->scale_abs[0];
    177 
    178         $this->csimareas = '';
    179         for( $i=0; $i<$this->numpoints; ++$i ) {
    180 
    181             // Skip null values
    182             if( $this->coords[0][$i]==='' || $this->coords[0][$i]==='-' || $this->coords[0][$i]==='x')
    183             continue;
    184 
    185             if( isset($this->coords[1]) )
    186             $xt = $xscale->Translate($this->coords[1][$i]);
    187             else
    188             $xt = $xscale->Translate($i);
    189             $yt = $yscale->Translate($this->coords[0][$i]);
    190 
    191 
    192             if( $this->link->iShow && isset($yt_old) ) {
    193                 $img->SetColor($this->link->iColor);
    194                 $img->SetLineWeight($this->link->iWeight);
    195                 $old = $img->SetLineStyle($this->link->iStyle);
    196                 $img->StyleLine($xt_old,$yt_old,$xt,$yt);
    197                 $img->SetLineStyle($old);
    198             }
    199 
    200             if( $this->impuls ) {
    201                 $img->SetColor($this->color);
    202                 $img->SetLineWeight($this->weight);
    203                 $img->Line($xt,$yzero,$xt,$yt);
    204             }
    205 
    206             if( !empty($this->csimtargets[$i]) ) {
    207                 if( !empty($this->csimwintargets[$i]) ) {
    208                     $this->mark->SetCSIMTarget($this->csimtargets[$i],$this->csimwintargets[$i]);
    209                 }
    210                 else {
    211                     $this->mark->SetCSIMTarget($this->csimtargets[$i]);
    212                 }
    213                 $this->mark->SetCSIMAlt($this->csimalts[$i]);
    214             }
    215 
    216             if( isset($this->coords[1]) ) {
    217                 $this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
    218             }
    219             else {
    220                 $this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
    221             }
    222 
    223             $this->mark->Stroke($img,$xt,$yt);
    224 
    225             $this->csimareas .= $this->mark->GetCSIMAreas();
    226             $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
    227 
    228             $xt_old = $xt;
    229             $yt_old = $yt;
    230         }
    231     }
    232 
     163        $ymin=$yscale->scale_abs[0];
     164        if( $yscale->scale[0] < 0 )
     165            $yzero=$yscale->Translate(0);
     166        else
     167            $yzero=$yscale->scale_abs[0];
     168           
     169        $this->csimareas = '';
     170        for( $i=0; $i<$this->numpoints; ++$i ) {
     171
     172            // Skip null values
     173            if( $this->coords[0][$i]==='' || $this->coords[0][$i]==='-' || $this->coords[0][$i]==='x')
     174                continue;
     175
     176            if( isset($this->coords[1]) )
     177                $xt = $xscale->Translate($this->coords[1][$i]);
     178            else
     179                $xt = $xscale->Translate($i);
     180            $yt = $yscale->Translate($this->coords[0][$i]);     
     181
     182
     183            if( $this->linkpoints && isset($yt_old) ) {
     184                $img->SetColor($this->linkpointcolor);
     185                $img->SetLineWeight($this->linkpointweight);
     186                $img->Line($xt_old,$yt_old,$xt,$yt);
     187            }
     188
     189            if( $this->impuls ) {
     190                $img->SetColor($this->color);
     191                $img->SetLineWeight($this->weight);
     192                $img->Line($xt,$yzero,$xt,$yt);
     193            }
     194       
     195            if( !empty($this->csimtargets[$i]) ) {
     196                if( !empty($this->csimwintargets[$i]) ) {
     197                    $this->mark->SetCSIMTarget($this->csimtargets[$i],$this->csimwintargets[$i]);
     198                }
     199                else {
     200                    $this->mark->SetCSIMTarget($this->csimtargets[$i]);
     201                }
     202                $this->mark->SetCSIMAlt($this->csimalts[$i]);
     203            }
     204           
     205            if( isset($this->coords[1]) ) {
     206                $this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
     207            }
     208            else {
     209                $this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
     210            }
     211
     212            $this->mark->Stroke($img,$xt,$yt);
     213       
     214            $this->csimareas .= $this->mark->GetCSIMAreas();
     215            $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
     216
     217            $xt_old = $xt;
     218            $yt_old = $yt;
     219        }
     220    }
     221       
    233222    // Framework function
    234223    function Legend($aGraph) {
    235         if( $this->legend != "" ) {
    236             $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
    237             $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
    238         }
    239     }
     224        if( $this->legend != "" ) {
     225            $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
     226                                $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
     227        }
     228    }   
    240229} // Class
    241230/* EOF */
Note: See TracChangeset for help on using the changeset viewer.