-
Notifications
You must be signed in to change notification settings - Fork 1
/
helperMethods.js
235 lines (203 loc) · 7.11 KB
/
helperMethods.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
var plot = function(where, values) {
// Formatters for counts and times (converting numbers to Dates).
var formatCount = d3.format(",.0f");
var margin = {top: 10, right: 30, bottom: 30, left: 30},
width = 400 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
if (values.length < 2) {
$(where).append("<span>Not enough data.</span>")
return; // cannot plot with less than 2 data points
}
console.log("values: " + values.length)
console.log(values)
var x = d3.scale.linear()
.domain([Math.min.apply(null, values), Math.max.apply(null, values)])
.range([0, width]);
// Generate a histogram using twenty uniformly-spaced bins.
var data = d3.layout.histogram()
.bins(x.ticks(35))
(values);
console.log("data: " + data)
var y = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.y; })])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var svg = d3.select(where).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var bar = svg.selectAll(".bar")
.data(data)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
bar.append("rect")
.attr("x", 1)
.attr("width", x(data[0].x + data[0].dx) - 1) // see http://stackoverflow.com/questions/15388481/d3-js-histogram-with-positive-and-negative-values
.attr("height", function(d) { return height - y(d.y); });
bar.append("text")
.attr("dy", ".75em")
.attr("y", 6)
.attr("x", x(data[0].dx) / 2)
.attr("text-anchor", "middle")
.text(function(d) { return formatCount(d.y); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
console.log("Plotting done.")
}
var processSubstance = function(success, status, response){
document.getElementById("json").innerHTML = "<pre>" + JSON.stringify(response, null, ' ') + "</pre>";
var size;
for (i=0;i<response.study.length;i++) {
substance = response.study[i]
owner = substance.owner
if (owner.company != null && owner.company.name != null) {
uuid = substance.owner.substance.uuid
uuidOwner = owner.company.uuid
uuidOwner = owner.company.uuid
name = owner.company.name
topCath = substance.protocol.topcategory
cath = substance.protocol.category.title
cathURL = substance.protocol.category.term
content =
"<tr><td><a href=\"https://apps.ideaconsult.net/enanomapper/substance/" + uuid + "/study\">" + materialNames[uuid] +
"</a></td><td><a href=\"" + cathURL + "\">" + cath +
"</a></td><td>" + substance.protocol.endpoint +
"</td><td><a href=\"https://apps.ideaconsult.net/enanomapper/substanceowner/" + uuidOwner + "\">" + name +
"</a></td><td>"
if (substance.citation != null && substance.citation.title != null) {
doiURI = substance.citation.title
content += "<a href=\"" + doiURI + "\">" + doiURI + "</a>";
}
content += "</td></tr>";
//document.getElementById("content"+topCath).innerHTML += content;
$("#table" + topCath + " tbody").append(content);
$("#table" + topCath).trigger("update", [true]);
if (topCatCounts[topCath]) {
topCatCounts[topCath] = topCatCounts[topCath] + 1
} else {
topCatCounts[topCath] = 1
}
}
var study = response.study[i]
for (j=0;j<study.effects.length;j++) {
var effect = study.effects[j]
// console.log("effect: " + effect)
if (effect.endpoint == "PARTICLE SIZE" || effect.endpoint == "PRIMARY SIZE 1ST DIMENSION") {
if (effect.result.unit == "nm") {
if (effect.result.loValue) {
size = effect.result.loValue;
if (size < 500) {
sizesVal[sizeCount] = size;
sizeCount = sizeCount + 1;
}
}
}
}
if (effect.endpoint == "ZETA POTENTIAL") {
if (effect.result.unit == "mV") {
if (effect.result.loValue) {
zeta = effect.result.loValue;
zetasVal[zetaCount] = zeta;
zetaCount = zetaCount + 1;
}
}
}
}
}
workload = workload - 1;
if (workload > 0) {
document.getElementById("workload").innerHTML = "" + workload + " still to load";
} else if (workload == 0) {
document.getElementById("workload").innerHTML = "";
plot(".sizes", sizesVal);
plot(".zetas", zetasVal);
// now convert the topCat counts to data
var data = [];
for (key in topCatCounts) {
keyStr = key
if (keyStr.length > 16) keyStr = keyStr.substring(0,14) + ".."
data.push({name:keyStr, value:topCatCounts[key]})
}
plotPie(data, "props");
}
};
var plotPie = function(data, where) {
var width = 200,
height = 300,
radius = Math.min(width, height) / 2;
var color = d3.scale.category20();
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.value; });
var svg = d3.select("." + where).append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr(
"transform",
"translate(" + width / 2 + "," + height / 2 + ")"
);
data.forEach(function(d) {
d.value = +d.value;
});
var g = svg.selectAll(".chart")
.data(pie(data))
.enter().append("g")
.attr("class", "chart");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.name); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { if (d.data.value > substanceCount/5) return d.data.name; return "";});
}
var processList = function(success, status, response){
substanceCount = response.substance.length
document.getElementById("summary").innerHTML =
"There are " + substanceCount + " nanomaterials found."
for (i=0;i<substanceCount;i++) {
var substance = response.substance[i]
uuid = substance.i5uuid
materialNames[uuid] = substance.name;
searcher.info(substance.URI, processSubstance);
workload = workload + 1;
document.getElementById("workload").innerHTML = "" + workload;
}
// make a provider pie chart
var data = [];
// count the units
unitCounts = {};
list = response.substance
max = 0
for (i=0; i<list.length; i++) {
item = list[i]
if (item.ownerName) {
units = item.ownerName;
}
if (!units) units = "none"
if (unitCounts[units]) {
unitCounts[units] = unitCounts[units] + 1
} else {
unitCounts[units] = 1
}
max = Math.max(max, unitCounts[units])
}
// now convert the unit counts to data
for (key in unitCounts) {
keyStr = key
if (keyStr.length > 16) keyStr = keyStr.substring(0,14) + ".."
data.push({name:keyStr, value:unitCounts[key]})
}
plotPie(data, "chart");
};