TinyPortal

Development => Block Codes => Topic started by: Ianedres on April 26, 2008, 02:39:48 AM

Title: Google Pie Chart For Total Hits By Year
Post by: Ianedres on April 26, 2008, 02:39:48 AM
Building on my last snippet (http://www.tinyportal.net/index.php?topic=23572.0), this will dynamically build a pie chart using Google's Chart API. It does not require an API key to display the image.

Once you set up the array for previous year's totals, the script calculates the total hits, hits for current year, percentage of all years collectively, and then outputs the Google Chart URL code for those values in a pie chart.

(https://www.tinyportal.net/proxy.php?request=http%3A%2F%2Fchart.apis.google.com%2Fchart%3Fcht%3Dp3%26amp%3Bchs%3D400x200%26amp%3Bchd%3Dt%3A24%2C73%2C3%26amp%3Bchl%3D2008%257C2007%257C2006%26amp%3Bchco%3D00ff00%2Cffffff%26amp%3Bchf%3Dbg%2Cs%2Ce0e0e0%26amp%3Bchtt%3DTotal%2BHits%3A%2B967%2C887%26amp%3Bchts%3D000000&hash=4d2e2bc1bbe4b4e58873c91baae4129b85012591)

You should not have to update the script other than once a year to save the prior year's totals in the array. The script will add the years to the labels as you add them to the array.

The line $year = array('2007'=>'710474', '2006' => '25505'); corresponds to 710,474 hits for 2007, and 25,505 hits for 2006. To add another year, you simply need to follow the format- preceded with a comma, enter the year and hits (no commas) with single quotes; i.e. , '2005' => '12633' before the last parenthesis.

Obviously, you can have a cluttered or clipped chart if the image size is too small or out of a suitable aspect range. Color selection can also affect chart legibility; as of right now, the label color can not be changed (that I am aware of) and does not render well against dark backgrounds.

Hope someone can use something like this.
Guaranteed 90 days or 90 feet, whichever comes first.

/*
Display pie chart of total hits by year using Google Chart API
April 25, 2008
Tim Antley - www.bayoumx.com

For Use With SMF & TinyPortal

Uses code from SMF's source to retrieve total numbers of hits
Calculates percentage of hits each year against total hits
Google Charts API plots pie chart image based on above info

Option to display chart for admins only
Option to display chart on home page only or all pages.
Provided user variables for chart display customization
*/

// user variables
$width = '400'; // chart width
$height = '200'; // chart height
$align = 'center'; // chart alignment (left/right/center)
$bg = 'e0e0e0'; // (rrggbb) background color
$color1 = '00ff00'; // (rrggbb) pie color 1
$color2 = 'ffffff'; // (rrggbb) pie color 2
$t_color = '000000'; // (rrggbb) title text color

// data information in array format; 'year' => '# of hits', etc.
// for accurate results, include all years prior to this year
// script will calculate hits for the current year

$year = array('2007'=>'710474', '2006' => '25505');

// end of variables

// link to Google's Chart API
$chart_url = 'http://chart.apis.google.com/chart';

// retrieve total number of hits from SMF database
global $txt, $scripturl, $db_prefix, $modSettings, $user_info, $context;

$result = db_query("
SELECT SUM(hits) AS hits
FROM {$db_prefix}log_activity", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
$context['num_hits'] = $row['hits'];
$t_hits = $context['num_hits']; // $t_hits now equals total hits

mysql_free_result($result);

// calculate previous year's hits
foreach($year as $key=>$value)
{
$prev_year_hits = $prev_year_hits + $value;
}

// calculate current year's hits
$current_year = $t_hits - ($prev_year_hits);
$this_year = date("Y");
$year[$this_year] = $current_year;

// sort the years in reverse
krsort($year);

// get values in array for Google's URL requirements
foreach($year as $key=>$value)
{
$chart_labels .= $key.'|';
$chart_data .= (round(($value/$t_hits),2)*100).',';
}

// trim off extraneous connectors from last step
$chart_data = 't:'.trim($chart_data,',');
$chart_labels = trim($chart_labels, '|');

// build chart title; use plus sign for space
$chtt = 'Total+Hits:+'.number_format($t_hits).'&chts='.$t_color;

// output div for chart - align using variable setting
echo '<hr><div align="'.$align.'" id="hit_chart">';
echo '<img src="'.$chart_url.'?cht=p3&chs='.$width.'x'.$height.'&chd='.$chart_data.'&chl='.
$chart_labels.'&chco='.$color1.','.$color2.'&chf=bg,s,'.$bg.'&chtt='.$chtt.'">';
echo '</div>';
Title: Re: Google Pie Chart For Total Hits By Year
Post by: pramod.513 on August 20, 2008, 04:47:57 AM
OK IT IS FINE