-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburtin-d3.html
204 lines (174 loc) · 5.93 KB
/
burtin-d3.html
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
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Burtin's Antibiotics</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<!--optional color scales, you can specify your own color scale-->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<style>
.chart {
font: 10px sans-serif;
text-align: left;
padding: 3px;
margin: 1px;
color: black;
height: auto;
width: auto;
border: 1px solid gainsboro;
background: #FCFCF9;
}
.grid line {
stroke: gray;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
</style>
</head>
<body>
<svg class="chart" width=800 height=900 ></svg>
<script>
var chart = d3.select(".chart"),
margin = {top: 50, right: 50, bottom: 50, left: 175},
width = +chart.attr("width") - margin.left - margin.right,
height = +chart.attr("height") - margin.top - margin.bottom;
g = chart.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var y0 = d3.scaleBand().rangeRound([0, height]).paddingInner(0.2); //
var y1 = d3.scaleBand().padding(0); //
var x = d3.scaleLog().range([500, 0]); //
var z = d3.scaleOrdinal().range(["#b4cbea", "#8483ae", "#6c5b77"]); //
var w = d3.scaleOrdinal().range(["beige","gainsboro"]); //
d3.csv("data.csv", (error, data) => {
if (error) throw error;
//------------------------------------------------------------------
function GS(d) { return (d.Gram_Staining === "positive" ? 1 : -1); };
data.sort(GS); // Order by Gram Staining
var keys = data.columns.slice(1).splice(0,3); // Antibiotics
console.log(keys);
var grams = ["Positive", "Negative"]
//------------------------------------------------------------------
y0.domain(data.map((d) => { return d.Bacteria; }));
y1.domain(keys).rangeRound([0, y0.bandwidth()]);
x.domain([
d3.min(data, (d) => { return d3.min(keys, (key) => { return d[key]; }); }),
d3.max(data, (d) => { return d3.max(keys, (key) => { return d[key]; }); })
]);
//==================================================================
// Bars
//==================================================================
//------------------------------------------------------------------
// Gram Staining
g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", (d, i) => { return "translate(0," + y0(d.Bacteria) + ")"; })
.append("rect")
.attr("width", width - 75) // FIXME! Hardcode
.attr("height", y0.bandwidth() + 10) // FIXME! Hardcode
.style("fill", (d) => { return w(d.Gram_Staining) });
//------------------------------------------------------------------
// Gridlines
function make_y_gridlines() {
return d3.axisBottom(x).ticks(5)
}
g.append("g")
.attr("class", "grid")
.call(make_y_gridlines()
.tickSize(height)
.tickFormat("")
);
//------------------------------------------------------------------
// Minimum Inhibitory Concentration (MIC)
g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", (d, i) => { return "translate(0," + y0(d.Bacteria) + ")"; })
.selectAll("rect")
.data((d) => {
return keys.map((key) => {
return {key: key, value: d[key], gs: d['Gram_Staining']};
});
})
.enter().append("rect")
.attr("y", (d) => { return y1(d.key) })
.attr("x", (d, i) => { x(d.value) })
.attr("width", (d) => { return x(d.value); })
.attr("height", y1.bandwidth())
.style("fill", (d, i) => { return z(d.key) });
//==================================================================
// Axes
//==================================================================
//------------------------------------------------------------------
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y0))
// .append("text")
// .text("Bacteria")
// .attr("y", -30)
// .attr("x", 0)
// .attr("dx", "0.32em")
// .attr("fill", "#000")
// .attr("font-weight", "bold")
// .attr("text-anchor", "start")
// .attr("transform", "rotate(270)");
//------------------------------------------------------------------
g.append("g")
.attr("class", "axis")
.call(d3.axisTop(x).ticks(5, ",.3f"))
.append("text")
.text("Antibiotic Effectiveness (Minimum Inhibitory Concentration)")
.attr("y", -30)
.attr("x", 125)
.attr("dx", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start");
//==================================================================
// Legends
//==================================================================
//------------------------------------------------------------------
var abx = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice())
.enter().append("g")
.attr("transform", (d, i) => { return "translate(20," +((i * 20) + 50)+ ")"; });
abx.append("rect")
.attr("x", width - 20)
.attr("width", 20)
.attr("height", 20)
.attr("fill", z);
abx.append("text")
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text((d) => { return d; });
//------------------------------------------------------------------
var gstain = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(grams.reverse())
.enter().append("g")
.attr("transform", (d, i) => { return "translate(20," + ((i * 20) + 500) + ")"; });
gstain.append("rect")
.attr("x", width - 20)
.attr("width", 20)
.attr("height", 20)
.attr("fill", w);
gstain.append("text")
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text((d) => { return d; });
});
</script>
</body>
</html>