File: chart.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (318 lines) | stat: -rw-r--r-- 10,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/* 	OpenDb - Open Media Lending Database
	Copyright (C) 2001,2002 by Jason Pell

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/**
* 	Note that this file/feature requires GD libraries installed on the php server
* 
* @param $sortorder 'asc' or 'desc'
* @param $chartType 'barchart' or 'piechart'
* @param $imgType 'gif', 'jpg', 'png'
* @param $chartOptions Array of options for chart, options include:
* 			striped=>[TRUE|FALSE]
* 			12oclock=>[TRUE|FALSE]
* 			threshold=>a number
* 
* @param $graphCfg = array(
					'image-size'=>$HTTP_VARS['image-size'],
					'font-size'=>$HTTP_VARS['font-size'],
					'transparent'=>$HTTP_VARS['transparent'],
					'text-color'=>$HTTP_VARS['text-color'],
					'caption-color'=>$HTTP_VARS['caption-color'],
					'light-color'=>$HTTP_VARS['light-color'],
					'dark-color'=>$HTTP_VARS['dark-color'],
					'light-border-color'=>$HTTP_VARS['light-border-color'],
					'dark_border-color'=>$HTTP_VARS['dark_border-color'],
					'background-color'=>$HTTP_VARS['background-color']);
*/
function build_and_send_graph($data, $sortorder, $chartType, $chartOptions, $graphCfg, $imgType)
{
	// process data
	$items = count($data);

	if($items > 0 && !empty($sortorder))
	{
		// Sort categories by popularity.

		// FIXME (jpell): I will be honest I have no idea how this sort function
		// works.  I have just swapped around the operators!
		function index_1_asc_sort($a, $b)
		{
			if ($a[1] == $b[1])
				return($a[0] < $b[0]);	// Alphabetic on category if equal percentages
			else
				return($a[1] > $b[1]);	// Forwards sort
		}

		function index_1_desc_sort($a, $b)
		{
			if ($a[1] == $b[1])
				return($a[0] > $b[0]);	// Alphabetic on category if equal percentages
			else
				return($a[1] < $b[1]);	// Backwards sort
		}

		if($sortorder == 'asc')
			usort($data, 'index_1_asc_sort');
		else if($sortorder == 'desc')
			usort($data, 'index_1_desc_sort');
	}

	// size of pie-chart (not counting text borders)
	$imgsize = $graphCfg['image-size'];

	$fontsize = $graphCfg['font-size'];
	$fontheight = imagefontheight($fontsize);

	// Create a new image
	$side_margin = round($imgsize * 2/3);

	$top_margin      = $fontheight+1;
	$xsize = $imgsize + 2*$side_margin;
	$ysize = $imgsize + 2*$top_margin;

	$im = ImageCreate($xsize, $ysize);

	// Allocate the colors for the image
	// believe it or not, but the first color allocated becomes the default background color for the generated image, unless explicitly
	// set using ImageFilledRectangle!
	$bgcolor 		= ImageColorAllocate($im, hexdec(substr($graphCfg['background-color'],0,2)), hexdec(substr($graphCfg['background-color'],2,2)), hexdec(substr($graphCfg['background-color'],4,2)));
	$text_color	 	= ImageColorAllocate($im, hexdec(substr($graphCfg['text-color'],0,2)), hexdec(substr($graphCfg['text-color'],2,2)), hexdec(substr($graphCfg['text-color'],4,2)));
	$captions_color	= ImageColorAllocate($im, hexdec(substr($graphCfg['caption-color'],0,2)), hexdec(substr($graphCfg['caption-color'],2,2)), hexdec(substr($graphCfg['caption-color'],4,2)));
	$lt_color	 	= ImageColorAllocate($im, hexdec(substr($graphCfg['light-color'],0,2)), hexdec(substr($graphCfg['light-color'],2,2)), hexdec(substr($graphCfg['light-color'],4,2)));
	$dk_color	 	= ImageColorAllocate($im, hexdec(substr($graphCfg['dark-color'],0,2)), hexdec(substr($graphCfg['dark-color'],2,2)), hexdec(substr($graphCfg['dark-color'],4,2)));
	$lt_border 		= ImageColorAllocate($im, hexdec(substr($graphCfg['light-border-color'],0,2)), hexdec(substr($graphCfg['light-border-color'],2,2)), hexdec(substr($graphCfg['light-border-color'],4,2)));
	$dk_border 		= ImageColorAllocate($im, hexdec(substr($graphCfg['dark-border-color'],0,2)), hexdec(substr($graphCfg['dark-border-color'],2,2)), hexdec(substr($graphCfg['dark-border-color'],4,2)));

	// Do this if you want your background image to show through:
	if($graphCfg['transparent'] === TRUE)
	{
		imagecolortransparent($im, $bgcolor);
	}

	// color background
	//ImageFilledRectangle($im, 0, 0, $xsize, $ysize, $bgcolor);

	// $items == 0 in small databases (no items, or all items have no
	// category).  To reproduce, add a CD that freedb classifies as "noise"
	// (Barbie's dance albums).	 "Noise" isn't one of OpenDb's MUSICGENREs.

	// This conditional looks a little odd.	 It skips a massive section if
	// there are no items to draw.
	if($chartType == 'barchart')
	{
		if($items>0)
		{
			$sidegap = 10;			// pixels.
			$topgap = $sidegap;

			// An iterative process to determine the best box width.
			// It will perform only one loop, unless you have so many items that
			// the box width is smaller than the font height.  Then it will
			// recompute with fewer items.
			$items++;			// We undo this on the next line.
			do {
				$items--;
				$boxwidth = round(($xsize - ($sidegap*2)) / ($items + 1));
				$gapwidth = round($boxwidth / 10);
				if ($gapwidth < 1) $gapwidth = 1;
				$boxwidth = floor(($xsize - ($sidegap*2) - ($gapwidth * ($items-1))) / $items);
				$totalwidth = $boxwidth * $items + $gapwidth * ($items-1);
			} while ($items && $boxwidth < $fontheight + 2);

			// centre it:
			$sidegap = ($xsize - $totalwidth) / 2;
	
			// Find the largest value, so that we can draw it filling the chart.
			// This puts more information in the chart when all the values are
			// low (e.g. DVD categories hovering around 10%).
			$maxdata = 0;
			for( $ix = 0; $ix < $items; $ix++ )
			{
				if ($maxdata < $data[$ix][1])
				{
					$maxdata = $data[$ix][1] + 1;
				}
			}

			for( $ix=0; $ix < $items; $ix++ )
			{
				//$height = $data[$ix][1] * ($ysize - $topgap*2) / 100;
				$height = $data[$ix][1] * ($ysize - $topgap*2) / $maxdata;

				$x1 = $sidegap + ($ix*$boxwidth) + ($ix*$gapwidth);

				// ImageLine($im, $x1, $ysize - $top_margin, $x1, $ysize - $top_margin - $height, $dk_border );
				ImageFilledRectangle($im,
					 $x1, $ysize - $topgap - $height,
					 $x1+$boxwidth, $ysize - $topgap,
					 $lt_color);

				// Highlight it from the top left, shadow on bottom and right.
				ImageLine($im, $x1, $ysize - $topgap - $height, $x1, $ysize - $topgap, $lt_border);
				ImageLine($im, $x1, $ysize - $topgap - $height, $x1+$boxwidth, $ysize - $topgap - $height, $lt_border);
				ImageLine($im, $x1+$boxwidth, $ysize - $topgap - $height, $x1+$boxwidth, $ysize - $topgap, $dk_border);
				ImageLine($im, $x1, $ysize - $topgap, $x1+$boxwidth, $ysize - $topgap, $dk_border);

				$text = $data[$ix][0]." ".round($data[$ix][1])."%"; // show percent
				ImageStringUp($im, $fontsize, $x1 + $boxwidth/2 - $fontheight/2, $ysize - $topgap - 3, $text, $text_color);
			}
		}
		else //if($items>0)
		{
			// Do nothing, which will produce an empty graph.
		}
	}//if($chartType == 'barchart')
	else
	{
		// A pie chart.
		if(is_numeric($chartOptions['threshold']))// minimum wedge percent to omit from caption
			$threshold = $chartOptions['threshold'];
		else
			$threshold = 3;

		$radius = round($imgsize/2);

		$originx = $radius+$side_margin;
		$originy = $radius+$top_margin;

		// draw a circle
		ImageArc($im, $originx, $originy, $radius*2, $radius*2, 0, 360, $dk_border);

		// fill circle with color
		ImageFill($im, $originx, $originy, $lt_color);

		// GD-2 version of the above two calls.	 Damn PHP.
		// ImageFilledArc($im, $originx, $originy, $radius*2, $radius*2, 0, 360, $dk_border, IMG_ARC_PIE);

		if($chartOptions['12oclock'] !== FALSE)
		{
			// draw a wedge
			$last_angle = deg2rad(-90);
			// Draw line at 0 degrees if we have more than one item.
			if ($items > 1)
			{
				ImageLine($im,
						$originx, $originy,
						$originx, $originy - $radius + 1,
						$dk_border );
			}
		}
		else
		{
			$last_angle = deg2rad(0);
			if ($items > 1)
			{
				ImageLine($im,
						$originx, $originy,
						$originx + $radius - 1, $originy,
						$dk_border );
			}
		}

		if($chartOptions['striped'] !== FALSE)
		{
			// Draw every other pie wedge a different colour.
			$striped = TRUE;
		}
	
		for( $ix=0; $ix < $items; $ix++ )
		{
			$angle = deg2rad(($data[$ix][1]*3.6))+$last_angle;

			$x2 = ($radius-1)*cos($angle) + $originx;
			$y2 = ($radius-1)*sin($angle) + $originy;

			if($ix != $items-1) // don't draw wedge-line for 100%
				ImageLine($im, $originx, $originy, $x2, $y2, $dk_border );
	
			$mid_angle = ($angle-$last_angle)/2;
		
			// Fill every other wedge with a different colour:
			if (($ix % 2) && $striped)
			{
				ImageFillToBorder($im,
							$radius*0.9*cos($mid_angle+$last_angle) + $originx,
							$radius*0.9*sin($mid_angle+$last_angle) + $originy,
							$dk_border,
							$dk_color);
			}
		
			if($data[$ix][1]>$threshold ) // caption if over $threshold
			{
				$x1 = ($radius/2)*cos($mid_angle+$last_angle) + $originx;
				$y1 = ($radius/2)*sin($mid_angle+$last_angle) + $originy;

				$x2 = $radius*cos($mid_angle+$last_angle) + $originx;
				$y2 = $radius*sin($mid_angle+$last_angle) + $originy;

				ImageArc( $im, $x1, $y1, $imgsize/25, $imgsize/25, 0, 360, $captions_color);  // display caption
				ImageLine($im, $x1, $y1, $x2, $y2, $captions_color);

				$text = $data[$ix][0]." ".round($data[$ix][1])."%"; // show percent
				if($x1 > $originx)
				{
					ImageLine($im, $x2, $y2, $x2+$side_margin, $y2, $captions_color); // caption on right side
					if ($y2 > $originy) // Bottom half
						ImageString($im, $fontsize, $x2, $y2, $text, $text_color);
					else
						// Should use font height here:
						ImageString($im, $fontsize, $x2, $y2-15, $text, $text_color);
				}
				else
				{
					ImageLine($im, $x2, $y2, $x2-$side_margin, $y2, $captions_color); // caption on left side
					if ($y2 > $originy) // Bottom half
						ImageString($im, $fontsize, $x2-$side_margin, $y2, $text, $text_color);
					else
					{
						// Should use font height here:
						ImageString($im, $fontsize, $x2-$side_margin, $y2 - 15, $text, $text_color);
					}
				}
			}
			$last_angle = $angle;
		}
	}

	header("Pragma: no-cache");
	header("Expires: 0");
				
	switch($imgType)
	{
		case 'jpeg':
		case 'jpg':
			Header("Content-Type: image/jpeg");
			ImageJPEG($im);
			break;

		case 'gif'://not all sites support GIF!
			Header("Content-Type: image/gif");
			ImageGIF($im);
			break;

		case 'png':
		default:
			Header("Content-Type: image/png");
			ImagePNG($im);// send image as PNG to browser
	}

	// destroy image when done
	ImageDestroy($im);
}
?>