GoogleChartCandlestickMarker.php
8 years ago
GoogleChartLineMarker.php
8 years ago
GoogleChartRangeMarker.php
8 years ago
GoogleChartShapeMarker.php
8 years ago
GoogleChartTextMarker.php
8 years ago
GoogleChartTextMarker.php
291 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 | require_once dirname(__FILE__).'/../GoogleChartMarker.php'; |
| 14 | |
| 15 | /** |
| 16 | * A Text marker. |
| 17 | * |
| 18 | * This class implement Text and Data Value Markers feature (@c chm). |
| 19 | * |
| 20 | * @par Example with value marker |
| 21 | * @include marker_text_value.php |
| 22 | * |
| 23 | * @par Example with text marker |
| 24 | * @include marker_text_text.php |
| 25 | * |
| 26 | * @see GoogleChartMarker |
| 27 | * @see http://code.google.com/apis/chart/docs/chart_params.html#gcharts_data_point_labels |
| 28 | */ |
| 29 | class GoogleChartTextMarker extends GoogleChartMarker |
| 30 | { |
| 31 | const FLAG = 'f'; |
| 32 | const TEXT = 't'; |
| 33 | const ANNOTATION = 'A'; |
| 34 | const VALUE = 'N'; |
| 35 | |
| 36 | const LEFT = 'r'; |
| 37 | const CENTER = 'h'; |
| 38 | const RIGHT = 'l'; |
| 39 | |
| 40 | const TOP = 'b'; |
| 41 | const MIDDLE = 'v'; |
| 42 | const BOTTOM = 't'; |
| 43 | |
| 44 | const BAR_BASE = 's'; |
| 45 | const BAR_CENTER = 'c'; |
| 46 | const BAR_TOP = 'e'; |
| 47 | |
| 48 | protected $marker_type = null; |
| 49 | |
| 50 | protected $points = null; |
| 51 | protected $position = null; |
| 52 | protected $size = 10; |
| 53 | protected $placement = null; |
| 54 | protected $text = null; |
| 55 | |
| 56 | protected $bar_sum = false; |
| 57 | |
| 58 | /** |
| 59 | * Create a new text marker. |
| 60 | * |
| 61 | * @param $marker_type (enum) |
| 62 | * One of the text marker type. The library provides a set of constant for |
| 63 | * currently implemented text marker. However, this parameter is not checked |
| 64 | * so it is open for other values the API may add in the future. |
| 65 | * - GoogleChartTextMarker::FLAG |
| 66 | * - GoogleChartTextMarker::TEXT |
| 67 | * - GoogleChartTextMarker::ANNOTATION |
| 68 | * - GoogleChartTextMarker::VALUE |
| 69 | * |
| 70 | * @param $text (string) |
| 71 | * For FLAG, TEXT and ANNOTATION markers, specify the text to be displayed. |
| 72 | * Ignored for VALUE marker. |
| 73 | */ |
| 74 | public function __construct($marker_type = self::VALUE, $text = null) |
| 75 | { |
| 76 | $this->marker_type = $marker_type; |
| 77 | $this->text = $text; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Set the text for FLAG, TEXT and ANNOTATION markers. |
| 82 | * |
| 83 | * @param $text (string) |
| 84 | * @return $this |
| 85 | */ |
| 86 | public function setText($text) |
| 87 | { |
| 88 | $this->text = $text; |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @name Position |
| 94 | * Thoses functions are mutually exclusive (if you call more than one of them, |
| 95 | * only the last one will be remembered). |
| 96 | */ |
| 97 | //@{ |
| 98 | /** |
| 99 | * Displays the marker at a fixed position (@c x:y format). |
| 100 | * |
| 101 | * When a marker has a fixed position, you MUST NOT associate it to a data |
| 102 | * serie (with setData()). Otherwise, the fixed position will be ignored. |
| 103 | * |
| 104 | * @note Fixed position is not supported for VALUE markers. |
| 105 | */ |
| 106 | public function setFixedPosition($x, $y) |
| 107 | { |
| 108 | if ( $x < 0 || $x > 1 || ! is_numeric($x) ) |
| 109 | throw new InvalidArgumentException('Invalid x position (must be between 0 and 1)'); |
| 110 | if ( $y < 0 || $y > 1 || ! is_numeric($y) ) |
| 111 | throw new InvalidArgumentException('Invalid y position (must be between 0 and 1)'); |
| 112 | |
| 113 | if ( $this->marker_type === self::VALUE ) |
| 114 | throw new LogicException('Fixed position is not supported for VALUE marker.'); |
| 115 | |
| 116 | $this->position = array( |
| 117 | 'x' => $x, |
| 118 | 'y' => $y |
| 119 | ); |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Displays only the selected point (@c n.d format). |
| 125 | * |
| 126 | * @param $point (int) The index of the point to display (0 based) in the data serie |
| 127 | * @return $this |
| 128 | */ |
| 129 | public function setPoint($point) |
| 130 | { |
| 131 | $this->points = $point; |
| 132 | return $this; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Displays a range of points (@c start:end:n or @c -n format). |
| 137 | * |
| 138 | * @return $this |
| 139 | */ |
| 140 | public function setPoints($start = null, $end = null, $step = null) |
| 141 | { |
| 142 | if ( $this->points['start'] === null && $this->points['end'] === null && $this->points['step'] === null ) { |
| 143 | $this->points = null; |
| 144 | } |
| 145 | |
| 146 | $this->points = array( |
| 147 | 'start' => $start, |
| 148 | 'end' => $end, |
| 149 | 'step' => $step |
| 150 | ); |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Displays every nth points (@c -n format). |
| 156 | * |
| 157 | * @return $this |
| 158 | */ |
| 159 | public function setStep($step) |
| 160 | { |
| 161 | $this->points = array( |
| 162 | 'start' => null, |
| 163 | 'end' => null, |
| 164 | 'step' => $step |
| 165 | ); |
| 166 | return $this; |
| 167 | } |
| 168 | //@} |
| 169 | |
| 170 | /** |
| 171 | * Set the size. |
| 172 | * |
| 173 | * @param $size (int) |
| 174 | * @return $this |
| 175 | */ |
| 176 | public function setSize($size) |
| 177 | { |
| 178 | $this->size = $size; |
| 179 | return $this; |
| 180 | } |
| 181 | |
| 182 | public function setPlacement($horizontal_placement = null, $vertical_placement = null, |
| 183 | $horizontal_offset = null, $vertical_offset = null, $bar_relative_placement = null) |
| 184 | { |
| 185 | $this->placement = array( |
| 186 | 'horizontal_placement' => $horizontal_placement, |
| 187 | 'vertical_placement' => $vertical_placement, |
| 188 | 'horizontal_offset' => $horizontal_offset, |
| 189 | 'vertical_offset' => $vertical_offset, |
| 190 | 'bar_relative_placement' => $bar_relative_placement |
| 191 | ); |
| 192 | return $this; |
| 193 | } |
| 194 | |
| 195 | public function getPlacement($bar_chart = false) |
| 196 | { |
| 197 | $str = ''; |
| 198 | |
| 199 | $str .= $this->placement['horizontal_placement'].$this->placement['vertical_placement']; |
| 200 | |
| 201 | if ( $bar_chart ) { |
| 202 | $str .= $this->placement['bar_relative_placement']; |
| 203 | } |
| 204 | |
| 205 | $str .= ':'.$this->placement['horizontal_offset'].':'.$this->placement['vertical_offset']; |
| 206 | $str = trim($str,':'); |
| 207 | return $str; |
| 208 | } |
| 209 | |
| 210 | public function setBarSum($sum) |
| 211 | { |
| 212 | $this->bar_sum = (bool) $sum; |
| 213 | return $this; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Compute the parameter value. |
| 218 | * |
| 219 | * @note For internal use only. |
| 220 | * @param $index (int) index of the data serie. |
| 221 | * @return string |
| 222 | */ |
| 223 | public function compute($index, $chart_type = null) |
| 224 | { |
| 225 | if ( $chart_type[0] == 'b' && $this->bar_sum ) { |
| 226 | $index = -1; |
| 227 | } |
| 228 | |
| 229 | if ( $index === null ) { |
| 230 | if ( $this->position === null ) { |
| 231 | throw new LogicException('Text marker requires one data serie or requires to have a fixed position.'); |
| 232 | } |
| 233 | |
| 234 | // Fixed position marker (x:y format) |
| 235 | $str = '@'; |
| 236 | $points = $this->position['x'].':'.$this->position['y']; |
| 237 | } |
| 238 | else { |
| 239 | $str = ''; |
| 240 | // Default = all (-1 format) |
| 241 | if ( $this->points === null ) { |
| 242 | $points = '-1'; |
| 243 | } |
| 244 | // Only one point (n.d format) |
| 245 | elseif ( ! is_array($this->points) ) { |
| 246 | $points = number_format($this->points,1); |
| 247 | } |
| 248 | // Step only (-n format) |
| 249 | elseif ( $this->points['start'] === null && $this->points['end'] === null ) { |
| 250 | $points = '-'.$this->points['step']; |
| 251 | } |
| 252 | // Serie (start:end:n) |
| 253 | else { |
| 254 | $points = $this->points['start'].':'.$this->points['end'].':'.$this->points['step']; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | $str .= $this->marker_type; |
| 259 | if ( $this->marker_type === self::VALUE ) { |
| 260 | |
| 261 | } |
| 262 | else { |
| 263 | $str .= str_replace(',','\,',$this->text); |
| 264 | } |
| 265 | |
| 266 | $str .= sprintf( |
| 267 | ',%s,%d,%s,%s', |
| 268 | $this->color, |
| 269 | $index, |
| 270 | $points, |
| 271 | $this->size |
| 272 | ); |
| 273 | |
| 274 | if ( $this->z_order !== null ) { |
| 275 | $str .= ','.$this->z_order; |
| 276 | } |
| 277 | |
| 278 | if ( $this->placement !== null ) { |
| 279 | $tmp = $this->getPlacement($chart_type !== null && $chart_type[0] === 'b'); |
| 280 | if ( $tmp ) { |
| 281 | if ( $this->z_order === null ) { |
| 282 | $str .= ','; // Add an empty z-order |
| 283 | } |
| 284 | |
| 285 | $str .= ','.$tmp; |
| 286 | } |
| 287 | } |
| 288 | return $str; |
| 289 | } |
| 290 | } |
| 291 |