vendor/amenadiel/jpgraph/src/graph/Axis.php line 27

  1. <?php
  2. /**
  3.  * JPGraph v4.0.3
  4.  */
  5. namespace Amenadiel\JpGraph\Graph;
  6. use Amenadiel\JpGraph\Util;
  7. /**
  8.  * @class Axis
  9.  * // Description: Defines X and Y axis. Notes that at the
  10.  * // moment the code is not really good since the axis on
  11.  * // several occasion must know wheter it's an X or Y axis.
  12.  * // This was a design decision to make the code easier to
  13.  * // follow.
  14.  */
  15. class Axis extends AxisPrototype
  16. {
  17.     public function __construct($img$aScale$color 'black')
  18.     {
  19.         parent::__construct($img$aScale$color);
  20.     }
  21.     // Stroke the axis.
  22.     public function Stroke($aOtherAxisScale$aStrokeLabels true)
  23.     {
  24.         if ($this->hide) {
  25.             return;
  26.         }
  27.         if (is_numeric($this->pos)) {
  28.             $pos $aOtherAxisScale->Translate($this->pos);
  29.         } else {
  30.             // Default to minimum of other scale if pos not set
  31.             if (($aOtherAxisScale->GetMinVal() >= && $this->pos == false) || $this->pos == 'min') {
  32.                 $pos $aOtherAxisScale->scale_abs[0];
  33.             } elseif ($this->pos == 'max') {
  34.                 $pos $aOtherAxisScale->scale_abs[1];
  35.             } else {
  36.                 // If negative set x-axis at 0
  37.                 $this->pos 0;
  38.                 $pos       $aOtherAxisScale->Translate(0);
  39.             }
  40.         }
  41.         $pos += $this->iDeltaAbsPos;
  42.         $this->img->SetLineWeight($this->weight);
  43.         $this->img->SetColor($this->color);
  44.         $this->img->SetFont($this->font_family$this->font_style$this->font_size);
  45.         if ($this->scale->type == 'x') {
  46.             if (!$this->hide_line) {
  47.                 // Stroke X-axis
  48.                 $this->img->FilledRectangle(
  49.                     $this->img->left_margin,
  50.                     $pos,
  51.                     $this->img->width $this->img->right_margin,
  52.                     $pos $this->weight 1
  53.                 );
  54.             }
  55.             if ($this->title_side == SIDE_DOWN) {
  56.                 $y      $pos $this->img->GetFontHeight() + $this->title_margin $this->title->margin;
  57.                 $yalign 'top';
  58.             } else {
  59.                 $y      $pos $this->img->GetFontHeight() - $this->title_margin $this->title->margin;
  60.                 $yalign 'bottom';
  61.             }
  62.             if ($this->title_adjust == 'high') {
  63.                 $this->title->SetPos($this->img->width $this->img->right_margin$y'right'$yalign);
  64.             } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
  65.                 $this->title->SetPos(($this->img->width $this->img->left_margin $this->img->right_margin) / $this->img->left_margin$y'center'$yalign);
  66.             } elseif ($this->title_adjust == 'low') {
  67.                 $this->title->SetPos($this->img->left_margin$y'left'$yalign);
  68.             } else {
  69.                 Util\JpGraphError::RaiseL(25060$this->title_adjust); //('Unknown alignment specified for X-axis title. ('.$this->title_adjust.')');
  70.             }
  71.         } elseif ($this->scale->type == 'y') {
  72.             // Add line weight to the height of the axis since
  73.             // the x-axis could have a width>1 and we want the axis to fit nicely together.
  74.             if (!$this->hide_line) {
  75.                 // Stroke Y-axis
  76.                 $this->img->FilledRectangle(
  77.                     $pos $this->weight 1,
  78.                     $this->img->top_margin,
  79.                     $pos,
  80.                     $this->img->height $this->img->bottom_margin $this->weight 1
  81.                 );
  82.             }
  83.             $x $pos;
  84.             if ($this->title_side == SIDE_LEFT) {
  85.                 $x -= $this->title_margin;
  86.                 $x -= $this->title->margin;
  87.                 $halign 'right';
  88.             } else {
  89.                 $x += $this->title_margin;
  90.                 $x += $this->title->margin;
  91.                 $halign 'left';
  92.             }
  93.             // If the user has manually specified an hor. align
  94.             // then we override the automatic settings with this
  95.             // specifed setting. Since default is 'left' we compare
  96.             // with that. (This means a manually set 'left' align
  97.             // will have no effect.)
  98.             if ($this->title->halign != 'left') {
  99.                 $halign $this->title->halign;
  100.             }
  101.             if ($this->title_adjust == 'high') {
  102.                 $this->title->SetPos($x$this->img->top_margin$halign'top');
  103.             } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
  104.                 $this->title->SetPos($x, ($this->img->height $this->img->top_margin $this->img->bottom_margin) / $this->img->top_margin$halign'center');
  105.             } elseif ($this->title_adjust == 'low') {
  106.                 $this->title->SetPos($x$this->img->height $this->img->bottom_margin$halign'bottom');
  107.             } else {
  108.                 Util\JpGraphError::RaiseL(25061$this->title_adjust); //('Unknown alignment specified for Y-axis title. ('.$this->title_adjust.')');
  109.             }
  110.         }
  111.         $this->scale->ticks->Stroke($this->img$this->scale$pos);
  112.         if ($aStrokeLabels) {
  113.             if (!$this->hide_labels) {
  114.                 $this->StrokeLabels($pos);
  115.             }
  116.             $this->title->Stroke($this->img);
  117.         }
  118.     }
  119.     /**
  120.      * PRIVATE METHODS
  121.      * // Draw all the tick labels on major tick marks.
  122.      *
  123.      * @param mixed $aPos
  124.      * @param mixed $aMinor
  125.      * @param mixed $aAbsLabel
  126.      */
  127.     public function StrokeLabels($aPos$aMinor false$aAbsLabel false)
  128.     {
  129.         if (is_array($this->label_color) && safe_count($this->label_color) > 3) {
  130.             $this->ticks_label_colors $this->label_color;
  131.             $this->img->SetColor($this->label_color[0]);
  132.         } else {
  133.             $this->img->SetColor($this->label_color);
  134.         }
  135.         $this->img->SetFont($this->font_family$this->font_style$this->font_size);
  136.         $yoff $this->img->GetFontHeight() / 2;
  137.         // Only draw labels at major tick marks
  138.         $nbr safe_count($this->scale->ticks->maj_ticks_label);
  139.         // We have the option to not-display the very first mark
  140.         // (Usefull when the first label might interfere with another
  141.         // axis.)
  142.         $i $this->show_first_label 1;
  143.         if (!$this->show_last_label) {
  144.             --$nbr;
  145.         }
  146.         // Now run through all labels making sure we don't overshoot the end
  147.         // of the scale.
  148.         $ncolor 0;
  149.         if (isset($this->ticks_label_colors)) {
  150.             $ncolor safe_count($this->ticks_label_colors);
  151.         }
  152.         while ($i $nbr) {
  153.             // $tpos holds the absolute text position for the label
  154.             $tpos $this->scale->ticks->maj_ticklabels_pos[$i];
  155.             // Note. the $limit is only used for the x axis since we
  156.             // might otherwise overshoot if the scale has been centered
  157.             // This is due to us "loosing" the last tick mark if we center.
  158.             if ($this->scale->type == 'x' && $tpos $this->img->width $this->img->right_margin 1) {
  159.                 return;
  160.             }
  161.             // we only draw every $label_step label
  162.             if (($i $this->label_step) == 0) {
  163.                 // Set specific label color if specified
  164.                 if ($ncolor 0) {
  165.                     $this->img->SetColor($this->ticks_label_colors[$i $ncolor]);
  166.                 }
  167.                 // If the label has been specified use that and in other case
  168.                 // just label the mark with the actual scale value
  169.                 $m $this->scale->ticks->GetMajor();
  170.                 // ticks_label has an entry for each data point and is the array
  171.                 // that holds the labels set by the user. If the user hasn't
  172.                 // specified any values we use whats in the automatically asigned
  173.                 // labels in the maj_ticks_label
  174.                 if (isset($this->ticks_label[$i $m])) {
  175.                     $label $this->ticks_label[$i $m];
  176.                 } else {
  177.                     if ($aAbsLabel) {
  178.                         $label abs($this->scale->ticks->maj_ticks_label[$i]);
  179.                     } else {
  180.                         $label $this->scale->ticks->maj_ticks_label[$i];
  181.                     }
  182.                     // We number the scale from 1 and not from 0 so increase by one
  183.                     if ($this->scale->textscale &&
  184.                         $this->scale->ticks->label_formfunc == '' &&
  185.                         !$this->scale->ticks->HaveManualLabels()) {
  186.                         ++$label;
  187.                     }
  188.                 }
  189.                 if ($this->scale->type == 'x') {
  190.                     if ($this->labelPos == SIDE_DOWN) {
  191.                         if ($this->label_angle == || $this->label_angle == 90) {
  192.                             if ($this->label_halign == '' && $this->label_valign == '') {
  193.                                 $this->img->SetTextAlign('center''top');
  194.                             } else {
  195.                                 $this->img->SetTextAlign($this->label_halign$this->label_valign);
  196.                             }
  197.                         } else {
  198.                             if ($this->label_halign == '' && $this->label_valign == '') {
  199.                                 $this->img->SetTextAlign('right''top');
  200.                             } else {
  201.                                 $this->img->SetTextAlign($this->label_halign$this->label_valign);
  202.                             }
  203.                         }
  204.                         $this->img->StrokeText(
  205.                             $tpos,
  206.                             $aPos $this->tick_label_margin,
  207.                             $label,
  208.                             $this->label_angle,
  209.                             $this->label_para_align
  210.                         );
  211.                     } else {
  212.                         if ($this->label_angle == || $this->label_angle == 90) {
  213.                             if ($this->label_halign == '' && $this->label_valign == '') {
  214.                                 $this->img->SetTextAlign('center''bottom');
  215.                             } else {
  216.                                 $this->img->SetTextAlign($this->label_halign$this->label_valign);
  217.                             }
  218.                         } else {
  219.                             if ($this->label_halign == '' && $this->label_valign == '') {
  220.                                 $this->img->SetTextAlign('right''bottom');
  221.                             } else {
  222.                                 $this->img->SetTextAlign($this->label_halign$this->label_valign);
  223.                             }
  224.                         }
  225.                         $this->img->StrokeText(
  226.                             $tpos,
  227.                             $aPos $this->tick_label_margin 1,
  228.                             $label,
  229.                             $this->label_angle,
  230.                             $this->label_para_align
  231.                         );
  232.                     }
  233.                 } else {
  234.                     // scale->type == "y"
  235.                     //if( $this->label_angle!=0 )
  236.                     //Util\JpGraphError::Raise(" Labels at an angle are not supported on Y-axis");
  237.                     if ($this->labelPos == SIDE_LEFT) {
  238.                         // To the left of y-axis
  239.                         if ($this->label_halign == '' && $this->label_valign == '') {
  240.                             $this->img->SetTextAlign('right''center');
  241.                         } else {
  242.                             $this->img->SetTextAlign($this->label_halign$this->label_valign);
  243.                         }
  244.                         $this->img->StrokeText($aPos $this->tick_label_margin$tpos$label$this->label_angle$this->label_para_align);
  245.                     } else {
  246.                         // To the right of the y-axis
  247.                         if ($this->label_halign == '' && $this->label_valign == '') {
  248.                             $this->img->SetTextAlign('left''center');
  249.                         } else {
  250.                             $this->img->SetTextAlign($this->label_halign$this->label_valign);
  251.                         }
  252.                         $this->img->StrokeText($aPos $this->tick_label_margin$tpos$label$this->label_angle$this->label_para_align);
  253.                     }
  254.                 }
  255.             }
  256.             ++$i;
  257.         }
  258.     }
  259. }