Repository navigation
Expand file tree
/
Copy pathapp_subset.js
More file actions
executable file
·320 lines (246 loc) · 11.6 KB
/
Copy pathapp_subset.js
File metadata and controls
executable file
·320 lines (246 loc) · 11.6 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// "use strict";
console.log("fileid: "+fileid);
console.log("hostname: "+hostname);
var colors = d3.scale.category20();
var zdata;
var varColor = '#f0f8ff'; //d3.rgb("aliceblue");
var selVarColor = '#fa8072'; //d3.rgb("salmon");
var d3Color = '#1f77b4'; // d3's default blue
var lefttab = "tab1"; //global for current tab in left panel
var ddiurl = hostname+"/api/meta/datafile/"+fileid;
var dataurl = hostname+"/api/access/datafile/"+fileid;
console.log("dataurl: "+dataurl);
// Pre-processed data:
var pURL = dataurl+"?format=prep";
// Uncomment the lines below, if you want to test the code with
// the sample data files distributed with the
// app in the "data" directory:
//if (!fileid) {
// //pURL = "data/preprocess2429360.txt"; // This is the Strezhnev Voeten JSON data
// pURL = "/subset/data/fearonLaitin.txt"; // This is the Fearon Laitin JSON data
// // pURL = "data/qog_pp.json"; // This is Qual of Gov
// ddiurl="/subset/data/fearonLaitin.xml"; // This is Fearon Laitin
//}
console.log("ddiurl: "+ddiurl);
var preprocess = {};
var valueKey = [];
var lablArray = [];
var hold = [];
var allNodes = [];
var nodes = [];
var apicall = "";
var brushable = false; // set to true if you want to turn on brushing over the plots
d3.json(pURL, function(error, json) {
//console.log("executing d3.json ("+pURL+")");
if (error) return console.warn(error);
var jsondata = json;
//copying the object
for(var key in jsondata) {
preprocess[key] = jsondata[key];
}
d3.xml(ddiurl, "application/xml", function(xml) {
//console.log("executing d3.xml");
var vars = xml.documentElement.getElementsByTagName("var");
var temp = xml.documentElement.getElementsByTagName("fileName");
zdata = temp[0].childNodes[0].nodeValue;
// dataset name trimmed to 12 chars
var dataname = zdata.replace( /\.(.*)/, "") ; // regular expression to drop any file extension
// Put dataset name, from meta-data, into top panel
d3.select("#leftpaneltitle").selectAll("h3")
.html(dataname);
for (var i=0;i<vars.length;i++) {
var sumStats = new Object;
var varStats = [];
valueKey[i] = vars[i].attributes.name.nodeValue;
if(vars[i].getElementsByTagName("labl").length === 0) {lablArray[i]="no label";}
else {lablArray[i] = vars[i].getElementsByTagName("labl")[0].childNodes[0].nodeValue;}
varStats = vars[i].getElementsByTagName("sumStat");
for (var j=0; j<varStats.length; j++) {
var myType = "";
myType = varStats[j].getAttribute("type");
if(myType==null) continue; // no sumStat
sumStats[myType] = varStats[j].childNodes[0].nodeValue;
}
allNodes.push({id:i, reflexive: false, "name": valueKey[i], "varid":vars[i].attributes.ID.nodeValue, "labl": lablArray[i], data: [5,15,20,0,5,15,20], count: hold, "nodeCol":colors(i), "baseCol":colors(i), "strokeColor":selVarColor, "strokeWidth":"1", "varLevel":vars[i].attributes.intrvl.nodeValue, "minimum":sumStats.min, "median":sumStats.medn, "standardDeviation":sumStats.stdev, "mode":sumStats.mode, "valid":sumStats.vald, "mean":sumStats.mean, "maximum":sumStats.max, "invalid":sumStats.invd, "subsetplot":false, "subsetrange":["", ""],"setxplot":false, "setxvals":["", ""], "grayout":false});
};
scaffolding();
layout();
});
});
// functions
function scaffolding() {
//console.log("executing scaffolding");
var count = 0;
d3.select("#tab1").selectAll("p")
.data(valueKey)
.enter()
.append("p")
.attr("id",function(d){
return d.replace(/\W/g, "_"); // replace non-alphanumerics for selection purposes
}) // perhapse ensure this id is unique by adding '_' to the front?
.text(function(d){return d;})
.style('background-color',function(d) {
if(findNodeIndex(d) > 2) {return varColor;}
else {return hexToRgba(selVarColor);}
})
.attr("data-container", "body")
.attr("data-toggle", "popover")
.attr("data-trigger", "hover")
.attr("data-placement", "right")
.attr("data-html", "true")
.attr("onmouseover", "$(this).popover('toggle');")
.attr("onmouseout", "$(this).popover('toggle');")
.attr("data-original-title", "Summary Statistics");
populatePopover(); // pipes in the summary stats
}
function populatePopover () {
//console.log("executing populatePopover");
d3.select("#tab1").selectAll("p")
.attr("data-content", function(d) {
var onNode = findNodeIndex(d);
return popoverContent(allNodes[onNode]);
});
}
function popoverContent(d) {
//console.log("executing popoverContent");
var rint = d3.format("r");
return "<div class='row'><label class='col-sm-4 control-label'>Label</label><div class='col-sm-6'><p class='form-control-static'><i>" + d.labl + "</i></p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Mean</label><div class='col-sm-6'><p class='form-control-static'>" + (+d.mean).toPrecision(4).toString() + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Median</label><div class='col-sm-6'><p class='form-control-static'>" + (+d.median).toPrecision(4).toString() + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Mode</label><div class='col-sm-6'><p class='form-control-static'>" + (+d.mode).toPrecision(4).toString() + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Stand Dev</label><div class='col-sm-6'><p class='form-control-static'>" + (+d.standardDeviation).toPrecision(4).toString() + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Maximum</label><div class='col-sm-6'><p class='form-control-static'>" + (+d.maximum).toPrecision(4).toString() + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Minimum</label><div class='col-sm-6'><p class='form-control-static'>" + (+d.minimum).toPrecision(4).toString() + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Invalid</label><div class='col-sm-6'><p class='form-control-static'>" + rint(d.invalid) + "</p></div></div>" +
"<div class='row'><label class='col-sm-4 control-label'>Valid</label><div class='col-sm-6'><p class='form-control-static'>" + rint(d.valid) + "</p></div></div>" ;
}
function layout() {
//console.log("executing layout");
var myValues=[];
nodes = [];
if(allNodes.length > 2) {
nodes = [allNodes[0], allNodes[1], allNodes[2]];
}
else if(allNodes.length === 2) {
nodes = [allNodes[0], allNodes[1]];
}
else if(allNodes.length === 1){
nodes = [allNodes[0]];
}
else {
alert("There are zero variables in the metadata.");
return;
}
panelPlots(); // after nodes is populated, add subset and setx panels
// add listerners to leftpanel.left. every time a variable is clicked, nodes updates and background color changes. mouseover shows summary stats or model description.
d3.select("#tab1").selectAll("p")
.on("click", function varClick(){
d3.select(this)
.style('background-color',function(d) {
var myText = d3.select(this).text();
var myColor = d3.select(this).style('background-color');
if(d3.rgb(myColor).toString() === varColor.toString()) { // adding a variable
nodes.push(findNode(myText));
return hexToRgba(selVarColor);
}
else { // dropping a variable
nodes.splice(nodeIndex(myText), 1);
return varColor;
}
});
panelPlots();
});
} // end layout
// returns id
var findNodeIndex = function(nodeName) {
for (var i in allNodes) {
if(allNodes[i]["name"] === nodeName) {return allNodes[i]["id"];}
};
}
// function to convert color codes
function hexToRgba(hex) {
var h=hex.replace('#', '');
var bigint = parseInt(h, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
var a = '0.5';
return "rgba(" + r + "," + g + "," + b + "," + a + ")";
}
function panelPlots() {
//console.log("executing panelPlots");
// build arrays from nodes in main
var dataArray = [];
var varArray = [];
var idArray = [];
var varidArray = [];
for(var j=0; j < nodes.length; j++ ) {
dataArray.push({varname: nodes[j].name, properties: preprocess[nodes[j].name]});
varArray.push(nodes[j].name);
idArray.push(nodes[j].id);
varidArray.push(nodes[j].varid);
}
for (var i = 0; i < varArray.length; i++) {
if (dataArray[i].properties.type === "continuous" & allNodes[idArray[i]].subsetplot==false) {
allNodes[idArray[i]].subsetplot=true;
density(dataArray[i], allNodes[idArray[i]], "subset", brushable);
}
else if (dataArray[i].properties.type === "bar" & allNodes[idArray[i]].subsetplot==false) {
allNodes[idArray[i]].subsetplot=true;
barsSubset(dataArray[i], allNodes[idArray[i]], "subset", brushable);
}
}
// this is where apicall is built
apicall=dataurl+"?variables="+varidArray.toString();
console.log('apicall ' + apicall);
// update the plots in subset tab
d3.select("#tab2ContentArea").selectAll("svg")
.each(function(){
d3.select(this);
var regstr = /(.+)_tab2ContentArea_(\d+)/;
var myname = regstr.exec(this.id);
var nodeid = myname[2];
myname = myname[1];
var j = varArray.indexOf(myname);
if(j == -1) {
allNodes[nodeid].subsetplot=false;
var temp = "#".concat(myname,"_tab2ContentArea_",nodeid);
d3.select(temp)
.remove();
}
});
}
var nodeIndex = function(nodeName) {
for (var i in nodes) {
if(nodes[i]["name"] === nodeName) {return i;}
}
}
var findNode = function(nodeName) {
for (var i in allNodes) {if (allNodes[i]["name"] === nodeName) return allNodes[i]};
}
function tabLeft(tab) {
//console.log("executing tableft");
if(tab == "tab3") {
API();
return;
}
else {lefttab=tab;}
var tabi = tab.substring(3);
document.getElementById('tab1').style.display = 'none';
document.getElementById('tab2').style.display = 'none';
document.getElementById('tab3').style.display = 'none';
if(tab==="tab1") {
document.getElementById('btnSubset').setAttribute("class", "btn btn-default");
document.getElementById('btnVariables').setAttribute("class", "btn active");
document.getElementById('txtSubset').style.display = 'none';
}
else if (tab==="tab2") {
document.getElementById('btnVariables').setAttribute("class", "btn btn-default");
document.getElementById('btnSubset').setAttribute("class", "btn active");
document.getElementById('txtSubset').style.display = 'block';
}
document.getElementById(tab).style.display = 'block';
}
function API() {
window.open(apicall);
}