-
Notifications
You must be signed in to change notification settings - Fork 5
/
bonus.js
230 lines (178 loc) · 5.71 KB
/
bonus.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Belly Button Biodiversity - Plotly.js
//samples.json> got data for names, metadata, samples
// BONUS: Build the Gauge Chart
// Color palette for Gauge Chart
var arrColorsG = ["#5899DA", "#E8743B", "#19A979", "#ED4A7B", "#945ECF", "#13A4B4", "#525DF4", "#BF399E", "#6C8893", "white"];
// Use`d 3.json` to fetch the metadata for a sample
// Hint: Inside the loop, you will need to use d3 to append new
// tags for each key-value in the metadata.
function buildMetadata(sample) {
d3.json("samples.json").then((data) => {
var metadata= data.metadata;
var resultsarray= metadata.filter(sampleobject =>
sampleobject.id == sample);
var result= resultsarray[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
Object.entries(result).forEach(([key, value]) => {
panel.append("h6").text(`${key}: ${value}`);
});
//buildGauge(result.wfreq)
});
}
//=============Gauge Chart Function=======================//
function buildGaugeChart(sample) {
console.log("sample", sample);
d3.json("samples.json").then(data =>{
var objs = data.metadata;
//console.log("objs", objs);
var matchedSampleObj = objs.filter(sampleData =>
sampleData["id"] === parseInt(sample));
//console.log("buildGaugeChart matchedSampleObj", matchedSampleObj);
gaugeChart(matchedSampleObj[0]);
});
}
//======================================================//
//=============== Build a GAUGE Chart ==================//
//======================================================//
function gaugeChart(data) {
console.log("gaugeChart", data);
if(data.wfreq === null){
data.wfreq = 0;
}
let degree = parseInt(data.wfreq) * (180/10);
// Trig to calc meter point
let degrees = 180 - degree;
let radius = .5;
let radians = degrees * Math.PI / 180;
let x = radius * Math.cos(radians);
let y = radius * Math.sin(radians);
let mainPath = 'M -.0 -0.025 L .0 0.025 L ',
pathX = String(x),
space = ' ',
pathY = String(y),
pathEnd = ' Z';
let path = mainPath.concat(pathX, space, pathY, pathEnd);
let trace = [{ type: 'scatter',
x: [0], y:[0],
marker: {size: 50, color:'2F6497'},
showlegend: false,
name: 'WASH FREQ',
text: data.wfreq,
hoverinfo: 'text+name'},
{ values: [1, 1, 1, 1, 1, 1, 1, 1, 1, 9],
rotation: 90,
text: ['8-9', '7-8', '6-7', '5-6', '4-5', '3-4', '2-3', '1-2', '0-1',''],
textinfo: 'text',
textposition:'inside',
textfont:{
size : 16,
},
marker: {colors:[...arrColorsG]},
labels: ['8-9', '7-8', '6-7', '5-6', '4-5', '3-4', '2-3', '2-1', '0-1',''],
hoverinfo: 'text',
hole: .5,
type: 'pie',
showlegend: false
}];
let layout = {
shapes:[{
type: 'path',
path: path,
fillcolor: '#2F6497',
line: {
color: '#2F6497'
}
}],
title: '<b>Belly Button Washing Frequency</b> <br> <b>Scrub Per Week</b>',
height: 550,
width: 550,
xaxis: {zeroline:false, showticklabels:false,
showgrid: false, range: [-1, 1]},
yaxis: {zeroline:false, showticklabels:false,
showgrid: false, range: [-1, 1]},
};
Plotly.newPlot('gauge', trace, layout, {responsive: true});
}
//=============Bubble&Bar Chart Functions=======================//
function buildCharts(sample) {
// Use `d3.json` to fetch the sample data for the plots
d3.json("samples.json").then((data) => {
var samples= data.samples;
var resultsarray= samples.filter(sampleobject =>
sampleobject.id == sample);
var result= resultsarray[0]
var ids = result.otu_ids;
var labels = result.otu_labels;
var values = result.sample_values;
//======================================================//
//================ Build a BUBBLE Chart=================//
//======================================================//
var LayoutBubble = {
margin: { t: 0 },
xaxis: { title: "OTU ID" },
hovermode: "closest",
};
var DataBubble = [
{
x: ids,
y: values,
text: labels,
mode: "markers",
marker: {
color: ids,
size: values,
}
}
];
Plotly.newPlot("bubble", DataBubble, LayoutBubble);
//=========================================================//
//=============== Build a BAR Chart=======================//
//=========================================================//
var bar_data =[
{
y:ids.slice(0, 10).map(otuID => `OTU ${otuID}`).reverse(),
x:values.slice(0,10).reverse(),
text: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", bar_data, barLayout);
});
}
//============= Function init =======================//
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
const firstSample = sampleNames[0];
buildMetadata(firstSample);
buildCharts(firstSample);
buildGaugeChart(firstSample)
});
}
function optionChanged(newSample) {
// Get new data each time a new sample is selected
buildMetadata(newSample);
buildCharts(newSample);
buildGaugeChart(newSample)
}
// Initialize the dashboard
init();