-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.js
108 lines (92 loc) · 2.88 KB
/
plots.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
function buildMetadata(sample) {
d3.json("samples.json").then((data) => {
var metadata = data.metadata;
// Filter the data for the object with the desired sample number
var resultArray = metadata.filter(sampleObj => sampleObj.id == sample);
var result = resultArray[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("");
// Use `Object.entries` to add each key and value pair to the panel
// Hint: Inside the loop, you will need to use d3 to append new
// tags for each key-value in the metadata.
Object.entries(result).forEach(([key, value]) => {
PANEL.append("h6").text(`${key.toUpperCase()}: ${value}`);
});
// BONUS: Build the Gauge Chart
buildGauge(result.wfreq);
});
}
function buildCharts(sample) {
d3.json("samples.json").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;
// 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"
}
}
];
Plotly.newPlot("bubble", bubbleData, bubbleLayout);
var yticks = otu_ids.slice(0, 10).map(otuID => `OTU ${otuID}`).reverse();
var barData = [
{
y: yticks,
x: sample_values.slice(0, 10).reverse(),
text: otu_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", barData, 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
var 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();