Ejemplo n.º 1
0
 protected function calcMinMaxInterval()
 {
     // INIT intervals
     $intervals = array();
     foreach (array(1, 2, 3, 4) as $num) {
         $dec = pow(0.1, $num);
         foreach (array(1, 2, 5) as $n => $int) {
             $intervals[] = $int * $dec;
         }
     }
     foreach (array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) as $num) {
         $dec = bcpow(10, $num);
         foreach (array(1, 2, 5) as $n => $int) {
             $intervals[] = bcmul($int, $dec);
         }
     }
     //------
     $sides = array(GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT);
     foreach ($sides as $snum => $side) {
         if (!isset($this->axis_valuetype[$side])) {
             continue;
         }
         if ($this->type == GRAPH_TYPE_STACKED) {
             $this->m_minY[$side] = min($this->m_minY[$side], 0);
             continue;
         }
         if ($this->ymax_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_maxY[$side] = $this->yaxismax;
             $this->m_minY[$side] = 0;
         }
         if ($this->ymin_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_minY[$side] = $this->yaxismin;
             if ($this->ymax_type == GRAPH_YAXIS_TYPE_CALCULATED && bccomp($this->m_maxY[$side], $this->m_minY[$side]) == -1) {
                 $this->m_maxY[$side] = bcmul($this->m_minY[$side], 1.2);
             }
         }
     }
     // SIDES
     $side = GRAPH_YAXIS_SIDE_LEFT;
     $other_side = GRAPH_YAXIS_SIDE_RIGHT;
     if (!isset($this->axis_valuetype[GRAPH_YAXIS_SIDE_LEFT])) {
         $side = GRAPH_YAXIS_SIDE_RIGHT;
         $other_side = GRAPH_YAXIS_SIDE_LEFT;
     }
     $tmp_minY = array();
     $tmp_maxY = array();
     $tmp_minY[GRAPH_YAXIS_SIDE_LEFT] = $this->m_minY[GRAPH_YAXIS_SIDE_LEFT];
     $tmp_minY[GRAPH_YAXIS_SIDE_RIGHT] = $this->m_minY[GRAPH_YAXIS_SIDE_RIGHT];
     $tmp_maxY[GRAPH_YAXIS_SIDE_LEFT] = $this->m_maxY[GRAPH_YAXIS_SIDE_LEFT];
     $tmp_maxY[GRAPH_YAXIS_SIDE_RIGHT] = $this->m_maxY[GRAPH_YAXIS_SIDE_RIGHT];
     //------
     // CALC interval
     $columnInterval = bcdiv(bcmul($this->gridPixelsVert, bcsub($this->m_maxY[$side], $this->m_minY[$side])), $this->sizeY);
     $dist = bcmul(5, bcpow(10, 18));
     $interval = 0;
     foreach ($intervals as $num => $int) {
         //we must get a positive number
         if (bccomp($int, $columnInterval) == -1) {
             $t = bcsub($columnInterval, $int);
         } else {
             $t = bcsub($int, $columnInterval);
         }
         if (bccomp($t, $dist) == -1) {
             $dist = $t;
             $interval = $int;
         }
     }
     $columnInterval = bcdiv(bcmul($this->gridPixelsVert, bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side])), $this->sizeY);
     $dist = bcmul(5, bcpow(10, 18));
     $interval_other_side = 0;
     foreach ($intervals as $num => $int) {
         //we must get a positive number
         if (bccomp($int, $columnInterval) == -1) {
             $t = bcsub($columnInterval, $int);
         } else {
             $t = bcsub($int, $columnInterval);
         }
         if (bccomp($t, $dist) == -1) {
             $dist = $t;
             $interval_other_side = $int;
         }
     }
     //------
     // correcting MIN & MAX
     $this->m_minY[$side] = bcmul(bcfloor(bcdiv($this->m_minY[$side], $interval)), $interval);
     $this->m_maxY[$side] = bcmul(bcceil(bcdiv($this->m_maxY[$side], $interval)), $interval);
     $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval_other_side)), $interval_other_side);
     $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval_other_side)), $interval_other_side);
     //--------------------
     $this->gridLinesCount[$side] = bcceil(bcdiv(bcsub($this->m_maxY[$side], $this->m_minY[$side]), $interval));
     // we add 1 interval so max Y wouldn't be at the top
     if (bccomp($this->m_maxY[$side], $tmp_maxY[$side], 2) == 0) {
         $this->gridLinesCount[$side]++;
     }
     $this->m_maxY[$side] = bcadd($this->m_minY[$side], bcmul($interval, $this->gridLinesCount[$side]));
     $this->gridStep[$side] = $interval;
     if (isset($this->axis_valuetype[$other_side])) {
         $dist = bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]);
         $interval = 1;
         foreach ($intervals as $num => $int) {
             if (bccomp($dist, bcmul($this->gridLinesCount[$side], $int)) == -1) {
                 $interval = $int;
                 break;
             }
         }
         // correcting MIN & MAX
         $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval)), $interval);
         $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval)), $interval);
         //--------------------
         // if we lowered min more than highed max - need additional recalculating
         if (bccomp($tmp_maxY[$other_side], $this->m_maxY[$other_side]) == 1 || bccomp($tmp_minY[$other_side], $this->m_minY[$other_side]) == -1) {
             $dist = bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]);
             $interval = 0;
             foreach ($intervals as $num => $int) {
                 if (bccomp($dist, bcmul($this->gridLinesCount[$side], $int)) == -1) {
                     $interval = $int;
                     break;
                 }
             }
             // recorrecting MIN & MAX
             $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval)), $interval);
             $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval)), $interval);
             //--------------------
         }
         $this->gridLinesCount[$other_side] = $this->gridLinesCount[$side];
         $this->m_maxY[$other_side] = bcadd($this->m_minY[$other_side], bcmul($interval, $this->gridLinesCount[$other_side]));
         $this->gridStep[$other_side] = $interval;
     }
     $sides = array(GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT);
     foreach ($sides as $snum => $graphSide) {
         if (!isset($this->axis_valuetype[$graphSide])) {
             continue;
         }
         if ($this->type == GRAPH_TYPE_STACKED) {
             $this->m_minY[$graphSide] = bccomp($tmp_minY[GRAPH_YAXIS_SIDE_LEFT], 0) == -1 ? $tmp_minY[GRAPH_YAXIS_SIDE_LEFT] : 0;
         }
         if ($this->ymax_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_maxY[$graphSide] = $this->yaxismax;
             $this->m_minY[$graphSide] = 0;
         } else {
             if ($this->ymax_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                 $this->m_maxY[$graphSide] = $tmp_maxY[$graphSide];
             }
         }
         if ($this->ymin_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_minY[$graphSide] = $this->yaxismin;
         } else {
             if ($this->ymin_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                 $this->m_minY[$graphSide] = $tmp_minY[$graphSide];
             }
         }
     }
     // division by zero
     $diff_val = bcsub($this->m_maxY[$side], $this->m_minY[$side]);
     if (bccomp($diff_val, 0) == 0) {
         $diff_val = 1;
     }
     $this->gridStepX[$side] = bcdiv(bcmul($this->gridStep[$side], $this->sizeY), $diff_val);
     if (isset($this->axis_valuetype[$other_side])) {
         $diff_val = bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]);
         if (bccomp($diff_val, 0) == 0) {
             $diff_val = 1;
         }
         $this->gridStepX[$other_side] = bcdiv(bcmul($this->gridStep[$other_side], $this->sizeY), $diff_val);
     }
     //SDI($this->gridStep);
     //SDI($this->gridStepX);
 }
Ejemplo n.º 2
0
 protected function calcMinMaxInterval()
 {
     // init intervals
     $intervals = array();
     foreach (array(1, 2, 3, 4) as $num) {
         $dec = pow(0.1, $num);
         foreach (array(1, 2, 5) as $int) {
             $intervals[] = bcmul($int, $dec);
         }
     }
     // check if items use B or Bps units
     $leftBase1024 = false;
     $rightBase1024 = false;
     for ($item = 0; $item < $this->num; $item++) {
         if ($this->items[$item]['units'] == 'B' || $this->items[$item]['units'] == 'Bps') {
             if ($this->items[$item]['axisside'] == GRAPH_YAXIS_SIDE_LEFT) {
                 $leftBase1024 = true;
             } else {
                 $rightBase1024 = true;
             }
         }
     }
     foreach (array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) as $num) {
         $dec = bcpow(10, $num);
         foreach (array(1, 2, 5) as $int) {
             $intervals[] = bcmul($int, $dec);
         }
     }
     if (isset($this->axis_valuetype[GRAPH_YAXIS_SIDE_RIGHT])) {
         $sides[] = GRAPH_YAXIS_SIDE_RIGHT;
     }
     if (isset($this->axis_valuetype[GRAPH_YAXIS_SIDE_LEFT]) || !isset($sides)) {
         $sides[] = GRAPH_YAXIS_SIDE_LEFT;
     }
     foreach ($sides as $snum => $side) {
         if (!isset($this->axis_valuetype[$side])) {
             continue;
         }
         if (($this->ymin_type != GRAPH_YAXIS_TYPE_FIXED || $this->ymax_type != GRAPH_YAXIS_TYPE_CALCULATED) && $this->type == GRAPH_TYPE_STACKED) {
             $this->m_minY[$side] = min($this->m_minY[$side], 0);
             $this->validateMinMax($this->m_minY[$side], $this->m_maxY[$side]);
             continue;
         }
         if ($this->ymax_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_maxY[$side] = $this->yaxismax;
             if ($this->ymin_type == GRAPH_YAXIS_TYPE_CALCULATED && ($this->m_minY[$side] == null || bccomp($this->m_maxY[$side], $this->m_minY[$side]) == 0 || bccomp($this->m_maxY[$side], $this->m_minY[$side]) == -1)) {
                 if ($this->m_maxY[$side] == 0) {
                     $this->m_minY[$side] = -1;
                 } elseif ($this->m_maxY[$side] > 0) {
                     $this->m_minY[$side] = bcmul($this->m_maxY[$side], 0.8);
                 } else {
                     $this->m_minY[$side] = bcmul($this->m_maxY[$side], 1.2);
                 }
             }
         }
         if ($this->ymin_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_minY[$side] = $this->yaxismin;
             if ($this->ymax_type == GRAPH_YAXIS_TYPE_CALCULATED && ($this->m_maxY[$side] == null || bccomp($this->m_maxY[$side], $this->m_minY[$side]) == 0 || bccomp($this->m_maxY[$side], $this->m_minY[$side]) == -1)) {
                 if ($this->m_minY[$side] > 0) {
                     $this->m_maxY[$side] = bcmul($this->m_minY[$side], 1.2);
                 } else {
                     $this->m_maxY[$side] = bcmul($this->m_minY[$side], 0.8);
                 }
             }
         }
         $this->validateMinMax($this->m_minY[$side], $this->m_maxY[$side]);
     }
     $side = GRAPH_YAXIS_SIDE_LEFT;
     $other_side = GRAPH_YAXIS_SIDE_RIGHT;
     // invert sides and it bases, if left side not exist
     if (!isset($this->axis_valuetype[GRAPH_YAXIS_SIDE_LEFT])) {
         $side = GRAPH_YAXIS_SIDE_RIGHT;
         $other_side = GRAPH_YAXIS_SIDE_LEFT;
         $tempBase = $leftBase1024;
         $leftBase1024 = $rightBase1024;
         $rightBase1024 = $tempBase;
     }
     if (!isset($this->m_minY[$side])) {
         $this->m_minY[$side] = 0;
     }
     if (!isset($this->m_maxY[$side])) {
         $this->m_maxY[$side] = 0;
     }
     if (!isset($this->m_minY[$other_side])) {
         $this->m_minY[$other_side] = 0;
     }
     if (!isset($this->m_maxY[$other_side])) {
         $this->m_maxY[$other_side] = 0;
     }
     $tmp_minY = $this->m_minY;
     $tmp_maxY = $this->m_maxY;
     // calc interval
     $columnInterval = bcdiv(bcmul($this->gridPixelsVert, bcsub($this->m_maxY[$side], $this->m_minY[$side])), $this->sizeY);
     $dist = bcmul(5, bcpow(10, 18));
     $interval = 0;
     foreach ($intervals as $int) {
         // we must get a positive number
         if (bccomp($int, $columnInterval) == -1) {
             $t = bcsub($columnInterval, $int);
         } else {
             $t = bcsub($int, $columnInterval);
         }
         if (bccomp($t, $dist) == -1) {
             $dist = $t;
             $interval = $int;
         }
     }
     // calculate interval, if left side use B or Bps
     if ($leftBase1024) {
         $interval = getBase1024Interval($interval, $this->m_minY[$side], $this->m_maxY[$side]);
     }
     $columnInterval = bcdiv(bcmul($this->gridPixelsVert, bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side])), $this->sizeY);
     $dist = bcmul(5, bcpow(10, 18));
     $interval_other_side = 0;
     foreach ($intervals as $int) {
         // we must get a positive number
         if (bccomp($int, $columnInterval) == -1) {
             $t = bcsub($columnInterval, $int);
         } else {
             $t = bcsub($int, $columnInterval);
         }
         if (bccomp($t, $dist) == -1) {
             $dist = $t;
             $interval_other_side = $int;
         }
     }
     // calculate interval, if right side use B or Bps
     if ($rightBase1024) {
         $interval_other_side = getBase1024Interval($interval_other_side, $this->m_minY[$other_side], $this->m_maxY[$other_side]);
     }
     // save original min and max items values
     foreach ($sides as $graphSide) {
         $minY[$graphSide] = $this->m_minY[$graphSide];
         $maxY[$graphSide] = $this->m_maxY[$graphSide];
     }
     if (!isset($minY[$side])) {
         $minY[$side] = 0;
     }
     if (!isset($maxY[$side])) {
         $maxY[$side] = 0;
     }
     // correcting MIN & MAX
     $this->m_minY[$side] = bcmul(bcfloor(bcdiv($this->m_minY[$side], $interval)), $interval);
     $this->m_maxY[$side] = bcmul(bcceil(bcdiv($this->m_maxY[$side], $interval)), $interval);
     $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval_other_side)), $interval_other_side);
     $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval_other_side)), $interval_other_side);
     // add intervals so min/max Y wouldn't be at the top
     foreach ($sides as $graphSide) {
         if ($graphSide == $side) {
             $tmpInterval = $interval;
         } else {
             $tmpInterval = $interval_other_side;
         }
         if (bccomp($this->m_minY[$graphSide], $minY[$side]) == 0 && $this->m_minY[$graphSide] != null && $this->m_minY[$graphSide] != 0) {
             $this->m_minY[$graphSide] = bcsub($this->m_minY[$graphSide], $tmpInterval);
         }
         if (bccomp($this->m_maxY[$graphSide], $maxY[$graphSide]) == 0 && $this->m_maxY[$graphSide] != null && $this->m_maxY[$graphSide] != 0) {
             $this->m_maxY[$graphSide] = bcadd($this->m_maxY[$graphSide], $tmpInterval);
         }
     }
     // calculate interval count for main and other side
     $this->gridLinesCount[$side] = bcceil(bcdiv(bcsub($this->m_maxY[$side], $this->m_minY[$side]), $interval));
     $this->gridLinesCount[$other_side] = bcceil(bcdiv(bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]), $interval_other_side));
     $this->m_maxY[$side] = bcadd($this->m_minY[$side], bcmul($interval, $this->gridLinesCount[$side]));
     $this->gridStep[$side] = $interval;
     if (isset($this->axis_valuetype[$other_side])) {
         // other side correction
         $dist = bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]);
         $interval = 1;
         foreach ($intervals as $int) {
             if (bccomp($dist, bcmul($this->gridLinesCount[$side], $int)) == -1) {
                 $interval = $int;
                 break;
             }
         }
         // correcting MIN & MAX
         $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval)), $interval);
         $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval)), $interval);
         // if we lowered min more than highed max - need additional recalculating
         if (bccomp($tmp_maxY[$other_side], $this->m_maxY[$other_side]) == 1 || bccomp($tmp_minY[$other_side], $this->m_minY[$other_side]) == -1) {
             $dist = bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]);
             $interval = 0;
             foreach ($intervals as $int) {
                 if (bccomp($dist, bcmul($this->gridLinesCount[$side], $int)) == -1) {
                     $interval = $int;
                     break;
                 }
             }
             // recorrecting MIN & MAX
             $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval)), $interval);
             $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval)), $interval);
         }
         // calculate interval, if right side use B or Bps
         if (isset($rightBase1024)) {
             $interval = getBase1024Interval($interval, $this->m_minY[$side], $this->m_maxY[$side]);
             // recorrecting MIN & MAX
             $this->m_minY[$other_side] = bcmul(bcfloor(bcdiv($this->m_minY[$other_side], $interval)), $interval);
             $this->m_maxY[$other_side] = bcmul(bcceil(bcdiv($this->m_maxY[$other_side], $interval)), $interval);
         }
         $this->gridLinesCount[$other_side] = $this->gridLinesCount[$side];
         $this->m_maxY[$other_side] = bcadd($this->m_minY[$other_side], bcmul($interval, $this->gridLinesCount[$other_side]));
         $this->gridStep[$other_side] = $interval;
     }
     foreach ($sides as $graphSide) {
         if (!isset($this->axis_valuetype[$graphSide])) {
             continue;
         }
         if ($this->type == GRAPH_TYPE_STACKED) {
             $this->m_minY[$graphSide] = bccomp($tmp_minY[GRAPH_YAXIS_SIDE_LEFT], 0) == -1 ? $tmp_minY[GRAPH_YAXIS_SIDE_LEFT] : 0;
         }
         if ($this->ymax_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_maxY[$graphSide] = $this->yaxismax;
         } elseif ($this->ymax_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
             $this->m_maxY[$graphSide] = $tmp_maxY[$graphSide];
         }
         if ($this->ymin_type == GRAPH_YAXIS_TYPE_FIXED) {
             $this->m_minY[$graphSide] = $this->yaxismin;
         } elseif ($this->ymin_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
             $this->m_minY[$graphSide] = $tmp_minY[$graphSide];
         }
         $this->validateMinMax($this->m_minY[$graphSide], $this->m_maxY[$graphSide]);
     }
     // division by zero
     $diff_val = bcsub($this->m_maxY[$side], $this->m_minY[$side]);
     if (bccomp($diff_val, 0) == 0) {
         $diff_val = 1;
     }
     $this->gridStepX[$side] = bcdiv(bcmul($this->gridStep[$side], $this->sizeY), $diff_val);
     if (isset($this->axis_valuetype[$other_side])) {
         $diff_val = bcsub($this->m_maxY[$other_side], $this->m_minY[$other_side]);
         if (bccomp($diff_val, 0) == 0) {
             $diff_val = 1;
         }
         $this->gridStepX[$other_side] = bcdiv(bcmul($this->gridStep[$other_side], $this->sizeY), $diff_val);
     }
 }