forked from egonw/mscpils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample5.html
147 lines (119 loc) · 4.37 KB
/
example5.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
Copyright (c) 2013 Egon Willighagen <[email protected]>
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.
-->
<head>
<title>d3.js example</title>
<!-- ops.js -->
<script src="lib/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="lib/purl.js"></script>
<script type="text/javascript" src="src/OPS.js"></script>
<script type="text/javascript" src="src/CompoundSearch.js"></script>
<!-- d3.js -->
<script src="lib/d3.v3.min.js" charset="utf-8"></script>
<!-- setup -->
<script type="text/javascript">
var prmstr = window.location.search.substr(1);
var prmarr = prmstr.split ("&");
var params = {};
for ( var i = 0; i < prmarr.length; i++) {
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
</script>
</head>
<body>
<h3>OpenPHACTS / d3.js mash up</h3>
<p>This example lists the number of activities for paracetamol by unit.</p>
<p><div class="chart"></div></p>
<pre><div class="json1">1</div></pre>
<pre><div class="json2">2</div></pre>
<!-- dynamically create a table with type information -->
<script type="text/javascript">
var sources = new Openphacts.CompoundSearch("https://beta.openphacts.org", params["app_id"], params["app_key"]);
var callback = function(success, status, response){
d3.select(".json2").html(JSON.stringify(response, undefined, 2));
var data = [];
// count the units
unitCounts = {};
list = response.items
max = 0
for (i=0; i<list.length; i++) {
item = list[i]
units = item.standardUnits
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) {
data.push({name:key, value:unitCounts[key]})
}
var width = 500,
height = 600,
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(".chart").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 > 10) return d.data.name; return "";});
};
var callbackCount = function(success, status, response){
d3.select(".json1").html(JSON.stringify(response, undefined, 2));
count = response.primaryTopic.compoundPharmacologyTotalResults
compound = response.primaryTopic._about
sources.compoundPharmacology(compound, 1, count, callback);
};
sources.compoundPharmacologyCount("http://www.conceptwiki.org/concept/a99023df-c3d2-4fa5-82d1-89d3a1d16e0e", callbackCount);
</script>
</body>
</html>