[xwiki-users] rounding error in TagCloud
Hey all, the TagCloud macro has a rounding error. The result is, that the highest occurrence has no value in class attribute and is displayed similar to notPopular. The following code should solve the problem: {{velocity}} ## ## tagCloudSpace - the space to display the tag cloud for. If blank, display cloud for the whole wiki. Default is blank. #set ($tagCloudSpace = $context.macro.params.get('space')) ## ## Configurable popularity levels. You must an even number of levels. Default: 6. #set ($popularityLevels = ["notPopular", "notVeryPopular", "somewhatPopular", "popular", "veryPopular", "ultraPopular"]) #set ($levelsHalf = $popularityLevels.size() / 2) ## $xwiki.ssx.use("XWiki.TagCloud") ## ## Get tag count map. Key: tag, value: number of occurrences. #set ($tagCount = $xwiki.tag.getTagCount("$!tagCloudSpace")) ## ## only build popularity map if at least one tag exists #if ($tagCount.size() > 0) ## ## Store the minimum and maximum number of occurrences. #set ($sortedTagCounts = $sorttool.sort($tagCount.values())) #set ($maxIndex = $sortedTagCounts.size() - 1) #set ($minCount = $listtool.get($sortedTagCounts, 0)) #set ($maxCount = $listtool.get($sortedTagCounts, $maxIndex)) ## ## Calculate the total number of tags in the wiki. #set ($totalTagNumber = 0) #foreach ($currentTagCount in $sortedTagCounts) #set ($totalTagNumber = $totalTagNumber + $currentTagCount) #end #set ($countAverage = $mathtool.div($totalTagNumber, $tagCount.size())) ## ## Build popularity map using the difference between minimum maximum tag occurrences counts ## and the average tag occurences count. #set ($firstHalfIntervalSize = $mathtool.div($mathtool.sub($countAverage, $minCount), $levelsHalf)) #set ($secondHalfIntervalSize = $mathtool.div($mathtool.sub($maxCount, $countAverage), $levelsHalf)) ## ## use a sorted map #set ($popularityMap = $util.getTreeMap()) #set ($previousPopularityMax = $minCount) #set ($intervalSize = $firstHalfIntervalSize) #set ($halfPassed = false) #foreach ($popularityLevel in $popularityLevels) #if ($velocityCount > $levelsHalf && !$halfPassed) #set ($intervalSize = $secondHalfIntervalSize) #set ($halfPassed = true) #end #set ($currentPopularityMax = $mathtool.add($previousPopularityMax, $intervalSize)) ## ## round the threshold #set ($threshold = $mathtool.roundTo(1, $currentPopularityMax)) #set ($discard = $popularityMap.put($threshold, $popularityLevel)) #set ($previousPopularityMax = $currentPopularityMax) #end #end ## ## Display tag cloud. {{html}} #if ($tagCount.size() > 0) <ol class="tagCloud"> #foreach ($tagEntry in $tagCount.entrySet()) #foreach ($popularityEntry in $popularityMap.entrySet()) #if ($popularityEntry.Key >= $tagEntry.Value) #set ($liClass = $popularityEntry.Value) #break #end #end <li class="$liClass"> <a class="tag" href="$xwiki.getURL("Main.Tags", "view", "do=viewTag&tag=$escapetool.url($tagEntry.Key)")" title="$msg.get("xe.tag.tooltip", ["$tagEntry.Value"])">$escapetool.xml($tagEntry.Key)</a> </li> #end </ol> #else #if("$!tagCloudSpace" == "") $msg.get("xe.tag.notags") #else $msg.get("xe.tag.notagsforspace", ["$tagCloudSpace"]) #end #end {{/html}} {{/velocity}} best regards Jörg
participants (1)
-
Schömer, Jörg