HuasoFoundries /
jpgraph
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * JPGraph v4.0.3 |
||
| 5 | */ |
||
| 6 | |||
| 7 | namespace Amenadiel\JpGraph\Plot; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * This class represent a plotting of a contour outline of data given as a X-Y matrice. |
||
| 11 | */ |
||
| 12 | class ContourPlot extends Plot |
||
| 13 | { |
||
| 14 | private $contour; |
||
| 15 | private $contourCoord; |
||
| 16 | private $contourVal; |
||
| 17 | private $contourColor; |
||
| 18 | private $nbrCountours = 0; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 19 | private $dataMatrix = []; |
||
| 20 | private $invertLegend = false; |
||
| 21 | private $interpFactor = 1; |
||
| 22 | private $flipData = false; |
||
| 23 | private $isobar = 10; |
||
| 24 | private $showLegend = false; |
||
| 25 | private $highcontrast = false; |
||
| 26 | private $highcontrastbw = false; |
||
| 27 | private $manualIsobarColors = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Construct a contour plotting algorithm. The end result of the algorithm is a sequence of |
||
| 31 | * line segments for each isobar given as two vertices. |
||
| 32 | * |
||
| 33 | * @param $aDataMatrix The Z-data to be used |
||
| 34 | * @param $aIsobar A mixed variable, if it is an integer then this specified the number of isobars to use. |
||
| 35 | * The values of the isobars are automatically detrmined to be equ-spaced between the min/max value of the |
||
| 36 | * data. If it is an array then it explicetely gives the isobar values |
||
| 37 | * @param $aInvert By default the matrice with row index 0 corresponds to Y-value 0, i.e. in the bottom of |
||
| 38 | * the plot. If this argument is true then the row with the highest index in the matrice corresponds to |
||
| 39 | * Y-value 0. In affect flipping the matrice around an imaginary horizontal axis. |
||
| 40 | * @param $aHighContrast Use high contrast colors (blue/red:ish) |
||
| 41 | * @param $aHighContrastBW Use only black colors for contours |
||
| 42 | * @param mixed $aFactor |
||
| 43 | * @param mixed $aIsobarColors |
||
| 44 | * |
||
| 45 | * @return an instance of the contour plot algorithm |
||
| 46 | */ |
||
| 47 | 1 | public function __construct($aDataMatrix, $aIsobar = 10, $aFactor = 1, $aInvert = false, $aIsobarColors = []) |
|
| 48 | { |
||
| 49 | 1 | $this->dataMatrix = $aDataMatrix; |
|
| 50 | 1 | $this->flipData = $aInvert; |
|
| 51 | 1 | $this->isobar = $aIsobar; |
|
| 52 | 1 | $this->interpFactor = $aFactor; |
|
| 53 | |||
| 54 | 1 | if ($this->interpFactor > 1) { |
|
| 55 | 1 | if ($this->interpFactor > 5) { |
|
| 56 | Util\JpGraphError::RaiseL(28007); // ContourPlot interpolation factor is too large (>5) |
||
| 57 | } |
||
| 58 | |||
| 59 | 1 | $ip = new MeshInterpolate(); |
|
| 60 | 1 | $this->dataMatrix = $ip->Linear($this->dataMatrix, $this->interpFactor); |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | $this->contour = new Contour($this->dataMatrix, $this->isobar, $aIsobarColors); |
|
| 64 | |||
| 65 | 1 | if (is_array($aIsobar)) { |
|
| 66 | $this->nbrContours = safe_count($aIsobar); |
||
| 67 | } else { |
||
| 68 | 1 | $this->nbrContours = $aIsobar; |
|
| 69 | } |
||
| 70 | 1 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Flipe the data around the center. |
||
| 74 | * |
||
| 75 | * @param $aFlg |
||
| 76 | */ |
||
| 77 | 1 | public function SetInvert($aFlg = true) |
|
| 78 | { |
||
| 79 | 1 | $this->flipData = $aFlg; |
|
| 80 | 1 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Set the colors for the isobar lines. |
||
| 84 | * |
||
| 85 | * @param $aColorArray |
||
| 86 | */ |
||
| 87 | public function SetIsobarColors($aColorArray) |
||
| 88 | { |
||
| 89 | $this->manualIsobarColors = $aColorArray; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Show the legend. |
||
| 94 | * |
||
| 95 | * @param $aFlg true if the legend should be shown |
||
| 96 | */ |
||
| 97 | 1 | public function ShowLegend($aFlg = true) |
|
| 98 | { |
||
| 99 | 1 | $this->showLegend = $aFlg; |
|
| 100 | 1 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param $aFlg true if the legend should start with the lowest isobar on top |
||
| 104 | * |
||
| 105 | * @return unknown_type |
||
| 106 | */ |
||
| 107 | 1 | public function Invertlegend($aFlg = true) |
|
| 108 | { |
||
| 109 | 1 | $this->invertLegend = $aFlg; |
|
| 110 | 1 | } |
|
| 111 | |||
| 112 | /* Internal method. Give the min value to be used for the scaling |
||
| 113 | * |
||
| 114 | */ |
||
| 115 | public function Min() |
||
| 116 | { |
||
| 117 | return [0, 0]; |
||
| 118 | } |
||
| 119 | |||
| 120 | /* Internal method. Give the max value to be used for the scaling |
||
| 121 | * |
||
| 122 | */ |
||
| 123 | public function Max() |
||
| 124 | { |
||
| 125 | return [count($this->dataMatrix[0]) - 1, safe_count($this->dataMatrix) - 1]; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Internal ramewrok method to setup the legend to be used for this plot. |
||
| 130 | * |
||
| 131 | * @param $aGraph The parent graph class |
||
| 132 | */ |
||
| 133 | 1 | public function Legend($aGraph) |
|
| 134 | { |
||
| 135 | 1 | if (!$this->showLegend) { |
|
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | 1 | if ($this->invertLegend) { |
|
| 140 | 1 | for ($i = 0; $i < $this->nbrContours; ++$i) { |
|
| 141 | 1 | $aGraph->legend->Add(sprintf('%.1f', $this->contourVal[$i]), $this->contourColor[$i]); |
|
| 142 | } |
||
| 143 | } else { |
||
| 144 | 1 | for ($i = $this->nbrContours - 1; $i >= 0; --$i) { |
|
| 145 | 1 | $aGraph->legend->Add(sprintf('%.1f', $this->contourVal[$i]), $this->contourColor[$i]); |
|
| 146 | } |
||
| 147 | } |
||
| 148 | 1 | } |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Framework function which gets called before the Stroke() method is called. |
||
| 152 | * |
||
| 153 | * @see Plot#PreScaleSetup($aGraph) |
||
| 154 | * |
||
| 155 | * @param mixed $aGraph |
||
| 156 | */ |
||
| 157 | 1 | public function PreScaleSetup($aGraph) |
|
| 158 | { |
||
| 159 | 1 | $xn = safe_count($this->dataMatrix[0]) - 1; |
|
| 160 | 1 | $yn = safe_count($this->dataMatrix) - 1; |
|
| 161 | |||
| 162 | 1 | $aGraph->xaxis->scale->Update($aGraph->img, 0, $xn); |
|
| 163 | 1 | $aGraph->yaxis->scale->Update($aGraph->img, 0, $yn); |
|
| 164 | |||
| 165 | 1 | $this->contour->SetInvert($this->flipData); |
|
| 166 | 1 | list($this->contourCoord, $this->contourVal, $this->contourColor) = $this->contour->getIsobars(); |
|
| 167 | 1 | } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Use high contrast color schema. |
||
| 171 | * |
||
| 172 | * @param $aFlg True, to use high contrast color |
||
| 173 | * @param $aBW True, Use only black and white color schema |
||
| 174 | */ |
||
| 175 | 1 | public function UseHighContrastColor($aFlg = true, $aBW = false) |
|
| 176 | { |
||
| 177 | 1 | $this->highcontrast = $aFlg; |
|
| 178 | 1 | $this->highcontrastbw = $aBW; |
|
| 179 | 1 | $this->contour->UseHighContrastColor($this->highcontrast, $this->highcontrastbw); |
|
| 180 | 1 | } |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Internal method. Stroke the contour plot to the graph. |
||
| 184 | * |
||
| 185 | * @param $img Image handler |
||
| 186 | * @param $xscale Instance of the xscale to use |
||
| 187 | * @param $yscale Instance of the yscale to use |
||
| 188 | */ |
||
| 189 | 1 | public function Stroke($img, $xscale, $yscale) |
|
| 190 | { |
||
| 191 | 1 | if (safe_count($this->manualIsobarColors) > 0) { |
|
| 192 | $this->contourColor = $this->manualIsobarColors; |
||
| 193 | if (safe_count($this->manualIsobarColors) != $this->nbrContours) { |
||
| 194 | Util\JpGraphError::RaiseL(28002); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | 1 | $img->SetLineWeight($this->line_weight); |
|
| 199 | |||
| 200 | 1 | for ($c = 0; $c < $this->nbrContours; ++$c) { |
|
| 201 | 1 | $img->SetColor($this->contourColor[$c]); |
|
| 202 | |||
| 203 | 1 | $n = safe_count($this->contourCoord[$c]); |
|
| 204 | 1 | $i = 0; |
|
| 205 | 1 | while ($i < $n) { |
|
| 206 | 1 | list($x1, $y1) = $this->contourCoord[$c][$i][0]; |
|
| 207 | 1 | $x1t = $xscale->Translate($x1); |
|
| 208 | 1 | $y1t = $yscale->Translate($y1); |
|
| 209 | |||
| 210 | 1 | list($x2, $y2) = $this->contourCoord[$c][$i++][1]; |
|
| 211 | 1 | $x2t = $xscale->Translate($x2); |
|
| 212 | 1 | $y2t = $yscale->Translate($y2); |
|
| 213 | |||
| 214 | 1 | $img->Line($x1t, $y1t, $x2t, $y2t); |
|
| 215 | } |
||
| 216 | } |
||
| 217 | 1 | } |
|
| 218 | } |
||
| 219 |