| 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 Shape Marker. |
| 17 |
* |
| 18 |
* This class implement the Shape Marker feature (@c chm) |
| 19 |
* |
| 20 |
* @par Example |
| 21 |
* @include marker_shape.php |
| 22 |
* |
| 23 |
* @see http://code.google.com/apis/chart/docs/chart_params.html#gcharts_shape_markers |
| 24 |
*/ |
| 25 |
class GoogleChartShapeMarker extends GoogleChartMarker |
| 26 |
{ |
| 27 |
const ARROW = 'a'; |
| 28 |
const CROSS = 'c'; |
| 29 |
//~ const RECTANGLE = 'C'; |
| 30 |
const DIAMOND = 'd'; |
| 31 |
//~ const ERROR_BAR = 'E'; |
| 32 |
const CIRCLE = 'o'; |
| 33 |
const SQUARE = 's'; |
| 34 |
const X = 'x'; |
| 35 |
|
| 36 |
protected $shape = null; |
| 37 |
|
| 38 |
protected $points = null; |
| 39 |
protected $position = null; |
| 40 |
|
| 41 |
protected $size = '10'; |
| 42 |
|
| 43 |
protected $border = null; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
* |
| 48 |
* @see http://code.google.com/apis/chart/docs/chart_params.html#gcharts_shape_markers |
| 49 |
* @param $shape You can specify the shape of the marker. |
| 50 |
*/ |
| 51 |
public function __construct($shape = self::CIRCLE) |
| 52 |
{ |
| 53 |
$this->shape = $shape; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @name Position |
| 58 |
* Thoses functions are mutually exclusive (if you call more than one of them, |
| 59 |
* only the last one will be remembered). |
| 60 |
*/ |
| 61 |
//@{ |
| 62 |
/** |
| 63 |
* Set a fixed position for the marker. |
| 64 |
* |
| 65 |
* When a marker has a fixed position, you MUST NOT associate it to a data |
| 66 |
* serie (with setData()). Otherwise, the fixed position will be ignored. |
| 67 |
* |
| 68 |
* @param $x (float) between 0 and 1 |
| 69 |
* @param $y (float) between 0 and 1 |
| 70 |
* @return $this |
| 71 |
*/ |
| 72 |
public function setFixedPosition($x, $y) |
| 73 |
{ |
| 74 |
if ( $x < 0 || $x > 1 || ! is_numeric($x) ) |
| 75 |
throw new InvalidArgumentException('Invalid x position (must be between 0 and 1)'); |
| 76 |
if ( $y < 0 || $y > 1 || ! is_numeric($y) ) |
| 77 |
throw new InvalidArgumentException('Invalid y position (must be between 0 and 1)'); |
| 78 |
|
| 79 |
$this->position = array( |
| 80 |
'x' => $x, |
| 81 |
'y' => $y |
| 82 |
); |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Displays only the selected point. |
| 88 |
* |
| 89 |
* @param $point (int) The index of the point to display (0 based) in the data serie |
| 90 |
* @return $this |
| 91 |
*/ |
| 92 |
public function setPoint($point) |
| 93 |
{ |
| 94 |
$this->points = $point; |
| 95 |
return $this; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Displays a range of points (@c start:end:n or @c -n format). |
| 100 |
* |
| 101 |
* @return $this |
| 102 |
*/ |
| 103 |
public function setPoints($start = null, $end = null, $step = null) |
| 104 |
{ |
| 105 |
if ( $this->points['start'] === null && $this->points['end'] === null && $this->points['step'] === null ) { |
| 106 |
$this->points = null; |
| 107 |
} |
| 108 |
|
| 109 |
$this->points = array( |
| 110 |
'start' => $start, |
| 111 |
'end' => $end, |
| 112 |
'step' => $step |
| 113 |
); |
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Displays every nth points (@c -n format). |
| 119 |
* |
| 120 |
* @return $this |
| 121 |
*/ |
| 122 |
public function setStep($step) |
| 123 |
{ |
| 124 |
$this->points = array( |
| 125 |
'start' => null, |
| 126 |
'end' => null, |
| 127 |
'step' => $step |
| 128 |
); |
| 129 |
return $this; |
| 130 |
} |
| 131 |
//@} |
| 132 |
|
| 133 |
/** |
| 134 |
* Set the size of the shape. |
| 135 |
* |
| 136 |
* @param $size (int) |
| 137 |
* @return $this |
| 138 |
*/ |
| 139 |
public function setSize($size) |
| 140 |
{ |
| 141 |
$this->size = $size; |
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Set a border to the shape. |
| 147 |
* |
| 148 |
* To create a border to a shape with Google Chart API, you need to create |
| 149 |
* another similar marker below the first one, with a different color |
| 150 |
* and a slightly bigger size. |
| 151 |
* |
| 152 |
* This function does the job for you. Just specify a color and the size of |
| 153 |
* the border, and it will create the second marker automatically. |
| 154 |
* |
| 155 |
* @since 0.4 |
| 156 |
* |
| 157 |
* @param $size (int) size of the border (default is 2) |
| 158 |
* @param $color (string) a color in RRGGBB format (default is white) |
| 159 |
* @return $this |
| 160 |
*/ |
| 161 |
public function setBorder($size = 2, $color = 'ffffff') |
| 162 |
{ |
| 163 |
$this->border = array( |
| 164 |
'size' => $size, |
| 165 |
'color' => $color |
| 166 |
); |
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @internal |
| 172 |
*/ |
| 173 |
public function compute($index, $chart_type = null) |
| 174 |
{ |
| 175 |
if ( $index === null ) { |
| 176 |
if ( $this->position === null ) { |
| 177 |
throw new LogicException('Shape marker requires one data serie or requires to have a fixed position.'); |
| 178 |
} |
| 179 |
|
| 180 |
// Fixed position marker (x:y format) |
| 181 |
$str = '@'; |
| 182 |
$points = $this->position['x'].':'.$this->position['y']; |
| 183 |
} |
| 184 |
else { |
| 185 |
$str = ''; |
| 186 |
// Default = all (-1 format) |
| 187 |
if ( $this->points === null ) { |
| 188 |
$points = '-1'; |
| 189 |
} |
| 190 |
// Only one point (n.d format) |
| 191 |
elseif ( ! is_array($this->points) ) { |
| 192 |
$points = number_format($this->points,1); |
| 193 |
} |
| 194 |
// Step only (-n format) |
| 195 |
elseif ( $this->points['start'] === null && $this->points['end'] === null ) { |
| 196 |
$points = '-'.$this->points['step']; |
| 197 |
} |
| 198 |
// Serie (start:end:n) |
| 199 |
else { |
| 200 |
$points = $this->points['start'].':'.$this->points['end'].':'.$this->points['step']; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if ( $this->border !== null ) { |
| 205 |
$str .= sprintf( |
| 206 |
'%s,%s,%d,%s,%s|', |
| 207 |
$this->shape, |
| 208 |
$this->border['color'], |
| 209 |
$index, |
| 210 |
$points, |
| 211 |
$this->size + $this->border['size'] |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
$str .= sprintf( |
| 216 |
'%s,%s,%d,%s,%s', |
| 217 |
$this->shape, |
| 218 |
$this->color, |
| 219 |
$index, |
| 220 |
$points, |
| 221 |
$this->size |
| 222 |
); |
| 223 |
|
| 224 |
if ( $this->z_order !== null ) { |
| 225 |
$str .= ','.$this->z_order; |
| 226 |
} |
| 227 |
return $str; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** @example marker_shape_fixed_position.php |
| 232 |
* An example of a shape marker with fixed position |
| 233 |
*/ |
| 234 |
|