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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| package
{
import com.flextoolbox.controls.treeMapClasses.*;
import flash.geom.Rectangle;
import mx.controls.Alert;
public class TreeMaplandscape implements ITreeMapLayoutStrategy
{
public function TreeMaplandscape()
{
}
public function updateLayout(branchRenderer:ITreeMapBranchRenderer, bounds:Rectangle):void
{
var items:Array = branchRenderer.itemsToArray();
this.squarify(items, bounds.clone());
}
//--------------------------------------
// Properties
//--------------------------------------
/**
* @private
* The sum of the weight values for the remaining items that have not
* yet been positioned and sized.
*/
private var _totalRemainingWeightSum:Number = 0;
/**
* @private
* The number of items remaining to be drawn.
*/
private var _itemsRemaining:int = 0;
private var temp:Number;
//--------------------------------------
// Private Methods
//--------------------------------------
/**
* @private
* The main squarify algorithm.
*/
private function squarify(items:Array, bounds:Rectangle):void
{
Alert.show(bounds.width.toString()+ " " +bounds.height.toString());
items = items.sortOn("weight", Array.DESCENDING | Array.NUMERIC);
this._totalRemainingWeightSum = this.sumWeights(items);
this._itemsRemaining = items.length;
var lastAspectRatio:Number = Number.POSITIVE_INFINITY;
var lengthOfShorterEdge:Number = Math.min(bounds.width, bounds.height);
var row:Array = [];
while(items.length > 0)
{
var nextItem:TreeMapItemLayoutData = TreeMapItemLayoutData(items.shift());
row.push(nextItem);
var drawRow:Boolean = true;
var aspectRatio:Number = this.calculateWorstAspectRatioInRow(row, lengthOfShorterEdge, bounds);
if(lastAspectRatio >= aspectRatio)
{
lastAspectRatio = aspectRatio;
//if this is the last item, force the row to draw
drawRow = items.length == 0;
}
else
{
//put the item back if the aspect ratio is worse than the previous one
//we want to draw, of course
items.unshift(row.pop());
}
if(drawRow)
{
/* if(bounds.width<bounds.height){
temp=bounds.width;
bounds.width=bounds.height;
bounds.height=bounds.width;
lengthOfShorterEdge= Math.min(bounds.width, bounds.height);
}*/
bounds = this.layoutRow(row, lengthOfShorterEdge, bounds);
//reset for the next pass
lastAspectRatio = Number.POSITIVE_INFINITY;
lengthOfShorterEdge = Math.min(bounds.width, bounds.height);
//Alert.show("largeur:"+bounds.width+"hautuer"+bounds.height);
row = [];
}
}
}
/**
* @private
* Determines the worst (maximum) aspect ratio of the items in a row.
*
* @param row a row of items for which to calculate the worst aspect ratio
* @param lengthOfShorterEdge the length, in pixels, of the edge of the remaining bounds on which to draw the row (the shorter one)
* @return the worst aspect ratio for the items in the row
*/
private function calculateWorstAspectRatioInRow(row:Array, lengthOfShorterEdge:Number, bounds:Rectangle):Number
{
if(row.length == 0)
{
throw new ArgumentError("Row must contain at least one item. If you see this message, please file a bug report.");
}
if(lengthOfShorterEdge == 0)
{
return Number.MAX_VALUE;
}
var totalArea:Number = bounds.width * bounds.height;
var lengthSquared:Number = lengthOfShorterEdge * lengthOfShorterEdge;
//special case where there is zero weight (to avoid divide by zero problems)
if(this._totalRemainingWeightSum == 0)
{
var oneItemArea:Number = totalArea * (1 / this._itemsRemaining);
var rowAreaSquared:Number = Math.pow(oneItemArea * row.length, 2);
return Math.max(lengthSquared * oneItemArea / rowAreaSquared, rowAreaSquared / (lengthSquared * oneItemArea));
}
var firstItem:TreeMapItemLayoutData = TreeMapItemLayoutData(row[0]);
var firstItemArea:Number = totalArea * (firstItem.weight / this._totalRemainingWeightSum);
var maxArea:Number = firstItemArea;
var minArea:Number = firstItemArea;
var sumOfAreas:Number = firstItemArea;
var rowCount:int = row.length;
for(var i:int = 1; i < rowCount; i++)
{
var item:TreeMapItemLayoutData = TreeMapItemLayoutData(row[i]);
var area:Number = totalArea * (item.weight / this._totalRemainingWeightSum);
minArea = Math.min(area, minArea);
maxArea = Math.max(area, maxArea);
sumOfAreas += area;
}
// max(w^2 * r+ / s^2, s^2 / (w^2 / r-))
var sumSquared:Number = sumOfAreas * sumOfAreas;
return Math.max(lengthSquared * maxArea / sumSquared, sumSquared / (lengthSquared * minArea));
}
/**
* @private
* Draws a row of items
*
* @param row The items in the row
* @param lengthOfShorterEdge the length, in pixels, of the edge of the remaining bounds on which to draw the row (the shorter one)
* @param bounds The remaining bounds into which to draw items
*/
private function layoutRow(row:Array, lengthOfShorterEdge:Number, bounds:Rectangle):Rectangle
{
var horizontal:Boolean = lengthOfShorterEdge == bounds.width;
var lengthOfLongerEdge:Number = horizontal ? bounds.height : bounds.width;
var sumOfRowWeights:Number = this.sumWeights(row);
var lengthOfCommonItemEdge:Number = lengthOfLongerEdge * (sumOfRowWeights / this._totalRemainingWeightSum);
if(isNaN(lengthOfCommonItemEdge))
{
if(this._totalRemainingWeightSum == 0)
{
lengthOfCommonItemEdge = lengthOfLongerEdge * row.length / this._itemsRemaining;
}
else
{
lengthOfCommonItemEdge = 0;
}
}
var rowCount:int = row.length;
var position:Number = 0;
for(var i:int = 0; i < rowCount; i++)
{
var item:TreeMapItemLayoutData = TreeMapItemLayoutData(row[i]);
var weight:Number = item.weight;
var ratio:Number = weight / sumOfRowWeights;
//if all nodes in a row have a weight of zero, give them the same area
if(isNaN(ratio))
{
if(sumOfRowWeights == 0 || isNaN(sumOfRowWeights))
{
ratio = 1 / row.length;
}
else
{
ratio = 0;
}
}
var lengthOfItemEdge:Number = lengthOfShorterEdge * ratio;
if(horizontal)
{
item.x = bounds.x + position;
item.y = bounds.y;
item.width = lengthOfItemEdge;
item.height = lengthOfCommonItemEdge;
}else
{
item.x = bounds.x;
item.y = bounds.y + position;
item.width = Math.max(0, lengthOfCommonItemEdge);
item.height = Math.max(0, lengthOfItemEdge);
}
position += lengthOfItemEdge;
this._itemsRemaining--;
}
this._totalRemainingWeightSum -= sumOfRowWeights;
return this.updateBoundsForNextRow(bounds, lengthOfCommonItemEdge);
}
/**
* @private
* After a row is drawn, the bounds must be made smaller to draw the
* next row.
*/
private function updateBoundsForNextRow(bounds:Rectangle, modifier:Number):Rectangle
{
if(bounds.width > bounds.height)
{
var newWidth:Number = Math.max(0, bounds.width - modifier);
bounds.x -= (newWidth - bounds.width);
bounds.width = newWidth;
}
else
{
var newHeight:Number = Math.max(0, bounds.height - modifier);
bounds.y -= (newHeight - bounds.height);
bounds.height = newHeight;
}
return bounds;
}
/**
* @private
* Calculates the sum of weight values in an Array of
* TreeMapItemLayoutData instances.
*/
private function sumWeights(source:Array):Number
{
var sum:Number = 0;
for each(var item:TreeMapItemLayoutData in source)
{
sum += item.weight;
}
return sum;
}
}
} |
Partager