-
Notifications
You must be signed in to change notification settings - Fork 7
/
charts.js
200 lines (177 loc) · 6.25 KB
/
charts.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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);
});
}
// Initialize the dashboard
init();
function optionChanged(newSample) {
// Fetch new data each time a new sample is selected
buildMetadata(newSample);
buildCharts(newSample);
}
// Demographics Panel
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}`);
});
});
}
// 1. Create the buildCharts function.
function buildCharts(sample) {
// 2. Use d3.json to load and retrieve the samples.json file
d3.json("samples.json").then((data) => {
// 3. Create a variable that holds the samples array.
console.log(data);
var samplesArray = data.samples;
console.log(samplesArray);
// 4. Create a variable that filters the samples for the object with the desired sample number.
var selectedIdSamples = samplesArray.filter(data => data.id == sample);
console.log(selectedIdSamples);
// 5. Create a variable that holds the first sample in the array.
var firstSample = selectedIdSamples[0];
console.log(firstSample);
// 6. Create variables that hold the otu_ids, otu_labels, and sample_values.
var otuIds = firstSample.otu_ids;
var otuLabels = firstSample.otu_labels;
var sampleValues = firstSample.sample_values;
console.log(otuIds);
console.log(otuLabels);
//console.log("hello");
console.log(sampleValues);
// 7. Create the yticks for the bar chart.
// Hint: Get the the top 10 otu_ids and map them in descending order
// so the otu_ids with the most bacteria are last.
var yticks = otuIds.slice(0,10).map(id => "OTU " + id).reverse();
console.log(yticks);
// 8. Create the trace for the bar chart.
var barData = [{
x: sampleValues.slice(0,10).reverse(),
text: otuLabels.slice(0,10).reverse(),
type: "bar"
}];
// 9. Create the layout for the bar chart.
var barLayout = {
title: "Top 10 Bacteria Cultures Found",
yaxis: {
tickmode: "array",
tickvals: [0,1,2,3,4,5,6,7,8,9],
ticktext: yticks
},
annotations: [{
xref: 'paper',
yref: 'paper',
x: 0.5,
xanchor: 'center',
y: -0.25,
yanchor: 'center',
text: 'The bar chart displays the top 10 bacterial species (OTUs)<br>with the number of samples found in your belly button',
showarrow: false
}]
};
//console.log("hello");
// 10. Use Plotly to plot the data with the layout.
Plotly.newPlot("bar", barData, barLayout, {responsive: true});
// Bar and Bubble charts
// Create the buildCharts function.
//function buildCharts(sample) {
// Use d3.json to load and retrieve the samples.json file
//d3.json("samples.json").then((data) => {
// 1. Create the trace for the bubble chart.
var bubbleData = [{
x: otuIds,
y: sampleValues,
text: otuLabels,
mode: 'markers',
marker: {
size: sampleValues,
color: otuIds,
colorscale: "Earth"
}
}];
console.log(bubbleData);
// 2. Create the layout for the bubble chart.
var bubbleLayout = {
title: 'Bacteria Cultures Per Sample',
showlegend: false,
xaxis: {title: "OTU ID", automargin: true},
yaxis: {automargin: true},
//margin: { t: 50, r: 50, l: 50, b: 50 },
hovermode: "closest"
};
console.log(bubbleLayout);
// 3. Use Plotly to plot the data with the layout.
Plotly.newPlot("bubble", bubbleData, bubbleLayout, {responsive: true});
// 1. Create a variable that filters the metadata array for the object with the desired sample number.
var metadata_SelId = data.metadata.filter(data => data.id == sample);
console.log(metadata_SelId);
// 3. Create a variable that holds the washing frequency.
var washFreq = +metadata_SelId[0].wfreq;
// 4. Create the trace for the gauge chart.
var gaugeData = [
{
domain: { x: [0, 1], y: [0, 1] },
value: washFreq,
title: { text: "<b>Belly Button Washing Frequency</b><br>Scrubs per week"},
type: "indicator",
mode: "gauge+number",
gauge: {
axis: {
range: [null, 10],
tickmode: "array",
tickvals: [0,2,4,6,8,10],
ticktext: [0,2,4,6,8,10]
},
bar: {color: "black"},
steps: [
{ range: [0, 2], color: "red" },
{ range: [2, 4], color: "orange" },
{ range: [4, 6], color: "yellow" },
{ range: [6, 8], color: "lime" },
{ range: [8, 10], color: "green" }]
}
}
];
// 5. Create the layout for the gauge chart.
var gaugeLayout = {
autosize: true,
annotations: [{
xref: 'paper',
yref: 'paper',
x: 0.5,
xanchor: 'center',
y: 0,
yanchor: 'center',
text: "The gauge displays your belly button weekly washing frequency",
showarrow: false
}]
};
// 6. Use Plotly to plot the gauge data and layout.
Plotly.newPlot("gauge", gaugeData, gaugeLayout, {responsive: true});
});
}