| Total Complexity | 113 |
| Total Lines | 728 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BarPlot often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BarPlot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class BarPlot extends Plot |
||
| 38 | { |
||
| 39 | public $fill = false; |
||
| 40 | public $fill_color = 'lightblue'; // Default is to fill with light blue |
||
| 41 | public $iPattern = -1; |
||
| 42 | public $iPatternDensity = 80; |
||
| 43 | public $iPatternColor = 'black'; |
||
| 44 | public $valuepos = 'top'; |
||
| 45 | public $grad = false; |
||
| 46 | public $grad_style = 1; |
||
| 47 | public $grad_fromcolor = [50, 50, 200]; |
||
| 48 | public $grad_tocolor = [255, 255, 255]; |
||
| 49 | public $ymin = 0; |
||
| 50 | protected $width = 0.4; // in percent of major ticks |
||
| 51 | protected $abswidth = -1; // Width in absolute pixels |
||
| 52 | protected $ybase = 0; // Bars start at 0 |
||
| 53 | protected $align = 'center'; |
||
| 54 | protected $bar_shadow = false; |
||
| 55 | protected $bar_shadow_color = 'black'; |
||
| 56 | protected $bar_shadow_hsize = 3; |
||
| 57 | protected $bar_shadow_vsize = 3; |
||
| 58 | protected $bar_3d = false; |
||
| 59 | protected $bar_3d_hsize = 3; |
||
| 60 | protected $bar_3d_vsize = 3; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * CONSTRUCTOR. |
||
| 64 | * |
||
| 65 | * @param mixed $datay |
||
| 66 | * @param mixed $datax |
||
| 67 | */ |
||
| 68 | public function __construct($datay, $datax = false) |
||
| 69 | { |
||
| 70 | parent::__construct($datay, $datax); |
||
| 71 | ++$this->numpoints; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * PUBLIC METHODS. |
||
| 76 | * |
||
| 77 | * @param mixed $aColor |
||
| 78 | * @param mixed $aHSize |
||
| 79 | * @param mixed $aVSize |
||
| 80 | * @param mixed $aShow |
||
| 81 | */ |
||
| 82 | // Set a drop shadow for the bar (or rather an "up-right" shadow) |
||
| 83 | public function SetShadow($aColor = 'black', $aHSize = 3, $aVSize = 3, $aShow = true) |
||
| 84 | { |
||
| 85 | $this->bar_shadow = $aShow; |
||
| 86 | $this->bar_shadow_color = $aColor; |
||
| 87 | $this->bar_shadow_vsize = $aVSize; |
||
| 88 | $this->bar_shadow_hsize = $aHSize; |
||
| 89 | |||
| 90 | // Adjust the value margin to compensate for shadow |
||
| 91 | $this->value->margin += $aVSize; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function Set3D($aHSize = 3, $aVSize = 3, $aShow = true) |
||
| 101 | } |
||
| 102 | |||
| 103 | // DEPRECATED use SetYBase instead |
||
| 104 | public function SetYMin($aYStartValue) |
||
| 105 | { |
||
| 106 | //die("JpGraph Error: Deprecated function SetYMin. Use SetYBase() instead."); |
||
| 107 | $this->ybase = $aYStartValue; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Specify the base value for the bars |
||
| 111 | public function SetYBase($aYStartValue) |
||
| 114 | } |
||
| 115 | |||
| 116 | // The method will take the specified pattern anre |
||
| 117 | // return a pattern index that corresponds to the original |
||
| 118 | // patterm being rotated 90 degreees. This is needed when plottin |
||
| 119 | // Horizontal bars |
||
| 120 | public function RotatePattern($aPat, $aRotate = true) |
||
| 128 | } |
||
| 129 | |||
| 130 | public function Legend($graph) |
||
| 131 | { |
||
| 132 | if ($this->grad && $this->legend != '' && !$this->fill) { |
||
| 133 | $color = [$this->grad_fromcolor, $this->grad_tocolor]; |
||
| 134 | // In order to differentiate between gradients and cooors specified as an Image\RGB triple |
||
| 135 | $graph->legend->Add( |
||
| 136 | $this->legend, |
||
| 137 | $color, |
||
| 138 | '', |
||
| 139 | -$this->grad_style, |
||
| 140 | $this->legendcsimtarget, |
||
| 141 | $this->legendcsimalt, |
||
| 142 | $this->legendcsimwintarget |
||
| 143 | ); |
||
| 144 | } elseif ($this->legend != '' && ($this->iPattern > -1 || is_array($this->iPattern))) { |
||
| 145 | if (is_array($this->iPattern)) { |
||
| 146 | $p1 = $this->RotatePattern($this->iPattern[0], $graph->img->a == 90); |
||
| 147 | $p2 = $this->iPatternColor[0]; |
||
| 148 | $p3 = $this->iPatternDensity[0]; |
||
| 149 | } else { |
||
| 150 | $p1 = $this->RotatePattern($this->iPattern, $graph->img->a == 90); |
||
| 151 | $p2 = $this->iPatternColor; |
||
| 152 | $p3 = $this->iPatternDensity; |
||
| 153 | } |
||
| 154 | if ($p3 < 90) { |
||
| 155 | $p3 += 5; |
||
| 156 | } |
||
| 157 | |||
| 158 | $color = [$p1, $p2, $p3, $this->fill_color]; |
||
| 159 | // A kludge: Too mark that we add a pattern we use a type value of < 100 |
||
| 160 | $graph->legend->Add( |
||
| 161 | $this->legend, |
||
| 162 | $color, |
||
| 163 | '', |
||
| 164 | -101, |
||
| 165 | $this->legendcsimtarget, |
||
| 166 | $this->legendcsimalt, |
||
| 167 | $this->legendcsimwintarget |
||
| 168 | ); |
||
| 169 | } elseif ($this->fill_color && $this->legend != '') { |
||
| 170 | if (is_array($this->fill_color)) { |
||
|
|
|||
| 171 | $graph->legend->Add( |
||
| 172 | $this->legend, |
||
| 173 | $this->fill_color[0], |
||
| 174 | '', |
||
| 175 | 0, |
||
| 176 | $this->legendcsimtarget, |
||
| 177 | $this->legendcsimalt, |
||
| 178 | $this->legendcsimwintarget |
||
| 179 | ); |
||
| 180 | } else { |
||
| 181 | $graph->legend->Add( |
||
| 182 | $this->legend, |
||
| 183 | $this->fill_color, |
||
| 184 | '', |
||
| 185 | 0, |
||
| 186 | $this->legendcsimtarget, |
||
| 187 | $this->legendcsimalt, |
||
| 188 | $this->legendcsimwintarget |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | // Gets called before any axis are stroked |
||
| 195 | public function PreStrokeAdjust($graph) |
||
| 196 | { |
||
| 197 | parent::PreStrokeAdjust($graph); |
||
| 198 | |||
| 199 | // If we are using a log Y-scale we want the base to be at the |
||
| 200 | // minimum Y-value unless the user have specifically set some other |
||
| 201 | // value than the default. |
||
| 202 | if (substr($graph->axtype, -3, 3) == 'log' && $this->ybase == 0) { |
||
| 203 | $this->ybase = $graph->yaxis->scale->GetMinVal(); |
||
| 204 | } |
||
| 205 | |||
| 206 | // For a "text" X-axis scale we will adjust the |
||
| 207 | // display of the bars a little bit. |
||
| 208 | if (substr($graph->axtype, 0, 3) == 'tex') { |
||
| 209 | // Position the ticks between the bars |
||
| 210 | $graph->xaxis->scale->ticks->SetXLabelOffset(0.5, 0); |
||
| 211 | |||
| 212 | // Center the bars |
||
| 213 | if ($this->abswidth > -1) { |
||
| 214 | $graph->SetTextScaleAbsCenterOff($this->abswidth); |
||
| 215 | } else { |
||
| 216 | if ($this->align == 'center') { |
||
| 217 | $graph->SetTextScaleOff(0.5 - $this->width / 2); |
||
| 218 | } elseif ($this->align == 'right') { |
||
| 219 | $graph->SetTextScaleOff(1 - $this->width); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } elseif (($this instanceof AccBarPlot) || ($this instanceof GroupBarPlot)) { |
||
| 223 | // We only set an absolute width for linear and int scale |
||
| 224 | // for text scale the width will be set to a fraction of |
||
| 225 | // the majstep width. |
||
| 226 | if ($this->abswidth == -1) { |
||
| 227 | // Not set |
||
| 228 | // set width to a visuable sensible default |
||
| 229 | $this->abswidth = $graph->img->plotwidth / (2 * $this->numpoints); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | public function Min() |
||
| 235 | { |
||
| 236 | $m = parent::Min(); |
||
| 237 | if ($m[1] >= $this->ybase) { |
||
| 238 | $m[1] = $this->ybase; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $m; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function Max() |
||
| 245 | { |
||
| 246 | $m = parent::Max(); |
||
| 247 | if ($m[1] <= $this->ybase) { |
||
| 248 | $m[1] = $this->ybase; |
||
| 249 | } |
||
| 250 | |||
| 251 | return $m; |
||
| 252 | } |
||
| 253 | |||
| 254 | // Specify width as fractions of the major stepo size |
||
| 255 | public function SetWidth($aWidth) |
||
| 256 | { |
||
| 257 | if ($aWidth > 1) { |
||
| 258 | // Interpret this as absolute width |
||
| 259 | $this->abswidth = $aWidth; |
||
| 260 | } else { |
||
| 261 | $this->width = $aWidth; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | // Specify width in absolute pixels. If specified this |
||
| 266 | // overrides SetWidth() |
||
| 267 | public function SetAbsWidth($aWidth) |
||
| 268 | { |
||
| 269 | $this->abswidth = $aWidth; |
||
| 270 | } |
||
| 271 | |||
| 272 | public function SetAlign($aAlign) |
||
| 275 | } |
||
| 276 | |||
| 277 | public function SetNoFill() |
||
| 278 | { |
||
| 279 | $this->grad = false; |
||
| 280 | $this->fill_color = false; |
||
| 281 | $this->fill = false; |
||
| 282 | } |
||
| 283 | |||
| 284 | public function SetFillColor($aColor) |
||
| 285 | { |
||
| 286 | // Do an extra error check if the color is specified as an Image\RGB array triple |
||
| 287 | // In that case convert it to a hex string since it will otherwise be |
||
| 288 | // interpretated as an array of colors for each individual bar. |
||
| 289 | |||
| 290 | $aColor = Image\RGB::tryHexConversion($aColor); |
||
| 291 | $this->fill = true; |
||
| 292 | $this->fill_color = $aColor; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function SetFillGradient($aFromColor, $aToColor = null, $aStyle = null) |
||
| 296 | { |
||
| 297 | $this->grad = true; |
||
| 298 | $this->grad_fromcolor = $aFromColor; |
||
| 299 | $this->grad_tocolor = $aToColor; |
||
| 300 | $this->grad_style = $aStyle; |
||
| 301 | } |
||
| 302 | |||
| 303 | public function SetValuePos($aPos) |
||
| 304 | { |
||
| 305 | $this->valuepos = $aPos; |
||
| 306 | } |
||
| 307 | |||
| 308 | public function SetPattern($aPattern, $aColor = 'black') |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | public function _SetPatternHelper($aPattern, &$aPatternValue, &$aDensity) |
||
| 389 | //('Unknown pattern specified in call to BarPlot::SetPattern()'); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | public function Stroke($img, $xscale, $yscale) |
||
| 394 | { |
||
| 395 | $numpoints = safe_count($this->coords[0]); |
||
| 396 | if (isset($this->coords[1])) { |
||
| 397 | if (safe_count($this->coords[1]) != $numpoints) { |
||
| 765 | } |
||
| 766 | } // @class |
||
| 767 | |||
| 768 | /* EOF */ |
||
| 769 |