forked from imranghory/treemap-squared
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreemap-raphael.js
More file actions
199 lines (164 loc) · 8.17 KB
/
Copy pathtreemap-raphael.js
File metadata and controls
199 lines (164 loc) · 8.17 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
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
/*
* Treemap Squared 0.5 - Treemap Charting library
*
* https://github.com/imranghory/treemap-squared/
*
* Copyright (c) 2012 Imran Ghory (imranghory@gmail.com)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
/* Hints for JSHint */
/*global Raphael, Treemap */
(function() {
"use strict";
Treemap.draw = function(){
// some utility functions
function isArray(arr) {
return arr && arr.constructor === Array;
}
function isFunction(func) {
return func && func.constructor === Function;
}
// mergeProperies - given two sets of associative arrays merge the,
// for clashes copy the new value over the original one
function mergeProperties(originalproperties, newproperties) {
var property;
for (property in newproperties) {
if (newproperties.hasOwnProperty(property)) {
originalproperties[property] = newproperties[property];
}
}
return originalproperties;
}
// drawTreemap - recursively iterate through the nested array of boxes
// and call the styles['draw'] method on them
function drawTreemap(paper, boxes, labels, styles, index) {
var i,j;
var newindex; // the index to the next box to draw
var label; // label of current box
if(isArray(boxes[0][0])) {
for(i=0; i<boxes.length; i++) {
newindex = index.slice();
newindex.push(i);
drawTreemap(paper, boxes[i],labels, styles, newindex);
}
} else {
for(i=0; i<boxes.length; i++) {
newindex = index.slice();
newindex.push(i);
// figure out the matching label using the index
label = labels;
for(j=0; j<newindex.length; j++){
label = label[newindex[j]];
}
// draw box & label
styles.draw(boxes[i], label, newindex);
}
}
}
function draw(element, width, height, data, labels, styles) {
var paper, background, nodes, labelFormatter, boxDrawer;
styles = (typeof styles === "undefined") ? [] : styles;
/* create some default style functions */
// This label formatter calculates a font-size based upon
// average label length and the size of the box the label is
// going into. The maximum font size is set to 20px.
labelFormatter = function () {
var averagelabelsize = totalLabelLength(labels) / countLabels(labels);
// total length of labels (i.e [["Italy"],["Spain", "Greece"]] -> 16)
function totalLabelLength(arr) {
var i, total = 0;
if(isArray(arr[0])) {
for(i=0; i<arr.length; i++) {
total += totalLabelLength(arr[i]);
}
} else {
for (i = 0; i<arr.length; i++){
total += arr[i].length;
}
}
return total;
}
// count of labels (i.e [["Italy"],["Spain", "Greece"]] -> 3)
function countLabels(arr) {
var i, total = 0;
if(isArray(arr[0])) {
for(i=0; i<arr.length; i++) {
total += countLabels(arr[i]);
}
} else {
for (i = 0; i<arr.length; i++){
total += 1;
}
}
return total;
}
function fontSize(width, height) {
// the font size should be proportional to the size of the box (and the value)
// otherwise you can end up creating a visual distortion where two boxes of identical
// size have different sized labels, and thus make it look as if the two boxes
// represent diffferent sizes
var area = width*height;
var arearoot = Math.pow(area, 0.5);
return Math.min( arearoot / (averagelabelsize), 20);
}
function style(coordinates, index) {
return { "fill" : "#FCFCFC", "font-size": fontSize(coordinates[2] - coordinates[0], coordinates[3] - coordinates[1] )};
}
return style;
}();
// default style for boxes
var boxFormatter = function (coordinates, index) {
var colors = ["hsb(0,1,0.4)", "hsb(0.2,1,0.4)", "hsb(0.4,1,0.4)", "hsb(0.6,1,0.4)", "hsb(0.8,1,0.4)"];
var color = (index.length === 1) ? colors[2] : colors[(index[0] + 2) % 5];
return { "stroke": "FEFEFE", "fill" : color};
};
// default box & label drawing routine - in most cases this default one in combination with changing the styles
// will suffice. Only if you're doing something complex and want to rewrite how the treemap gets drawn
// would you replace this.
boxDrawer = function () {
function drawbox(coordinates, label, newindex) {
var x1=coordinates[0], y1=coordinates[1], x2=coordinates[2], y2=coordinates[3];
var box, text;
var boxattr, labelattr;
var rgbobj;
// draw box
box = paper.rect(x1, y1, x2 - x1, y2 - y1);
boxattr = isFunction(styles.box) ? styles.box(coordinates, newindex) : styles.box;
boxattr = mergeProperties(boxFormatter(coordinates, newindex), boxattr);
// dirty hack to fix opacity support in non-webkit web browsers
if ("fill-opacity" in boxattr) {
rgbobj = Raphael.getRGB(boxattr.fill);
if (!rgbobj.error) {
boxattr.fill = "rgba(" + rgbobj.r + "," + rgbobj.g + "," + rgbobj.b + "," + boxattr['fill-opacity'] + ")";
}
}
box.attr(boxattr);
// draw labels
text = paper.text((x1 + x2) / 2, (y1 + y2) / 2, label);
labelattr = isFunction(styles.label) ? styles.label(coordinates, newindex) : styles.label;
labelattr = mergeProperties(labelFormatter(coordinates, newindex), labelattr);
text.attr(labelattr);
// if the label fits better sideways then rotate it
if(text.getBBox().width > x2-x1 && text.getBBox().width <= y2-y1) {
text.rotate(-90);
}
// TODO: add more sophisticated logic to shrink text if it overflows the box size
}
return drawbox;
}();
styles.background = (typeof styles.background === "undefined") ? {} : styles.background;
styles.label = (typeof styles.label === "undefined") ? {} : styles.label;
styles.box = (typeof styles.box === "undefined") ? {} : styles.box;
styles.draw = (typeof styles.draw === "undefined") ? boxDrawer : styles.draw;
// create our canvas and style the background
paper = new Raphael(element, width, height);
background = paper.rect(0, 0, width, height);
background.attr(styles.background);
// generate our treemap from our data
nodes = Treemap.generate(data, width, height);
// draw the generated treemap
drawTreemap(paper, nodes, labels, styles, []);
}
return draw;
}();
})();