-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (81 loc) · 2.86 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
// Use D3 to fetch the JSON data
const url = "https://2u-data-curriculum-team.s3.amazonaws.com/dataviz-classroom/v1.1/14-Interactive-Web-Visualizations/02-Homework/samples.json";
// Function to initialize the dashboard
function init() {
d3.json(url).then(data => {
// Populate dropdown menu
var select = d3.select("#selDataset");
data.names.forEach(name => {
select.append("option").text(name).property("value", name);
});
// Use the first sample from the list to build the initial plots
const firstSample = data.names[0];
updateCharts(firstSample);
updateMetadata(firstSample);
});
}
// Function to update the bar and bubble chart
function updateCharts(sample) {
d3.json(url).then(data => {
var samples = data.samples;
var resultArray = samples.filter(sampleObj => sampleObj.id == sample);
var result = resultArray[0];
var otu_ids = result.otu_ids;
var otu_labels = result.otu_labels;
var sample_values = result.sample_values;
// Bar chart
var barData = [{
x: sample_values.slice(0, 10).reverse(),
y: otu_ids.slice(0, 10).map(otuID => `OTU ${otuID}`).reverse(),
text: otu_labels.slice(0, 10).reverse(),
type: "bar",
orientation: "h"
}];
var barLayout = {
title: "Top 10 OTUs Found in Individual",
xaxis: {title: "Sample Values"},
yaxis: {title: "OTU IDs"}
};
Plotly.newPlot("bar", barData, barLayout);
// Bubble chart
var bubbleData = [{
x: otu_ids,
y: sample_values,
text: otu_labels,
mode: 'markers',
marker: {
size: sample_values,
color: otu_ids,
colorscale: "Earth"
}
}];
var bubbleLayout = {
title: 'Bacteria Cultures Per Sample',
showlegend: false,
height: 600,
width: 1200
};
Plotly.newPlot('bubble', bubbleData, bubbleLayout);
});
}
// Function to build the metadata panel
function updateMetadata(sample) {
d3.json(url).then(data => {
var metadata = data.metadata;
var resultArray = metadata.filter(sampleObj => sampleObj.id == sample);
var result = resultArray[0];
var displayPanel = d3.select("#sample-metadata");
displayPanel.html(""); // Clear existing metadata
Object.entries(result).forEach(([key, value]) => {
displayPanel.append("h6").text(`${key.toUpperCase()}: ${value}`);
});
});
}
// Function to handle the change event
function optionChanged(newSample) {
// Update the charts and metadata with the new sample
updateCharts(newSample);
updateMetadata(newSample);
}
// Initialize the dashboard
init();