Skip to content
This repository
tag: 0.2.30
Fetching contributors…

Cannot retrieve contributors at this time

file 168 lines (148 sloc) 3.214 kb
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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id: Cloud.php 444 2008-04-11 13:38:22Z johmathe $
*
* @package Piwik_Visualization
*/


/**
* Generates a tag cloud from a given data array.
* The generated tag cloud can be in PHP format, or in HTML.
*
* Inspired from Derek Harvey (www.derekharvey.co.uk)
*
* @package Piwik_Visualization
*/
class Piwik_Visualization_Cloud
{
protected $wordsArray = array();

public $truncatingLimit = 30;

/**
* @param array array( word => 10, word2 => 50, word3 => 1)
*/
function __construct($words = false)
{
if ($words !== false && is_array($words))
{
foreach ($words as $word => $value)
{
$this->addWord($word, $value);
}
}
}

/*
* Assign word to array
*
* @param string $word
* @return string
*/
function addWord($word, $value = 1)
{
// $word = strtolower($word);
if (isset($this->wordsArray[$word]))
{
$this->wordsArray[$word] += $value;
}
else
{
$this->wordsArray[$word] = $value;
}
}

/*
* Shuffle associated names in array
*/
function shuffleCloud()
{
$keys = array_keys($this->wordsArray);

shuffle($keys);

if (count($keys) && is_array($keys))
{
$tmpArray = $this->wordsArray;
$this->wordsArray = array();
foreach ($keys as $key => $value)
$this->wordsArray[$value] = $tmpArray[$value];
}
}

/*
* Calculate size of words array
*/

function getCloudSize()
{
return array_sum($this->wordsArray);
}

/*
* Get the class range using a percentage
*
* @returns int $class
*/
function getClassFromPercent($percent)
{
$mapping = array(
95,
70,
50,
30,
15,
5,
0
);
foreach($mapping as $key => $value)
{
if($percent >= $value)
{
return $key;
}
}
}

/*
* Create the HTML code for each word and apply font size.
*
* @returns string $spans
*/

function render($returnType = "html")
{
$this->shuffleCloud();

if($returnType == "html")
{
$return = '';
}
else
{
$return = array();
}

if (count($this->wordsArray) > 0)
{
$this->max = max($this->wordsArray);

$return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : ""));
foreach ($this->wordsArray as $word => $popularity)
{

// truncating the word
$wordTruncated = $word;
if(strlen($word) > $this->truncatingLimit)
{
$wordTruncated = substr($word, 0, $this->truncatingLimit - 3).'...';
}

// computing the percentage
$percent = ($popularity / $this->max) * 100;

// and the CSS style value
$sizeRange = $this->getClassFromPercent($percent);

if ($returnType == "array")
{
$return[$word]['word'] = $word;
$return[$word]['wordTruncated'] = $wordTruncated;
$return[$word]['size'] = $sizeRange;
$return[$word]['percent'] = $percent;
}
else if ($returnType == "html")
{
$return .= "\n<span title='".$word."' class='word size{$sizeRange}'> &nbsp; {$wordTruncated} &nbsp; </span>";
}
// print( $word ."=".$percent."<br>");
}
}
return $return;
}
}
Something went wrong with that request. Please try again.