-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweightedtree_test.js
261 lines (188 loc) · 8.7 KB
/
weightedtree_test.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
/*
Copyright (c) 2016, BrightPoint Consulting, Inc.
MIT LICENSE:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
// @version 1.1.54
//**************************************************************************************************************
//
// This is a test/example file that shows you one way you could use a vizuly object.
// We have tried to make these examples easy enough to follow, while still using some more advanced
// techniques. Vizuly does not rely on any libraries other than D3. These examples do use jQuery and
// materialize.css to simplify the examples and provide a decent UI framework.
//
//**************************************************************************************************************
// html element that holds the chart
var viz_container;
// our weighted tree
var viz;
// our theme
var theme;
// nested data
var data = {};
// stores the currently selected value field
var valueField = "Comunities";
var valueFields = ["Comunities", "Papers"];
var formatCurrency = function (d) { if (isNaN(d)) d = 0; return d3.format(",.0f")(d); };
function loadData() {
d3.csv("data/viz_with_clusters.csv", function (csv) {
data.values=prepData(csv);
var blob = JSON.stringify(data);
initialize();
});
}
function prepData(csv) {
var values=[];
//Clean federal budget data and remove all rows where all values are zero or no labels
csv.forEach(function (d,i) {
var t = 0;
for (var i = 0; i < valueFields.length; i++) {
t += Number(d[valueFields[i]]);
}
if (t > 0) {
values.push(d);
}
})
//Make our data into a nested tree. If you already have a nested structure you don't need to do this.
var nest = d3.nest()
.key(function (d) {
return d.Level1;
})
.key(function (d) {
return d.Level2;
})
.entries(values);
//This will be a viz.data function;
vizuly.core.util.aggregateNest(nest, valueFields, function (a, b) {
return Number(a) + Number(b);
});
//Remove empty child nodes left at end of aggregation and add unqiue ids
function removeEmptyNodes(node,parentId,childId) {
if (!node) return;
node.id=parentId + "_" + childId;
if (node.values) {
for(var i = node.values.length - 1; i >= 0; i--) {
node.id=parentId + "_" + i;
if(!node.values[i].key && !node.values[i].Level5) {
node.values.splice(i, 1);
}
else {
removeEmptyNodes(node.values[i],node.id,i)
}
}
}
}
var node={};
node.values = nest;
removeEmptyNodes(node,"0","0");
var blob = JSON.stringify(nest);
return nest;
}
function initialize() {
viz = vizuly.viz.weighted_tree(document.getElementById("viz_container"));
//Here we create three vizuly themes for each radial progress component.
//A theme manages the look and feel of the component output. You can only have
//one component active per theme, so we bind each theme to the corresponding component.
theme = vizuly.theme.weighted_tree(viz).skin(vizuly.skin.WEIGHTED_TREE_AXIIS);
//Like D3 and jQuery, vizuly uses a function chaining syntax to set component properties
//Here we set some bases line properties for all three components.
viz.data(data) // Expects hierarchical array of objects.
.width(600) // Width of component
.height(600) // Height of component
.children(function (d) { return d.values }) // Denotes the property that holds child object array
.key(function (d) { return d.id }) // Unique key
.value(function (d) {
return Number(d["agg_" + valueField]) }) // The property of the datum that will be used for the branch and node size
.fixedSpan(-1) // fixedSpan > 0 will use this pixel value for horizontal spread versus auto size based on viz width
.branchPadding(.07)
.label(function (d) { // returns label for each node.
return trimLabel(d.key || (d['Level' + d.depth]))})
.on("measure",onMeasure) // Make any measurement changes
.on("mouseover",onMouseOver) // mouseover callback - all viz components issue these events
.on("mouseout",onMouseOut) // mouseout callback - all viz components issue these events
.on("click",onClick); // mouseout callback - all viz components issue these events
//We use this function to size the components based on the selected value from the RadiaLProgressTest.html page.
changeSize("1366,900");
}
function trimLabel(label) {
return (String(label).length > 20) ? String(label).substr(0, 17) + "..." : label;
}
var datatip='<div class="tooltip" style="width: 250px; background-opacity:.5">' +
'<div class="header1">HEADER1</div>' +
'<div class="header-rule"></div>' +
'<div class="header2"> HEADER2 </div>' +
'<div class="header-rule"></div>' +
'<div class="header3"> HEADER3 </div>' +
'</div>';
// This function uses the above html template to replace values and then creates a new <div> that it appends to the
// document.body. This is just one way you could implement a data tip.
function createDataTip(x,y,h1,h2,h3) {
var html = datatip.replace("HEADER1", h1);
html = html.replace("HEADER2", h2);
html = html.replace("HEADER3", h3);
d3.select("body")
.append("div")
.attr("class", "vz-weighted_tree-tip")
.style("position", "absolute")
.style("top", y + "px")
.style("left", (x - 125) + "px")
.style("opacity",0)
.html(html)
.transition().style("opacity",1);
}
function onMeasure() {
// Allows you to manually override vertical spacing
// viz.tree().nodeSize([100,0]);
}
function onMouseOver(e,d,i) {
if (d == data) return;
var rect = e.getBoundingClientRect();
if (d.target) d = d.target; //This if for link elements
createDataTip(rect.left, (rect.top+viz.height() *.05), (d.key || (d['Level' + d.depth])), formatCurrency(d["agg_" + valueFields[0]]) + " Comunities","");
}
function onMouseOut(e,d,i) {
d3.selectAll(".vz-weighted_tree-tip").remove();
}
//We can capture click events and respond to them
function onClick(g,d,i) {
// viz.toggleNode(d);
var modal = document.getElementById(d.key);
modal.style.display = "block";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
}
//This function is called when the user selects a different skin.
function changeSkin(val) {
if (val == "None") {
theme.release();
}
else {
theme.viz(viz);
theme.skin(val);
}
viz().update(); //We could use theme.apply() here, but we want to trigger the tween.
}
//This changes the size of the component by adjusting the width/height;
function changeSize(val) {
var s = String(val).split(",");
viz_container.transition().duration(400).style('width', 2000 + "px").style('height', 1000 + "px");
viz.width(s[0]).height(s[1]*.8).update();
}
//This sets the same value for each radial progress
function changeData(val) {
valueField=valueFields[Number(val)];
viz.update();
}