icons
8 years ago
markers
8 years ago
GoogleChart.php
8 years ago
GoogleChartApi.php
8 years ago
GoogleChartAxis.php
8 years ago
GoogleChartData.php
8 years ago
GoogleChartIcon.php
8 years ago
GoogleChartMarker.php
8 years ago
LICENSE
8 years ago
GoogleChartAxis.php
330 lines
| 1 | <?php |
| 2 | |
| 3 | /** @file |
| 4 | * This file is part of Google Chart PHP library. |
| 5 | * |
| 6 | * Copyright (c) 2010 Rémi Lanvin <remi@cloudconnected.fr> |
| 7 | * |
| 8 | * Licensed under the MIT license. |
| 9 | * |
| 10 | * For the full copyright and license information, please view the LICENSE file. |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * An Axis. |
| 15 | */ |
| 16 | class GoogleChartAxis |
| 17 | { |
| 18 | const ALIGN_LEFT = -1; |
| 19 | const ALIGN_CENTER = 0; |
| 20 | const ALIGN_RIGHT = 1; |
| 21 | |
| 22 | /** |
| 23 | * @var string Name of the axis |
| 24 | */ |
| 25 | protected $name = null; |
| 26 | /** |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $labels = null; |
| 30 | |
| 31 | protected $range = null; |
| 32 | protected $tick_marks = null; |
| 33 | protected $style = null; |
| 34 | |
| 35 | protected $chxs = 0; |
| 36 | protected $label_color = '666666'; |
| 37 | protected $font_size = '11'; |
| 38 | protected $label_alignment = null; |
| 39 | protected $draw_line = true; |
| 40 | protected $draw_tick_marks = true; |
| 41 | protected $tick_color = '666666'; |
| 42 | protected $label_positions = null; |
| 43 | |
| 44 | /** |
| 45 | * Create a new axis. |
| 46 | * |
| 47 | * @param string $name The name of the axis. Supported axis are 'x', 'y', 't' (top) or 'r' (right). |
| 48 | */ |
| 49 | public function __construct($name) |
| 50 | { |
| 51 | switch ( $name ) { |
| 52 | case 'x': |
| 53 | case 't': |
| 54 | $this->label_alignment = 0; |
| 55 | break; |
| 56 | case 'r': |
| 57 | $this->label_alignment = -1; |
| 58 | break; |
| 59 | case 'y': |
| 60 | $this->label_alignment = 1; |
| 61 | break; |
| 62 | default: |
| 63 | throw new InvalidArgumentException('Axis names must be x, y, t or r.'); |
| 64 | } |
| 65 | |
| 66 | $this->name = $name; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the name. |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | public function getName() |
| 75 | { |
| 76 | return $this->name; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Custom axis labels (chxl). |
| 81 | * |
| 82 | * @see http://code.google.com/apis/chart/docs/chart_params.html#axis_labels |
| 83 | */ |
| 84 | public function setLabels(array $labels) |
| 85 | { |
| 86 | $this->labels = $labels; |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | public function getLabels($compute = true) |
| 91 | { |
| 92 | if ( ! $compute ) { |
| 93 | return $this->labels; |
| 94 | } |
| 95 | |
| 96 | if ( $this->labels === null ) |
| 97 | return null; |
| 98 | |
| 99 | return '%d:|'.implode('|',$this->labels); |
| 100 | } |
| 101 | |
| 102 | public function hasCustomLabels() |
| 103 | { |
| 104 | return $this->labels !== null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Axis ranges (chxr). |
| 109 | * |
| 110 | * Specify the range of values that appear. |
| 111 | * |
| 112 | * @see http://code.google.com/apis/chart/docs/chart_params.html#axis_range |
| 113 | */ |
| 114 | public function setRange($start_val, $end_val, $step = false) |
| 115 | { |
| 116 | $this->range = array( |
| 117 | 'start_val' => $start_val === null ? 0 : $start_val, |
| 118 | 'end_val' => $end_val === null ? 100 : $end_val, |
| 119 | 'step' => $step === null ? false : $step |
| 120 | ); |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | public function getRange($compute = true) |
| 125 | { |
| 126 | if ( ! $compute ) |
| 127 | return $this->range; |
| 128 | |
| 129 | if ( $this->range === null ) |
| 130 | return null; |
| 131 | |
| 132 | $str = '%d,'.$this->range['start_val'].','.$this->range['end_val']; |
| 133 | if ( $this->range['step'] !== false ) |
| 134 | $str .= ','.$this->range['step']; |
| 135 | return $str; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Axis Tick Mark Styles (chxtc) |
| 140 | * |
| 141 | * @see http://code.google.com/apis/chart/docs/chart_params.html#axis_tick_marks |
| 142 | */ |
| 143 | public function setTickMarks() |
| 144 | { |
| 145 | $this->tick_marks = func_get_args(); |
| 146 | if ( ! isset($this->tick_marks[0]) ) |
| 147 | $this->tick_marks = null; |
| 148 | |
| 149 | return $this; |
| 150 | } |
| 151 | |
| 152 | public function getTickMarks($compute = true) |
| 153 | { |
| 154 | if ( ! $compute ) |
| 155 | return $this->tick_marks; |
| 156 | |
| 157 | if ( $this->tick_marks === null ) |
| 158 | return null; |
| 159 | |
| 160 | return '%d,'.implode(',',$this->tick_marks); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @c chxp |
| 165 | */ |
| 166 | public function setLabelPositions() |
| 167 | { |
| 168 | $this->label_positions = func_get_args(); |
| 169 | if ( ! isset($this->label_positions[0]) ) |
| 170 | $this->label_positions = null; |
| 171 | |
| 172 | return $this; |
| 173 | } |
| 174 | |
| 175 | public function computeChxp($axis_index) |
| 176 | { |
| 177 | if ( ! $this->label_positions ) |
| 178 | return null; |
| 179 | |
| 180 | $str = $axis_index.','.implode(',',$this->label_positions); |
| 181 | return $str; |
| 182 | } |
| 183 | |
| 184 | public function hasChxp() |
| 185 | { |
| 186 | return $this->label_positions !== null; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @name Axis style (chxs) |
| 191 | */ |
| 192 | //@{ |
| 193 | |
| 194 | /** |
| 195 | * @since 0.4 |
| 196 | */ |
| 197 | public function setLabelFormat() |
| 198 | { |
| 199 | if ( $this->chxs < 1 ) { |
| 200 | $this->chxs = 1; |
| 201 | } |
| 202 | |
| 203 | // @todo |
| 204 | |
| 205 | return $this; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @since 0.4 |
| 210 | */ |
| 211 | public function setLabelColor($color) |
| 212 | { |
| 213 | if ( $this->chxs < 1 ) { |
| 214 | $this->chxs = 1; |
| 215 | } |
| 216 | $this->label_color = $color; |
| 217 | return $this; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @since 0.4 |
| 222 | */ |
| 223 | public function setFontSize($size) |
| 224 | { |
| 225 | if ( $this->chxs < 2 ) { |
| 226 | $this->chxs = 2; |
| 227 | } |
| 228 | $this->font_size = $size; |
| 229 | return $this; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @since 0.4 |
| 234 | */ |
| 235 | public function setLabelAlignment($alignment) |
| 236 | { |
| 237 | if ( $this->chxs < 3 ) { |
| 238 | $this->chxs = 3; |
| 239 | } |
| 240 | $this->label_alignment = $alignment; |
| 241 | return $this; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @since 0.4 |
| 246 | */ |
| 247 | public function setDrawLine($line) |
| 248 | { |
| 249 | if ( $this->chxs < 4 ) { |
| 250 | $this->chxs = 4; |
| 251 | } |
| 252 | $this->draw_line = $line; |
| 253 | return $this; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @since 0.4 |
| 258 | */ |
| 259 | public function setDrawTickMarks($tick_marks) |
| 260 | { |
| 261 | if ( $this->chxs < 4 ) { |
| 262 | $this->chxs = 4; |
| 263 | } |
| 264 | $this->draw_tick_marks = $tick_marks; |
| 265 | return $this; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @since 0.4 |
| 270 | */ |
| 271 | public function setTickColor($tick_color) |
| 272 | { |
| 273 | if ( $this->chxs < 5 ) { |
| 274 | $this->chxs = 5; |
| 275 | } |
| 276 | $this->tick_color = $tick_color; |
| 277 | return $this; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @since 0.5 |
| 282 | */ |
| 283 | public function computeChxs($axis_index, $chart_type = null) |
| 284 | { |
| 285 | // Parameter not needed for this axis |
| 286 | if ( ! $this->chxs ) |
| 287 | return null; |
| 288 | |
| 289 | // Axis index (provided by GoogleChart class at runtime) |
| 290 | $str = $axis_index; |
| 291 | |
| 292 | // @todo format string |
| 293 | |
| 294 | $str .= ','.$this->label_color; |
| 295 | if ( $this->chxs > 1 ) { |
| 296 | $str .= ','.$this->font_size; |
| 297 | if ( $this->chxs > 2 ) { |
| 298 | $str .= ','.$this->label_alignment; |
| 299 | if ( $this->chxs > 3 ) { |
| 300 | $str .= ','; |
| 301 | if ( ! $this->draw_line && ! $this->draw_tick_marks ) { |
| 302 | $str .= '_'; |
| 303 | } |
| 304 | else { |
| 305 | if ( $this->draw_line ) { |
| 306 | $str .= 'l'; |
| 307 | } |
| 308 | if ( $this->draw_tick_marks ) { |
| 309 | $str .= 't'; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Not supported in Google-o-meter |
| 314 | if ( $this->chxs > 4 && $chart_type != 'gom') { |
| 315 | $str .= ','.$this->tick_color; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return $str; |
| 322 | } |
| 323 | |
| 324 | public function getLabelFormat($compute = true) |
| 325 | { |
| 326 | |
| 327 | } |
| 328 | //@} |
| 329 | } |
| 330 |