DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Randall has posted 3 posts at DZone. View Full User Profile

Google Charts Scatter Plot Showing Drupal Userpoints

08.25.2008
| 10285 views |
  • submit to reddit
        // description of your code here

Implementation of Google Charts API to create a scatterplot of Userpoints in Drupal

Chart URL: 
http://chart.apis.google.com/chart?chd=t%3A12.5%2C25%2C37.5%2C50%2C62.5%2C75%2C87.5%2C100%7C10.3%2C4.6%2C55.8%2C35.8%2C91.7%2C2.3%2C0.8%2C55.8%7C10.3%2C4.6%2C55.8%2C35.8%2C91.7%2C2.3%2C0.8%2C55.8&cht=s&chs=500x500&chtt=rgoya+has+558+flyDollars&chts=ff0000%2C25&chg=12.5%2C10%2C1%2C1&chm=o%2C00ff00%2C0%2C0%2C100%7Co%2Cff0000%2C0%2C8%2C55.8%7Cr%2Cffaaaa%2C0%2C0.288%2C0.298&chf=bg%2Cs%2CFFFFFF&chxr=0%2C0%2C8%7C1%2C0%2C1000&chxl=2%3A%7C+%7Cmembers%7C+%7C3%3A%7C+%7CflyDollars%7C+&chxt=x%2Cy%2Cx%2Cy&chxp=&chxs=0%2C000000%2C10%7C1%2C000000%2C10%7C2%2C0000ff%2C15%7C3%2C00ff00%2C15

<?php

/*  flyDiGiTs scatter plot */

//load user info
global $user;
user_load(array('uid' => $user->uid));
$uid = $user->uid;

//init some vars
$cnt = 0;
$totpts = 0;
$arrpts = array();
$arrmem = array();

//get the number of records in the userpoints table
//this is necessary because a query on the userpoints table returns only the first record
$query3 = "SELECT COUNT(*) FROM userpoints";
$data3 = mysql_query($query3)
or die(mysql_error());
$info3 = mysql_fetch_array( $data3 );
$sqlcnt = $info3[0];

//a scalar for the chart members (x) axis
//otherwise chart will use decimal segments (factor of ten)
//ie instead of 0 - [number of members] it will plot members on 0 - 10 scale
$scl = 10/($sqlcnt+1);

//get the points and fill some arrays
while ($cnt < $sqlcnt){
$cnt++;
$query = "SELECT points FROM userpoints WHERE pid = $cnt";
$data = mysql_query($query)
or die(mysql_error());
$info = mysql_fetch_array( $data );
$thepts = $info[0];
$arrpts[$cnt-1] = $thepts/10;
$totpts += $thepts;
$arrmem[$cnt] = $cnt*10*$scl;
}

//how many mambers? for average
$pcount = $cnt;

//get the current user's points
$query2 = "SELECT points FROM userpoints WHERE uid = $uid";
$data2 = mysql_query($query2)
or die(mysql_error());
$info2 = mysql_fetch_array( $data2 );
$solopts = $info2[0];

//create the individual user text
$userpts = $user->name . " has " . $solopts . " " . "flyDollars";

//round the average
$avgpts = round($totpts/$pcount);

//create the average and total text
$sumpts = " Average flyDollars = " . $avgpts. "<br  clear='all' />Total flyDollars = " . $totpts ;

//print the texts
print("<div class='chartstats'>" . $userpts . "<br clear='all' />" . $sumpts . "<br clear='all' /><br /><br /></div>");


//add one more data set for the current user
$xcnt = $cnt + 1;
$solopts = $solopts/10;
$arrmem[$xcnt] = $xcnt*10*$scl;
$arrpts[$cnt] = $solopts;

//debug
//$arr1 = implode(" ",$arrmem);
//$arr2 = implode(" ",$arrpts);
//print("<br>arrmem " . $arr1 . " arrpts " . $arr2  . "<br>");


//Drupal module chart constructor
$chart = array(
      '#chart_id' => 'flyscatter',
      '#title' => chart_title(t($userpts), 'ff0000', 25),
      '#type' => CHART_TYPE_SCATTER,
      '#size' => chart_size(500, 500),
);

$chart['#data']['members'] = $arrmem;
$chart['#data']['points'] = $arrpts;
$chart['#data']['count'] = $arrpts;

//x-axis = number of members + 1
$xstep = 100/$xcnt;
$grid = array($xstep,10,1,1);
$chart['#grid_lines'] = $grid;


//use same shape so user marker covers the default one
//first line is general marker, second line is current user marker
$chart['#shape_markers'][] = array('o','00ff00',0,0,100.0);
$chart['#shape_markers'][] = array('o','ff0000',0,$xcnt,$solopts);

//average bar specs
$avgbarstart = round($avgpts)/1000;
$avgbarend = $avgbarstart + .01;
$chart['#shape_markers'][] = array('r','ffaaaa',0,$avgbarstart,$avgbarend);

$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_range_label(0, $sqlcnt + 1);
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t(' '));    
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t('members'));
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t(' '));    
 
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][2][] = chart_mixed_axis_range_label(0, 1000);
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][3][] =  chart_mixed_axis_label(t(' '));
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][3][] =  chart_mixed_axis_label(t('flyDollars'));
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][3][] =  chart_mixed_axis_label(t(' '));


//label colors
$chart['#mixed_axis_label_styles'][] = array(0,'000000',10);
$chart['#mixed_axis_label_styles'][] = array(1,'000000',10);
$chart['#mixed_axis_label_styles'][] = array(2,'0000ff',15);
$chart['#mixed_axis_label_styles'][] = array(3,'00ff00',15);

echo chart_render($chart);


?>