-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (131 loc) · 4.37 KB
/
app.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
//get data from sample json file
function getData(sampleID){
d3.json("samples.json").then((importedData) => {
var data = importedData;
console.log(data);
var sampleData = data.samples;
console.log(sampleValue);
var resultArr = sampleData.filter(sampleObject => sampleObject.id == sampleID);
var variableArr = resultArr[0];
console.log(variableArr);
var sampleValue = variableArr.sample_values;
//console.log(sampleValue);
var otuID = variableArr.otu_ids;
//console.log(otuID);
var otuLabel = variableArr.otu_labels;
//console.log(otuLabel);
//get wfreqdata
var mData = data.metadata;
var resultFreq = mData.filter(freqObject => freqObject.id == sampleID);
var freqData = resultFreq[0];
console.log(freqData);
var wFreq = freqData.wfreq;
//create bar chart
var arrData = [{
x: sampleValue.slice(0, 10).reverse(),
y: otuID.slice(0,10).map(otuID => `OTU${otuID}`).reverse(),
text: otuLabel.slice(0, 10).reverse(),
type: "bar",
orientation: "h",
marker: {
color: 'rgba(58,200,225,.5)'
}
}];
var barLayout = {
title: "Top 10 OTUs found",
font: { family: 'Raleway, sans-serif'}
};
Plotly.newPlot("bar", arrData, barLayout);
//create bubble data
var bubbleData =[{
x: otuID,
y: sampleValue,
text: otuLabel,
mode: "markers",
marker: {
size: sampleValue,
color: otuID,
colorScale: "Jet"
}
}];
var bubbleLayout = {
title: "OTUs Sample Size"
};
Plotly.newPlot("bubble", bubbleData, bubbleLayout);
//create gauge chart
var gaugeData = [{
type: "indicator",
mode: "gauge+number+delta",
value: wFreq,
title: "Scrub per Week",
gauge: {
axis: { range: [0, 9], tickwidth: 1, tickcolor: "darkblue" },
bar: { color: "darkblue" },
bgcolor: "white",
borderwidth: 2,
bordercolor: "gray",
steps: [
{ range: [0, 5], color: "cyan" },
{ range: [5, 7], color: "lightblue"},
{ range: [7, 9], color: "royalblue" }
],
}
}];
var gaugeLayout = {
title: "Belly Button Washing Frequency",
width: 500,
height: 400,
margin: { t: 25, r: 25, l: 25, b: 25 }
};
Plotly.newPlot("gauge",gaugeData, gaugeLayout)
});
};
function getDemoData(metaID){
d3.json("samples.json").then((importedData) => {
var data = importedData;
console.log(data);
var metaData = data.metadata;
console.log(metaData);
var metaArr = metaData.filter(metaObject => metaObject.id == metaID);
var varArr = metaArr[0];
var ethnicity = varArr.ethnicity;
console.log(ethnicity);
var gender = varArr.gender;
console.log(gender);
var age = varArr.age;
console.log(age);
var location = varArr.location;
console.log(location);
var bbtype = varArr.bbtype;
console.log(bbtype);
var wfreq = varArr.wfreq;
console.log(wfreq);
var metaDataset = d3.select("#sample-metadata");
metaDataset.html("");
Object.entries(varArr).forEach(([key, value]) =>{
metaDataset.append("h6").text(`${key}: ${value}`);
})
});
};
//initalize the page with default plot
function init(){
d3.json("samples.json").then((d) => {
var sampleName = d.names;
var dataset = d3.select("#selDataset");
sampleName.forEach((sample) =>{
dataset.append("option")
.text(sample)
.property("value", sample);
});
var resultSample = sampleName[0];
getData(resultSample);
getDemoData(resultSample);
});
};
init();
d3.selectAll("#selDataset").on("change", optionChanged);
//get new data and create new chart per new data
function optionChanged(newSample){
getData(newSample);
getDemoData(newSample);
};