-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.html
222 lines (194 loc) · 6.96 KB
/
test.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
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
<html>
<head>
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
function setColor(structure_id, color)
{
var svgContainer = document.getElementById("svgDrawing");
var svgElement = getElementByAttribute('structure_id',structure_id,svgContainer);
if (!(svgElement == null)) {
svgElement.style.fill = color;
}
}
function getStructureColor(data,valuesDict, default_color)
{// this function iterates over the regions in the data. For each region it checks if it has a value if not it takes the value from its parent
var currentStructID,parentStructID,currentValue,parentValue;
currentStructID = data[0]['id'];
valuesDict[currentStructID] = default_color;
for (var i=1; i< data.length; i++ )
{
currentStructID = data[i]['id'];
parentStructID = data[i]['parent_structure_id'];
if ( !(currentStructID in valuesDict) ){
parentValue = valuesDict[parentStructID];
if (parentValue == null) {
valuesDict[currentStructID] = default_color;
}
else {
valuesDict[currentStructID] = parentValue;
}
}
setColor(currentStructID, valuesDict[currentStructID] );
}
}
function translateNumberToColor(values, minValue,maxValue,colormap)
{
var colorDict = {};
var current_value , releventIndexInColorMap;
var colormap_size = colormap.length;
for(var index in values) {
current_value = values[index];
releventIndexInColorMap = (values[index] - minValue ) / (maxValue - minValue) * (colormap_size -1);
releventIndexInColorMap = Math.round(releventIndexInColorMap);
colorDict[index] = colormap[releventIndexInColorMap];
colorDict[index] = '#' + colorDict[index];
}
return colorDict;
}
function getValuesAndMinMax(data)
{
var minValue = NaN;
var maxValue = NaN;
var valuesDict = {};
var currentValue,numberValue;
for (var i=0; i< data.feed.entry.length; i++ )
{
currentValue = data.feed.entry[i]['gsx$value']['$t'];
currentStructID = data.feed.entry[i]['gsx$id']['$t'];
if (!(currentValue == '' )) {
valuesDict[currentStructID] = currentValue;
numberValue = parseFloat(currentValue);
if (isNaN(minValue)) {
minValue = numberValue;
}
else {
if (minValue > numberValue) {
minValue = numberValue;
}
};
if (isNaN(maxValue)) {
maxValue = numberValue;
}
else {
if (maxValue < numberValue) {
maxValue = numberValue;
}
};
}
}
return {
'valuesDict' : valuesDict,
'minValue' : minValue,
'maxValue' : maxValue
};
}
function getElementByAttribute(attr, value, root) {
root = root || document.body;
if (root.attributes) {
if(root.hasAttribute(attr) && root.getAttribute(attr) == value) {
return root;
}
}
else {
return null;
}
var children = root.childNodes,
element;
for(var i = children.length; i--; ) {
element = getElementByAttribute(attr, value, children[i]);
if(element) {
return element;
}
}
return null;
}
function changeSlice(numberOfImages) {
var location = getURLParameter('location');
location = parseFloat(location);
var spreadsheetkey = getURLParameter('spreadsheetkey');
location = location + numberOfImages;
if (location < 1) {
location = 1;
}
redirectToPage(location, spreadsheetkey);
}
function goToSlice() {
var location = $('#currentLocation').val();
location = parseFloat(location);
var spreadsheetkey = getURLParameter('spreadsheetkey');
if (location < 1) {
location = 1;
}
redirectToPage(location, spreadsheetkey);
}
function redirectToPage(location, spreadsheetkey) {
window.location.replace("test.html?spreadsheetkey=" + spreadsheetkey+ "&location=" +location);
}
function encode_as_img_and_link(){
var location = getURLParameter('location');
// Add some critical information
$("svg").attr({ version: '1.1' , xmlns:"http://www.w3.org/2000/svg"});
var svg = $("#page_div").html();
var b64 = Base64.encode(svg); // or use btoa if supported
// Works in Firefox 3.6 and Webit and possibly any browser which supports the data-uri
$("#buttonsAndStuff").append($("<a href-lang='image/svg+xml' href='data:image/svg+xml;base64,\n"+b64+"' title='file.svg' download='image" + location + ".svg'>Download</a>"));
}
</script>
<script type="text/javascript" src="webtoolkit.base64.js"></script>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="buttonsAndStuff" >
<button onclick="changeSlice(-1);"> ⇐ </button>
<input type="text" id="currentLocation" size="3" > </input>
<button onclick="changeSlice(1);"> ⇒ </button>
<button onclick="goToSlice();">Go</button>
<img src="ajax-loader.gif" id="loader">
</div>
<div id="page_div"></div>
<script>
$(document).ready(function()
{
var default_color = '#f2f1f0';
var heatcolormap = ['6A0000','740000','7F0000','8A0000','940000','9F0000','AA0000','B40000','BF0000','C90000','D40000','DF0000','E90000','F40000','FF0000','FF0A00','FF1500','FF1F00','FF2A00','FF3500','FF3F00','FF4A00','FF5500','FF5F00','FF6A00','FF7400','FF7F00','FF8A00','FF9400','FF9F00','FAA00','FFB400','FFBF00','FFC900','FFD400','FFDF00','FFE900','FFF400','FFFF00','FFFF0F','FFFF1F','FFFF2F','FFFF3F','FFFF4F','FFFF5F','FFFF6F','FFFF7F','FFFF8F','FFFF9F','FFFFAF','FFFFBF','FFFFCF','FFFFDF'];
var colormap = heatcolormap;
var location = getURLParameter('location');
var spreadsheetkey = getURLParameter('spreadsheetkey');
if ( "null" == location) {
location = "1";
redirectToPage(location, spreadsheetkey);
}
var currentLocationBox = $("#currentLocation");
currentLocationBox.attr("placeholder", location);
var fileName = "mouse_atlas_svg/" + "mouse_" + location + ".svg";
$.get(fileName, null,
function(data)
{
var svgNode = $("svg", data);
svgNode.attr('id', "svgDrawing");
var docNode = document.adoptNode(svgNode[0]);
var pageNode = $("#page_div");
pageNode.html(docNode);
},
'xml');
if ( "null" == spreadsheetkey) {
spreadsheetkey = "0AiPUAyOtb4cHdFN5d2VrMC15VTdHakl3VkNleWNacXc";
redirectToPage(location, spreadsheetkey);
}
$.getJSON("ontology/mouse.json", function(ontologyData) {
getStructureColor(ontologyData, {}, default_color);
$.getJSON("http://cors.io/spreadsheets.google.com/feeds/list/" + spreadsheetkey + "/od6/public/values?alt=json", function(data) {
var valuesAndMinMax = getValuesAndMinMax(data);
var colorDict = translateNumberToColor(valuesAndMinMax.valuesDict, valuesAndMinMax.minValue,valuesAndMinMax.maxValue,colormap);
getStructureColor(ontologyData, colorDict, default_color);
$('#loader').hide();
encode_as_img_and_link();
});
});
}) ;
</script>
</body>