-
Notifications
You must be signed in to change notification settings - Fork 9
/
_utils.js
259 lines (235 loc) · 5.97 KB
/
_utils.js
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
define(function () {
var utils = {
group: function (/*Array*/items, /*Array*/groupingFunctions, /*Function*/measureFunction) {
var response = {
children: []
};
var merge = function (obj, entry) {
if (!obj.__treeValue) {
obj.__treeValue = 0;
}
obj.__treeValue += measureFunction(entry);
return obj;
};
// we go over each entry in the array
items.forEach(function (entry) {
var r = response;
// for this entry, for each rowField we
// look at the actual value for this rowField
// and create a holding object for this
// value in response if it does not exist
groupingFunctions.forEach(function (groupingFunction, j) {
// actual value for the rowField
var data = groupingFunction(entry);
// create child if undefined
var child = utils.find(r.children, function (item) {
return (item.__treeName === data);
});
if (!child) {
r.children.push(child = {
__treeName: data,
__treeID: data + Math.random(),
children: []
});
}
child = merge(child, entry);
if (j !== groupingFunctions.length - 1) {
// branch & prepare response for
// next call
r = child;
} else {
// add the entry to the leaf!
child.children.push(entry);
}
});
r = merge(r, entry);
});
return response;
},
find: function (/*Array*/array, /*Function*/callback) {
var l = array.length;
for (var i = 0; i < l; ++i) {
if (callback.call(null, array[i])) {
return array[i];
}
}
return null;
},
solve: function (items, width, height, areaFunc, rtl) {
//
// Create temporary TreeMap elements
//
var treeMapElements = utils.initElements(items, areaFunc);
var dataTotal = treeMapElements.total;
var elements = treeMapElements.elements;
var realSize = dataTotal;
if (dataTotal === 0) {
if (elements.length === 0) {
return {
items: items,
rects: [],
total: 0
};
}
elements.forEach(function (element) {
element.size = element.sizeTmp = 100;
});
dataTotal = elements.length * 100;
}
//
// Sort the TreeMap elements
//
elements.sort(function (b, a) {
return a.size - b.size;
});
utils._compute(width, height, elements, dataTotal);
//
// Restore initial Sort order
//
elements.sort(function (a, b) {
return a.index - b.index;
});
var result = {};
result.elements = elements;
result.size = realSize;
result.rectangles = elements.map(function (element) {
return {
x: rtl ? width - element.x - element.width : element.x,
y: element.y,
w: element.width,
h: element.height
};
});
return result;
},
initElements: function (items, areaFunc) {
var total = 0;
var elements = items.map(function (item, index) {
var size = areaFunc != null ? areaFunc(item) : 0;
if (size < 0) {
throw new Error("item size dimension must be positive");
}
total += size;
return {
index: index,
size: size,
sizeTmp: size
};
});
return {
elements: elements,
total: total
};
},
_compute: function (width, height, elements, total) {
var valueScale = ((width * height) / total) / 100;
elements.forEach(function (element) {
element.sizeTmp *= valueScale;
});
var start = 0;
var end = 0;
var aspectCurr = -1 >>> 1; // int.MaxValue;
var aspectLast;
var offsetX = 0;
var offsetY = 0;
var tmpWidth = width;
var tmpHeight = height;
var vert = tmpWidth > tmpHeight, n;
while (end !== elements.length) {
aspectLast = utils._trySolution(elements, start, end, vert, tmpWidth, tmpHeight);
if ((aspectLast > aspectCurr) || (aspectLast < 1)) {
var currX = 0;
var currY = 0;
for (n = start; n < end; n++) {
elements[n].x = offsetX + currX;
elements[n].y = offsetY + currY;
if (vert) {
currY += elements[n].height;
} else {
currX += elements[n].width;
}
}
if (vert) {
offsetX += elements[start].width;
} else {
offsetY += elements[start].height;
}
tmpWidth = width - offsetX;
tmpHeight = height - offsetY;
vert = tmpWidth > tmpHeight;
start = end;
end = start;
aspectCurr = -1 >>> 1; // int.MaxValue;
continue;
} else {
for (n = start; n <= end; n++) {
elements[n].width = elements[n].widthTmp;
elements[n].height = elements[n].heightTmp;
}
aspectCurr = aspectLast;
}
end++;
}
var currX1 = 0;
var currY1 = 0;
for (n = start; n < end; n++) {
elements[n].x = offsetX + currX1;
elements[n].y = offsetY + currY1;
if (vert) {
currY1 += elements[n].height;
} else {
currX1 += elements[n].width;
}
}
},
_trySolution: function (elements, start, end, vert, tmpWidth, tmpHeight) {
var total = 0;
var aspect;
var localWidth = 0;
var localHeight = 0;
var n;
for (n = start; n <= end; n++) {
total += elements[n].sizeTmp;
}
if (vert) {
if (tmpHeight === 0) {
localWidth = localHeight = 0;
} else {
localWidth = total / tmpHeight * 100;
localHeight = tmpHeight;
}
} else {
if (tmpWidth === 0) {
localWidth = localHeight = 0;
} else {
localHeight = total / tmpWidth * 100;
localWidth = tmpWidth;
}
}
for (n = start; n <= end; n++) {
if (vert) {
elements[n].widthTmp = localWidth;
if (total === 0) {
elements[n].heightTmp = 0;
} else {
elements[n].heightTmp = localHeight * elements[n].sizeTmp / total;
}
} else {
if (total === 0) {
elements[n].widthTmp = 0;
} else {
elements[n].widthTmp = localWidth * elements[n].sizeTmp / total;
}
elements[n].heightTmp = localHeight;
}
}
aspect = Math.max(elements[end].heightTmp / elements[end].widthTmp, elements[end].widthTmp
/ elements[end].heightTmp);
if (aspect === undefined) {
return 1;
}
return aspect;
}
};
return utils;
});