-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (113 loc) · 3.29 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
const url = "https://2u-data-curriculum-team.s3.amazonaws.com/dataviz-classroom/v1.1/14-Interactive-Web-Visualizations/02-Homework/samples.json"
d3.json(url).then(function(data)
{
console.log(data);
});
function init()
{
let dropdownMenu = d3.select("#selDataset");
d3.json(url).then((data) =>
{
let names = data.names;
names.forEach((id) =>
{
console.log(id);
dropdownMenu.append("option")
.text(id)
.property("value",id);
});
let sample_one = names[0];
console.log(sample_one);
buildMetadata(sample_one);
buildBarChart(sample_one);
buildBubbleChart(sample_one);
buildGaugeChart(sample_one);
});
};
function buildMetadata(sample)
{
d3.json(url).then((data) =>
{
let metadata = data.metadata;
let value = metadata.filter(result => result.id == sample);
console.log(value)
let valueData = value[0];
d3.select("#sample-metadata").html("");
Object.entries(valueData).forEach(([key,value]) =>
{
console.log(key,value);
d3.select("#sample-metadata").append("h5").text(`${key}: ${value}`);
});
});
};
function buildBarChart(sample)
{
d3.json(url).then((data) =>
{
let sampleInfo = data.samples;
let value = sampleInfo.filter(result => result.id == sample);
let valueData = value[0];
let otu_ids = valueData.otu_ids;
let otu_labels = valueData.otu_labels;
let sample_values = valueData.sample_values;
console.log(otu_ids,otu_labels,sample_values);
let yticks = otu_ids.slice(0,10).map(id => `OTU ${id}`).reverse();
let xticks = sample_values.slice(0,10).reverse();
let labels = otu_labels.slice(0,10).reverse();
let trace =
{
x: xticks,
y: yticks,
text: labels,
type: "bar",
orientation: "h"
};
let layout =
{
title: "Top 10 OTUs"
};
Plotly.newPlot("bar", [trace], layout)
});
};
function buildBubbleChart(sample)
{
d3.json(url).then((data) =>
{
let sampleInfo = data.samples;
let value = sampleInfo.filter(result => result.id == sample);
let valueData = value[0];
let otu_ids = valueData.otu_ids;
let otu_labels = valueData.otu_labels;
let sample_values = valueData.sample_values;
console.log(otu_ids,otu_labels,sample_values);
let trace1 =
{
x: otu_ids,
y: sample_values,
text: otu_labels,
mode: "markers",
marker:
{
size: sample_values,
color: otu_ids,
colorscale: "Earth"
}
};
let layout =
{
title: "Bacteria Per Sample",
hovermode: "closest",
xaxis: {title: "OTU ID"},
};
Plotly.newPlot("bubble", [trace1], layout)
});
};
function optionChanged(value)
{
console.log(value);
buildMetadata(value);
buildBarChart(value);
buildBubbleChart(value);
buildGaugeChart(value);
};
init();