-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
133 lines (110 loc) · 3.67 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
// Build the metadata panel
function buildMetadata(sample) {
d3.json("https://static.bc-edx.com/data/dl-1-2/m14/lms/starter/samples.json").then((data) => {
// get the metadata field
var metadata = data.metadata;
// Filter the metadata for the object with the desired sample number
var filteredMetadata = metadata.filter(meta => meta.id.toString() === sample)[0];
// Use d3 to select the panel with id of `#sample-metadata`
var panel = d3.select("#sample-metadata");
// Use `.html("") to clear any existing metadata
panel.html("");
// Inside a loop, you will need to use d3 to append new
// tags for each key-value in the filtered metadata.
Object.entries(filteredMetadata).forEach(([key, value]) => {
panel.append("h6").text(`${key.toUpperCase()}: ${value}`);
});
});
}
// function to build both charts
function buildCharts(sample) {
d3.json("https://static.bc-edx.com/data/dl-1-2/m14/lms/starter/samples.json").then((data) => {
// Get the samples field
var samples = data.samples;
// Filter the samples for the object with the desired sample number
var resultArray = samples.filter(sampleObj => sampleObj.id == sample);
var result = resultArray[0];
// Get the otu_ids, otu_labels, and sample_values
var otu_ids = result.otu_ids;
var otu_labels = result.otu_labels;
var sample_values = result.sample_values;
// Build a Bubble Chart
var bubbleLayout = {
title: "Bacteria Cultures Per Sample",
margin: { t: 0 },
hovermode: "closest",
xaxis: { title: "OTU ID" },
margin: { t: 30}
};
var bubbleData = [
{
x: otu_ids,
y: sample_values,
text: otu_labels,
mode: "markers",
marker: {
size: sample_values,
color: otu_ids,
colorscale: "Earth"
}
}
];
// Render the Bubble Chart
Plotly.newPlot("bubble", bubbleData, bubbleLayout);
// For the Bar Chart, map the otu_ids to a list of strings for your yticks
var yticks = otu_ids.slice(0, 10).map(otuID => `OTU ${otuID}`).reverse();
// Build a Bar Chart
// Don't forget to slice and reverse the input data appropriately
var barData = [
{
y: yticks,
x: sample_values.slice(0, 10).reverse(),
text: otu_labels.slice(0, 10).reverse(),
type: "bar",
orientation: "h",
}
];
// Render the Bar Chart
var barLayout = {
title: "Top 10 Bacteria Cultures Found",
margin: { t: 30, l: 150 },
xaxis: {
title: {
text: 'Number of Bacteria',
},
},
};
Plotly.newPlot("bar", barData, barLayout);
});
}
// Function to run on page load
function init() {
d3.json("https://static.bc-edx.com/data/dl-1-2/m14/lms/starter/samples.json").then((data) => {
// Get the names field
var sampleNames = data.names;
// Use d3 to select the dropdown with id of `#selDataset`
var selector = d3.select("#selDataset");
// Use the list of sample names to populate the select options
// Hint: Inside a loop, you will need to use d3 to append a new
// option for each sample name.
sampleNames.forEach((sample) => {
selector
.append("option")
.text(sample)
.property("value", sample);
});
// Get the first sample from the list
var firstSample = sampleNames[0];
// Build charts and metadata panel with the first sample
buildCharts(firstSample);
buildMetadata(firstSample);
});
}
// Function for event listener
function optionChanged(newSample) {
// Build charts and metadata panel each time a new sample is selected
buildCharts(newSample);
buildMetadata(newSample);
}
// Initialize the dashboard
init();