Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/*
3
pBubble - class to draw bubble charts
4
 
5
Version     : 2.1.4
6
Made by     : Jean-Damien POGOLOTTI
7
Last Update : 19/01/2014
8
 
9
This file can be distributed under the license you can find at :
10
 
11
http://www.pchart.net/license
12
 
13
You can find the whole class documentation on the pChart web site.
14
*/
15
define("BUBBLE_SHAPE_ROUND", 700001);
16
define("BUBBLE_SHAPE_SQUARE", 700002);
17
 
18
/* pBubble class definition */
19
class pBubble
20
{
21
	var $pChartObject;
22
	var $pDataObject;
23
	/* Class creator */
24
	function __construct($pChartObject, $pDataObject)
25
	{
26
		$this->pChartObject = $pChartObject;
27
		$this->pDataObject = $pDataObject;
28
	}
29
 
30
	/* Prepare the scale */
31
	function bubbleScale($DataSeries, $WeightSeries)
32
	{
33
		(!is_array($DataSeries)) AND $DataSeries = [$DataSeries];
34
		(!is_array($WeightSeries)) AND $WeightSeries = [$WeightSeries];
35
 
36
		/* Parse each data series to find the new min & max boundaries to scale */
37
		$NewPositiveSerie = [];
38
		$NewNegativeSerie = [];
39
		$MaxValues = 0;
40
		$LastPositive = 0;
41
		$LastNegative = 0;
42
		foreach($DataSeries as $Key => $SerieName) {
43
			$SerieWeightName = $WeightSeries[$Key];
44
			$this->pDataObject->setSerieDrawable($SerieWeightName, FALSE);
45
			if (count($this->pDataObject->Data["Series"][$SerieName]["Data"]) > $MaxValues) {
46
				$MaxValues = count($this->pDataObject->Data["Series"][$SerieName]["Data"]);
47
			}
48
 
49
			foreach($this->pDataObject->Data["Series"][$SerieName]["Data"] as $Key => $Value) {
50
				if ($Value >= 0) {
51
					$BubbleBounds = $Value + $this->pDataObject->Data["Series"][$SerieWeightName]["Data"][$Key];
52
					if (!isset($NewPositiveSerie[$Key])) {
53
						$NewPositiveSerie[$Key] = $BubbleBounds;
54
					} elseif ($NewPositiveSerie[$Key] < $BubbleBounds) {
55
						$NewPositiveSerie[$Key] = $BubbleBounds;
56
					}
57
 
58
					$LastPositive = $BubbleBounds;
59
				} else {
60
					$BubbleBounds = $Value - $this->pDataObject->Data["Series"][$SerieWeightName]["Data"][$Key];
61
					if (!isset($NewNegativeSerie[$Key])) {
62
						$NewNegativeSerie[$Key] = $BubbleBounds;
63
					} elseif ($NewNegativeSerie[$Key] > $BubbleBounds) {
64
						$NewNegativeSerie[$Key] = $BubbleBounds;
65
					}
66
 
67
					$LastNegative = $BubbleBounds;
68
				}
69
			}
70
		}
71
 
72
		/* Check for missing values and all the fake positive serie */
73
		if (count($NewPositiveSerie) > 0) //if ( $NewPositiveSerie != "" )
74
		{
75
			for ($i = 0; $i < $MaxValues; $i++) {
76
				if (!isset($NewPositiveSerie[$i])) {
77
					$NewPositiveSerie[$i] = $LastPositive;
78
				}
79
			}
80
 
81
			$this->pDataObject->addPoints($NewPositiveSerie, "BubbleFakePositiveSerie");
82
		}
83
 
84
		/* Check for missing values and all the fake negative serie */
85
		if (count($NewNegativeSerie) > 0) // if ( $NewNegativeSerie != "" )
86
		{
87
			for ($i = 0; $i < $MaxValues; $i++) {
88
				if (!isset($NewNegativeSerie[$i])) {
89
					$NewNegativeSerie[$i] = $LastNegative;
90
				}
91
			}
92
 
93
			$this->pDataObject->addPoints($NewNegativeSerie, "BubbleFakeNegativeSerie");
94
		}
95
	}
96
 
97
	function resetSeriesColors()
98
	{
99
		$Data = $this->pDataObject->getData();
100
		$Palette = $this->pDataObject->getPalette();
101
		$ID = 0;
102
		foreach($Data["Series"] as $SerieName => $SeriesParameters) {
103
			if ($SeriesParameters["isDrawable"]) {
104
				$this->pDataObject->Data["Series"][$SerieName]["Color"] = [
105
					"R" => $Palette[$ID]["R"],
106
					"G" => $Palette[$ID]["G"],
107
					"B" => $Palette[$ID]["B"],
108
					"Alpha" => $Palette[$ID]["Alpha"]
109
				];
110
				$ID++;
111
			}
112
		}
113
	}
114
 
115
	/* Prepare the scale */
116
	function drawBubbleChart($DataSeries, $WeightSeries, array $Format = [])
117
	{
118
		$ForceAlpha = VOID;
119
		$DrawBorder = TRUE;
120
		$BorderWidth = 1;
121
		$Shape = BUBBLE_SHAPE_ROUND;
122
		$Surrounding = NULL;
123
		$BorderR = 0;
124
		$BorderG = 0;
125
		$BorderB = 0;
126
		$BorderAlpha = 30;
127
		$RecordImageMap = FALSE;
128
 
129
		/* Override defaults */
130
		extract($Format);
131
 
132
		(!is_array($DataSeries)) AND $DataSeries = [$DataSeries];
133
		(!is_array($WeightSeries)) AND $WeightSeries = [$WeightSeries];
134
 
135
		$Data = $this->pDataObject->getData();
136
		$Palette = $this->pDataObject->getPalette();
137
		if (isset($Data["Series"]["BubbleFakePositiveSerie"])) {
138
			$this->pDataObject->setSerieDrawable("BubbleFakePositiveSerie", FALSE);
139
		}
140
 
141
		if (isset($Data["Series"]["BubbleFakeNegativeSerie"])) {
142
			$this->pDataObject->setSerieDrawable("BubbleFakeNegativeSerie", FALSE);
143
		}
144
 
145
		$this->resetSeriesColors();
146
		list($XMargin, $XDivs) = $this->pChartObject->scaleGetXSettings();
147
		foreach($DataSeries as $Key => $SerieName) {
148
			$AxisID = $Data["Series"][$SerieName]["Axis"];
149
			$Mode = $Data["Axis"][$AxisID]["Display"];
150
			$Format = $Data["Axis"][$AxisID]["Format"];
151
			$Unit = $Data["Axis"][$AxisID]["Unit"];
152
			$SerieDescription = (isset($Data["Series"][$SerieName]["Description"])) ? $Data["Series"][$SerieName]["Description"] : $SerieName;
153
			$XStep = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1 - $XMargin * 2) / $XDivs;
154
			$X = $this->pChartObject->GraphAreaX1 + $XMargin;
155
			$Y = $this->pChartObject->GraphAreaY1 + $XMargin;
156
			$Color = ["R" => $Palette[$Key]["R"],"G" => $Palette[$Key]["G"],"B" => $Palette[$Key]["B"],"Alpha" => $Palette[$Key]["Alpha"]];
157
			if ($ForceAlpha != VOID) {
158
				$Color["Alpha"] = $ForceAlpha;
159
			}
160
 
161
			if ($DrawBorder) {
162
				if ($BorderWidth != 1) {
163
					if ($Surrounding != NULL) {
164
						$BorderR = $Palette[$Key]["R"] + $Surrounding;
165
						$BorderG = $Palette[$Key]["G"] + $Surrounding;
166
						$BorderB = $Palette[$Key]["B"] + $Surrounding;
167
					} else {
168
						$BorderR = $BorderR;
169
						$BorderG = $BorderG;
170
						$BorderB = $BorderB;
171
					}
172
 
173
					if ($ForceAlpha != VOID) {
174
						$BorderAlpha = $ForceAlpha / 2;
175
					}
176
 
177
					$BorderColor = ["R" => $BorderR,"G" => $BorderG,"B" => $BorderB,"Alpha" => $BorderAlpha];
178
				} else {
179
					$Color["BorderAlpha"] = $BorderAlpha;
180
					if ($Surrounding != NULL) {
181
						$Color["BorderR"] = $Palette[$Key]["R"] + $Surrounding;
182
						$Color["BorderG"] = $Palette[$Key]["G"] + $Surrounding;
183
						$Color["BorderB"] = $Palette[$Key]["B"] + $Surrounding;
184
					} else {
185
						$Color["BorderR"] = $BorderR;
186
						$Color["BorderG"] = $BorderG;
187
						$Color["BorderB"] = $BorderB;
188
					}
189
 
190
					if ($ForceAlpha != VOID) {
191
						$Color["BorderAlpha"] = $ForceAlpha / 2;
192
					}
193
				}
194
			}
195
 
196
			foreach($Data["Series"][$SerieName]["Data"] as $iKey => $Point) {
197
				$Weight = $Point + $Data["Series"][$WeightSeries[$Key]]["Data"][$iKey];
198
				$PosArray = $this->pChartObject->scaleComputeY($Point, ["AxisID" => $AxisID]);
199
				$WeightArray = $this->pChartObject->scaleComputeY($Weight, ["AxisID" => $AxisID]);
200
				if ($Data["Orientation"] == SCALE_POS_LEFTRIGHT) {
201
					$XStep = ($XDivs == 0) ? 0 : ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1 - $XMargin * 2) / $XDivs;
202
					$Y = floor($PosArray);
203
					$CircleRadius = floor(abs($PosArray - $WeightArray) / 2);
204
					if ($Shape == BUBBLE_SHAPE_SQUARE) {
205
						if ($RecordImageMap) {
206
							$this->pChartObject->addToImageMap("RECT", floor($X - $CircleRadius) . "," . floor($Y - $CircleRadius) . "," . floor($X + $CircleRadius) . "," . floor($Y + $CircleRadius) , $this->pChartObject->toHTMLColor($Palette[$Key]["R"], $Palette[$Key]["G"], $Palette[$Key]["B"]) , $SerieDescription, $Data["Series"][$WeightSeries[$Key]]["Data"][$iKey]);
207
						}
208
 
209
						if ($BorderWidth != 1) {
210
							$this->pChartObject->drawFilledRectangle($X - $CircleRadius - $BorderWidth, $Y - $CircleRadius - $BorderWidth, $X + $CircleRadius + $BorderWidth, $Y + $CircleRadius + $BorderWidth, $BorderColor);
211
							$this->pChartObject->drawFilledRectangle($X - $CircleRadius, $Y - $CircleRadius, $X + $CircleRadius, $Y + $CircleRadius, $Color);
212
						} else {
213
							$this->pChartObject->drawFilledRectangle($X - $CircleRadius, $Y - $CircleRadius, $X + $CircleRadius, $Y + $CircleRadius, $Color);
214
						}
215
					} elseif ($Shape == BUBBLE_SHAPE_ROUND) {
216
						if ($RecordImageMap) {
217
							$this->pChartObject->addToImageMap("CIRCLE", floor($X) . "," . floor($Y) . "," . floor($CircleRadius) , $this->pChartObject->toHTMLColor($Palette[$Key]["R"], $Palette[$Key]["G"], $Palette[$Key]["B"]) , $SerieDescription, $Data["Series"][$WeightSeries[$Key]]["Data"][$iKey]);
218
						}
219
 
220
						if ($BorderWidth != 1) {
221
							$this->pChartObject->drawFilledCircle($X, $Y, $CircleRadius + $BorderWidth, $BorderColor);
222
							$this->pChartObject->drawFilledCircle($X, $Y, $CircleRadius, $Color);
223
						} else {
224
							$this->pChartObject->drawFilledCircle($X, $Y, $CircleRadius, $Color);
225
						}
226
					}
227
 
228
					$X = $X + $XStep;
229
 
230
				} elseif ($Data["Orientation"] == SCALE_POS_TOPBOTTOM) {
231
					if ($XDivs == 0) {
232
						$XStep = 0;
233
					} else {
234
						$XStep = ($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1 - $XMargin * 2) / $XDivs;
235
					}
236
 
237
					$X = floor($PosArray);
238
					$CircleRadius = floor(abs($PosArray - $WeightArray) / 2);
239
					if ($Shape == BUBBLE_SHAPE_SQUARE) {
240
						if ($RecordImageMap) {
241
							$this->pChartObject->addToImageMap("RECT", floor($X - $CircleRadius) . "," . floor($Y - $CircleRadius) . "," . floor($X + $CircleRadius) . "," . floor($Y + $CircleRadius) , $this->pChartObject->toHTMLColor($Palette[$Key]["R"], $Palette[$Key]["G"], $Palette[$Key]["B"]) , $SerieDescription, $Data["Series"][$WeightSeries[$Key]]["Data"][$iKey]);
242
						}
243
 
244
						if ($BorderWidth != 1) {
245
							$this->pChartObject->drawFilledRectangle($X - $CircleRadius - $BorderWidth, $Y - $CircleRadius - $BorderWidth, $X + $CircleRadius + $BorderWidth, $Y + $CircleRadius + $BorderWidth, $BorderColor);
246
							$this->pChartObject->drawFilledRectangle($X - $CircleRadius, $Y - $CircleRadius, $X + $CircleRadius, $Y + $CircleRadius, $Color);
247
						} else {
248
							$this->pChartObject->drawFilledRectangle($X - $CircleRadius, $Y - $CircleRadius, $X + $CircleRadius, $Y + $CircleRadius, $Color);
249
						}
250
					} elseif ($Shape == BUBBLE_SHAPE_ROUND) {
251
						if ($RecordImageMap) {
252
							$this->pChartObject->addToImageMap("CIRCLE", floor($X) . "," . floor($Y) . "," . floor($CircleRadius) , $this->pChartObject->toHTMLColor($Palette[$Key]["R"], $Palette[$Key]["G"], $Palette[$Key]["B"]) , $SerieDescription, $Data["Series"][$WeightSeries[$Key]]["Data"][$iKey]);
253
						}
254
 
255
						if ($BorderWidth != 1) {
256
							$this->pChartObject->drawFilledCircle($X, $Y, $CircleRadius + $BorderWidth, $BorderColor);
257
							$this->pChartObject->drawFilledCircle($X, $Y, $CircleRadius, $Color);
258
						} else {
259
							$this->pChartObject->drawFilledCircle($X, $Y, $CircleRadius, $Color);
260
						}
261
					}
262
 
263
					$Y = $Y + $XStep;
264
				}
265
			}
266
		}
267
	}
268
 
269
	function writeBubbleLabel($SerieName, $SerieWeightName, $Points, array $Format = [])
270
	{
271
 
272
		$Data = $this->pDataObject->getData();
273
		$Palette = $this->pDataObject->getPalette();
274
		if (!isset($Data["Series"][$SerieName]) || !isset($Data["Series"][$SerieWeightName])) {
275
			return 0;
276
		}
277
 
278
		$OverrideTitle = isset($Format["OverrideTitle"]) ? $Format["OverrideTitle"] : NULL;
279
		$DrawPoint = isset($Format["DrawPoint"]) ? $Format["DrawPoint"] : LABEL_POINT_BOX;
280
		(!is_array($Points)) AND $Points = [$Points];
281
 
282
		list($XMargin, $XDivs) = $this->pChartObject->scaleGetXSettings();
283
		$AxisID = $Data["Series"][$SerieName]["Axis"];
284
		$AxisMode = $Data["Axis"][$AxisID]["Display"];
285
		$AxisFormat = $Data["Axis"][$AxisID]["Format"];
286
		$AxisUnit = $Data["Axis"][$AxisID]["Unit"];
287
		$XStep = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1 - $XMargin * 2) / $XDivs;
288
		$X = $this->pChartObject->GraphAreaX1 + $XMargin;
289
		$Y = $this->pChartObject->GraphAreaY1 + $XMargin;
290
		$Color = [
291
			"R" => $Data["Series"][$SerieName]["Color"]["R"],
292
			"G" => $Data["Series"][$SerieName]["Color"]["G"],
293
			"B" => $Data["Series"][$SerieName]["Color"]["B"],
294
			"Alpha" => $Data["Series"][$SerieName]["Color"]["Alpha"]
295
		];
296
		foreach($Points as $Key => $Point) {
297
			$Value = $Data["Series"][$SerieName]["Data"][$Point];
298
			$PosArray = $this->pChartObject->scaleComputeY($Value, ["AxisID" => $AxisID]);
299
			if (isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Point])) {
300
				$Abscissa = $Data["Series"][$Data["Abscissa"]]["Data"][$Point] . " : ";
301
			} else {
302
				$Abscissa = "";
303
			}
304
 
305
			$Value = $this->pChartObject->scaleFormat($Value, $AxisMode, $AxisFormat, $AxisUnit);
306
			$Weight = $Data["Series"][$SerieWeightName]["Data"][$Point];
307
			$Caption = $Abscissa . $Value . " / " . $Weight;
308
			$Description = (isset($Data["Series"][$SerieName]["Description"])) ? $Data["Series"][$SerieName]["Description"] : "No description";
309
			$Series = ["Format" => $Color,"Caption" => $Caption];
310
 
311
			if ($Data["Orientation"] == SCALE_POS_LEFTRIGHT) {
312
				$XStep = ($XDivs == 0) ? 0 : ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1 - $XMargin * 2) / $XDivs;
313
				$X = floor($X + $Point * $XStep);
314
				$Y = floor($PosArray);
315
			} else {
316
				$YStep = ($XDivs == 0) ? 0 :($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1 - $XMargin * 2) / $XDivs;
317
				$X = floor($PosArray);
318
				$Y = floor($Y + $Point * $YStep);
319
			}
320
 
321
			if ($DrawPoint == LABEL_POINT_CIRCLE) {
322
				$this->pChartObject->drawFilledCircle($X, $Y, 3, ["R" => 255,"G" => 255,"B" => 255,"BorderR" => 0,"BorderG" => 0,"BorderB" => 0]);
323
			} elseif ($DrawPoint == LABEL_POINT_BOX) {
324
				$this->pChartObject->drawFilledRectangle($X - 2, $Y - 2, $X + 2, $Y + 2, ["R" => 255,"G" => 255,"B" => 255,"BorderR" => 0,	"BorderG" => 0,	"BorderB" => 0]);
325
			}
326
 
327
			$this->pChartObject->drawLabelBox($X, $Y - 3, $Description, $Series, $Format);
328
		}
329
	}
330
}
331
 
332
?>