add amendments metric
[contractdashboard.git] / lib / wordcloud.php
blob:a/lib/wordcloud.php -> blob:b/lib/wordcloud.php
<? <?
   
/* /*
@wordCloud @wordCloud
Author: Derek Harvey Author: Derek Harvey
Website: www.lotsofcode.com Website: www.lotsofcode.com
   
@Description @Description
PHP Tag Cloud Class, a nice and simple way to create a php tag cloud, a database and non-database solution. PHP Tag Cloud Class, a nice and simple way to create a php tag cloud, a database and non-database solution.
*/ */
   
class wordCloud class wordCloud
{ {
var $wordsArray = array(); var $wordsArray = array();
var $refsArray = array(); var $refsArray = array();
   
/* /*
* PHP 5 Constructor * PHP 5 Constructor
* *
* @param array $words * @param array $words
* @return void * @return void
*/ */
   
function __construct($words = false) function __construct($words = false)
{ {
if ($words !== false && is_array($words)) if ($words !== false && is_array($words))
{ {
foreach ($words as $key => $value) foreach ($words as $key => $value)
{ {
$this->addWord($value); $this->addWord($value);
} }
} }
} }
   
/* /*
* PHP 4 Constructor * PHP 4 Constructor
* *
* @param array $words * @param array $words
* @return void * @return void
*/ */
   
function wordCloud($words = false) function wordCloud($words = false)
{ {
$this->__construct($words); $this->__construct($words);
} }
   
/* /*
* Assign word to array * Assign word to array
* *
* @param string $word * @param string $word
* @return string * @return string
*/ */
   
function addWord($word, $value = 1, $ref = 0) function addWord($word, $value = 1, $ref = 0)
{ {
if (array_key_exists($word, $this->wordsArray)) if (array_key_exists($word, $this->wordsArray))
$this->wordsArray[$word] += $value; $this->wordsArray[$word] += $value;
else else
$this->wordsArray[$word] = $value; $this->wordsArray[$word] = $value;
$this->refsArray[$word] = $ref; $this->refsArray[$word] = $ref;
   
return $this->wordsArray[$word]; return $this->wordsArray[$word];
} }
   
/* /*
* Shuffle associated names in array * Shuffle associated names in array
*/ */
   
function shuffleCloud() function shuffleCloud()
{ {
$keys = array_keys($this->wordsArray); $keys = array_keys($this->wordsArray);
   
shuffle($keys); shuffle($keys);
   
if (count($keys) && is_array($keys)) if (count($keys) && is_array($keys))
{ {
$tmpArray = $this->wordsArray; $tmpArray = $this->wordsArray;
$this->wordsArray = array(); $this->wordsArray = array();
foreach ($keys as $key => $value) foreach ($keys as $key => $value)
$this->wordsArray[$value] = $tmpArray[$value]; $this->wordsArray[$value] = $tmpArray[$value];
} }
} }
   
/* /*
* Calculate size of words array * Calculate size of words array
*/ */
   
function getCloudSize() function getCloudSize()
{ {
return array_sum($this->wordsArray); return array_sum($this->wordsArray);
} }
   
/* /*
* Get the class range using a percentage * Get the class range using a percentage
* *
* @returns int $class * @returns int $class
*/ */
   
function getClassFromPercent($percent) function getClassFromPercent($percent)
{ {
if ($percent >= 99) if ($percent >= 99)
$class = 1; $class = 1;
else if ($percent >= 70) else if ($percent >= 70)
$class = 2; $class = 2;
else if ($percent >= 60) else if ($percent >= 60)
$class = 3; $class = 3;
else if ($percent >= 50) else if ($percent >= 50)
$class = 4; $class = 4;
else if ($percent >= 40) else if ($percent >= 40)
$class = 5; $class = 5;
else if ($percent >= 30) else if ($percent >= 30)
$class = 6; $class = 6;
else if ($percent >= 20) else if ($percent >= 20)
$class = 7; $class = 7;
else if ($percent >= 10) else if ($percent >= 10)
$class = 8; $class = 8;
else if ($percent >= 5) else if ($percent >= 5)
$class = 9; $class = 9;
else else
$class = 0; $class = 0;
   
return $class; return $class;
} }
   
/* /*
* Create the HTML code for each word and apply font size. * Create the HTML code for each word and apply font size.
* *
* @returns string $spans * @returns string $spans
*/ */
   
function showCloud($returnType = "html") function showCloud($returnType = "html")
{ {
$this->shuffleCloud(); $this->shuffleCloud();
$this->max = max($this->wordsArray); $this->max = max($this->wordsArray);
   
if (is_array($this->wordsArray)) if (is_array($this->wordsArray))
{ {
$return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : "")); $return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : ""));
foreach ($this->wordsArray as $word => $popularity) foreach ($this->wordsArray as $word => $popularity)
{ {
$sizeRange = $this->getClassFromPercent(($popularity / $this->max) * 100); $sizeRange = $this->getClassFromPercent(($popularity / $this->max) * 100);
if ($returnType == "array") if ($returnType == "array")
{ {
$return[$word]['word'] = $word; $return[$word]['word'] = $word;
$return[$word]['ref'] = $this->refsArray[$word]; $return[$word]['ref'] = $this->refsArray[$word];
$return[$word]['sizeRange'] = $sizeRange; $return[$word]['sizeRange'] = $sizeRange;
if ($currentColour) if ($currentColour)
$return[$word]['randomColour'] = $currentColour; $return[$word]['randomColour'] = $currentColour;
} }
else if ($returnType == "html") else if ($returnType == "html")
{ {
$return .= "<span class='word size{$sizeRange}'> {$word} </span>"; $return .= "<span class='word size{$sizeRange}'> {$word} </span>";
} }
} }
return $return; return $return;
} }
} }
} }
?> ?>