-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
123 lines (97 loc) · 2.94 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
// Belly Button Biodiversity - Plotly.js
//samples.json> got data for names, metadata, samples
// BONUS: Build the Gauge Chart
function buildMetadata(sample) {
d3.json("samples.json").then((data) => {
var metadata= data.metadata;
var resultsarray= metadata.filter(sampleobject =>
sampleobject.id == sample);
var result= resultsarray[0]
var panel = d3.select("#sample-metadata");
panel.html("");
Object.entries(result).forEach(([key, value]) => {
panel.append("h6").text(`${key}: ${value}`);
});
//buildGauge(result.wfreq)
});
}
//function buildGauge(wfreq) {}
function buildCharts(sample) {
// Use `d3.json` to fetch the sample data for the plots
d3.json("samples.json").then((data) => {
var samples= data.samples;
var resultsarray= samples.filter(sampleobject =>
sampleobject.id == sample);
var result= resultsarray[0]
var ids = result.otu_ids;
var labels = result.otu_labels;
var values = result.sample_values;
//------------------------------------------------------//
//------------------------------------------------------//
// Build a BUBBLE Chart
//------------------------------------------------------//
//------------------------------------------------------//
var LayoutBubble = {
margin: { t: 0 },
xaxis: { title: "OTU ID" },
hovermode: "closest",
};
var DataBubble = [
{
x: ids,
y: values,
text: labels,
mode: "markers",
marker: {
color: ids,
size: values,
}
}
];
Plotly.newPlot("bubble", DataBubble, LayoutBubble);
//---------------------------------------------------------//
//---------------------------------------------------------//
// Build a BAR Chart
//---------------------------------------------------------//
//---------------------------------------------------------//
var bar_data =[
{
y:ids.slice(0, 10).map(otuID => `OTU ${otuID}`).reverse(),
x:values.slice(0,10).reverse(),
text:labels.slice(0,10).reverse(),
type:"bar",
orientation:"h"
}
];
var barLayout = {
title: "Top 10 Bacteria Cultures Found",
margin: { t: 30, l: 150 }
};
Plotly.newPlot("bar", bar_data, barLayout);
});
}
function init() {
// Grab a reference to the dropdown select element
var selector = d3.select("#selDataset");
// Use the list of sample names to populate the select options
d3.json("samples.json").then((data) => {
var sampleNames = data.names;
sampleNames.forEach((sample) => {
selector
.append("option")
.text(sample)
.property("value", sample);
});
// Use the first sample from the list to build the initial plots
const firstSample = sampleNames[0];
buildCharts(firstSample);
buildMetadata(firstSample);
});
}
function optionChanged(newSample) {
// Fetch new data each time a new sample is selected
buildCharts(newSample);
buildMetadata(newSample);
}
// Initialize the dashboard
init();