-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
220 lines (176 loc) · 7.92 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
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
// Render default visuals
init();
// Function to render all visuals before anything is selected in the dropdown menu
function init() {
// Load data
d3.json("data/samples.json").then((data) => {
// Append test subject IDs into the HTML <select> tag so we have a complete dropdown menu
var dropdownMenu = d3.select("#selDataset");
// Append <option> tags with test subject IDs
data.samples.forEach((subject) => {
var option = dropdownMenu.append("option");
option.text(subject.id).attr("value", `${subject.id}`);
})
// Select an initial ID for rendering all visuals
selectedId = getId();
// ----------------------------------------------
// Panel: Demographic Info
panel = d3.select("#sample-metadata");
// Filter to include only the metadata for the selected subject ID
filteredMetaData = data.metadata.filter(subject => subject.id == selectedId);
// Append <p> tags with key-value paired metadata
Object.entries(filteredMetaData[0]).forEach(([key, value]) => {
var demInfo = panel.append("p");
demInfo.text(`${key}: ${value}`);
})
// ----------------------------------------------
// Plot 1: Horizontal Bar chart
// Filter to include only the data for the selected subject ID
var filteredData = data.samples.filter(subject => subject.id === selectedId);
var traceBar = {
x: filteredData[0].sample_values.slice(0, 10).map(value => value).reverse(),
y: filteredData[0].otu_ids.slice(0, 10).map(id => `OTU ${id}`).reverse(),
text: filteredData[0].otu_labels.slice(0, 10).map(label => label).reverse(),
type: "bar",
orientation: "h"
};
var dataBar = [traceBar];
var layoutBar = {
title: `Test Subject ${selectedId}'s Top 10 OTUs`,
xaxis: { title: "Sample Values"},
width: 500,
height: 470,
margin: {
l: 120,
r: 150,
b: 80,
t: 50
}
};
// Render the plot in the corresponding html element
Plotly.newPlot("bar", dataBar, layoutBar, {displayModeBar: false});
// ----------------------------------------------
// Plot 2: Bubble chart for each test subject's sample data
var traceBubble = {
x: filteredData[0].otu_ids,
y: filteredData[0].sample_values,
text: filteredData[0].otu_labels,
mode: 'markers',
marker: {
color: filteredData[0].otu_ids,
size: filteredData[0].sample_values
}
};
var dataBubble = [traceBubble];
var layoutBubble = {
title: `Test Subject ${selectedId}'s Sample Values by OTU ID`,
xaxis: { title: "OTU ID"}
};
// Render the plot in the corresponding html element
Plotly.newPlot("bubble", dataBubble, layoutBubble, {displaylogo: false});
// ----------------------------------------------
// Plot 3: Gauge chart for each test subject's belly button weekly washing frequency
var wfreq = filteredMetaData[0].wfreq;
var dataGauge = [
{
domain: { x: [0, 1], y: [0, 1] },
value: wfreq,
title: { text: "<b>Belly Button Washing Frequency</b><br>Scrubs per Week" },
type: "indicator",
mode: "gauge+number",
gauge: {
axis: {range: [0, d3.max(data.metadata.map(subject => subject.wfreq))], tickmode: "linear"},
bar: { color: "#db5773" },
borderwidth: 2,
bordercolor: "#f0f0f1",
steps: [
{range: [0, 1], color: "#f0f6fc"},
{range: [1, 2], color: "#c5d9ed"},
{range: [2, 3], color: "#9ec2e6"},
{range: [3, 4], color: "#72aee6"},
{range: [4, 5], color: "#4f94d4"},
{range: [5, 6], color: "#3582c4"},
{range: [6, 7], color: "#2271b1"},
{range: [7, 8], color: "#135e96"},
{range: [8, 9], color: "#0a4b78"}
]
}
}
];
// Render the plot in the corresponding html element
Plotly.newPlot("gauge", dataGauge);
// Adjust gauge chart title position...
d3.selectAll("text>tspan.line").attr("y", "-20");
// Customize the color of the gauge chart's number indicator
d3.selectAll("g>text.number").style("fill", "#db5773");
})
}
// Function to grab the first ID for rendering the default bar chart when page is loaded
function getId() {
var dropdownMenu = d3.select("#selDataset");
var selectedId = dropdownMenu.property("value");
return selectedId;
}
// Function to re-render visuals when an option (ID) is selected (or changed)
function optionChanged(selectedId) {
// Load data
d3.json("data/samples.json").then((data) => {
// ----------------------------------------------
// Update panel
panel = d3.select("#sample-metadata");
// Clear the exisiting default children <p> tags from the <div>
panel.html("")
// Filter to include only the metadata for the selected subject ID
filteredMetaData = data.metadata.filter(subject => subject.id == selectedId);
// Append <p> tags with key-value paired metadata
Object.entries(filteredMetaData[0]).forEach(([key, value]) => {
var demInfo = panel.append("p");
demInfo.text(`${key}: ${value}`);
})
// ----------------------------------------------
// Update bar chart
// Filter to include only the data for the selected subject ID
var filteredData = data.samples.filter(subject => subject.id === selectedId);
var xBar = filteredData[0].sample_values.slice(0, 10).map(value => value).reverse();
var yBar = filteredData[0].otu_ids.slice(0, 10).map(id => `OTU ${id}`).reverse();
var textBar = filteredData[0].otu_labels.slice(0, 10).map(label => label).reverse();
var dataUpdateBar = {
"x": [xBar],
"y": [yBar],
"text": [textBar]
};
var layoutUpdateBar = {
title: `Test Subject ${selectedId}'s Top 10 OTUs`
};
// Update the bar chart with data plus title to reflect the selected ID
Plotly.update("bar", dataUpdateBar, layoutUpdateBar);
// ----------------------------------------------
// Update bubble chart
var xBubble = filteredData[0].otu_ids;
var yBubble = filteredData[0].sample_values;
var textBubble = filteredData[0].otu_labels;
var colorBubble = filteredData[0].otu_ids;
var sizeBubble = filteredData[0].sample_values;
var dataUpdateBubble = {
"x": [xBubble],
"y": [yBubble],
"text": [textBubble],
"marker.color": [colorBubble],
"marker.size": [sizeBubble]
};
var layoutUpdateBubble = {
title: `Test Subject ${selectedId}'s Sample Values by OTU ID`
};
// Update the bubble chart with data plus title to reflect the selected ID
Plotly.update("bubble", dataUpdateBubble, layoutUpdateBubble);
// ----------------------------------------------
// Update gauge chart
var wfreq = filteredMetaData[0].wfreq;
// Restyle gauge chart
Plotly.restyle("gauge", "value", [wfreq]);
// Re-adjust gauge chart title position...
d3.selectAll("text>tspan.line").attr("y", "-20");
// Customize the color of the gauge chart's number indicator
d3.selectAll("g>text.number").style("fill", "#db5773");
})
}