This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericHistogram.vue
333 lines (299 loc) · 8.74 KB
/
GenericHistogram.vue
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<template>
<div class="datahistclass"></div>
</template>
<script>
const d3 = require("d3v4");
export default {
props: {
chartData: {
type: Array,
default: undefined,
},
title: {
type: String,
default: "",
},
xlabel: {
type: String,
default: "",
},
plotHistogram: {
type: Boolean,
default: true,
},
plotKDE: {
type: Boolean,
default: false,
},
markMedianBar: {
type: Boolean,
default: false,
},
minWidth: {
type: Number,
default: 200,
},
minHeight: {
type: Number,
default: 200,
},
selectedLines: {
type: Array,
default: null,
},
margin: {
type: Object,
default: function () {
return { top: 20, right: 10, bottom: 50, left: 50 };
},
},
},
data: function () {
return {
minValue: 0,
maxValue: 1,
};
},
watch: {
chartData: function () {
this.init();
},
selectedLines: function (newSelectedLines) {
this.drawLines(newSelectedLines);
},
},
methods: {
drawPlot: function (data) {
var sortedData = [...data];
sortedData.sort();
//console.log(data)
this.maxValue = Math.max.apply(Math, sortedData);
this.minValue = Math.min.apply(Math, sortedData);
// console.log(max)
// console.log((Math.round(max*10)/10)+0.1)
// console.log(min)
var nclass = 20;
// ref: https://bl.ocks.org/d3noob/96b74d0bd6d11427dd797892551a103c
var width = this.minWidth - this.margin.left - this.margin.right;
var height = this.minHeight - this.margin.top - this.margin.bottom;
// ceil / floor to a multiple of 0.1
var maxX = Math.ceil(this.maxValue / 0.1) * 0.1;
var minX = Math.floor(this.minValue / 0.1) * 0.1;
// set the ranges
var x = d3.scaleLinear().domain([minX, maxX]).rangeRound([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var thresholds = x.ticks(nclass - 1);
// set the parameters for the histogram
var histogram = d3
.histogram()
.value(function (d) {
return d;
})
.domain(x.domain())
.thresholds(thresholds);
nclass = x.ticks(nclass - 1).length;
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3
.select(this.$el)
.append("svg")
.attr("class", "HistogramContainer")
.attr("width", width + this.margin.left + this.margin.right)
.attr("height", height + this.margin.top + this.margin.bottom)
.append("g")
.attr(
"transform",
"translate(" + this.margin.left + "," + this.margin.top + ")"
);
// group the data for the bars
var bins = histogram(sortedData);
var maxFrequency = d3.max(bins, function (d) {
return d.length;
});
// Scale the range of the data in the y domain
y.domain([0, maxFrequency]);
if (this.plotHistogram) {
var i = 0;
// append the bar rectangles to the svg element
svg
.selectAll("rect")
.data(bins)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", 1)
.attr("transform", function (d) {
return "translate(" + x(d.x0) + "," + y(d.length) + ")";
})
.attr("width", function (d) {
return x(d.x1) - x(d.x0) - 1;
})
.attr("height", function (d) {
return height - y(d.length);
})
.attr("id", function () {
return "bar" + i++;
})
.style("fill", "#CCCCCC"); // blue: #0073CF
}
if (this.plotKDE) {
// Kernel density estimate graph
var bandwidth = ((maxX - minX) / nclass) * 2;
var density = this.kde(
this.epanechnikov(),
thresholds,
sortedData,
bandwidth
);
var line = d3
.line()
.curve(d3.curveBasis)
.x(function (d) {
return x(d[0]);
})
.y(function (d) {
return y(d[1]);
});
svg
.append("path")
.datum(density)
.attr("fill", "none")
.attr("stroke", "#000")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("d", line);
}
// add the x Axis
svg
.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg
.append("text")
.attr(
"transform",
"translate(" +
width / 2 +
" ," +
(height + this.margin.top + 20) +
")"
)
.style("text-anchor", "middle")
.text(this.xlabel);
// add the y Axis
svg.append("g").call(d3.axisLeft(y));
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - this.margin.left)
.attr("x", 0 - height / 2)
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Frequency");
svg.append("g").attr("class", "lines");
// colour median bar red
if (this.markMedianBar) {
var median = this.getMedian(sortedData);
// console.log(median)
var medBarId = this.findMedBar(maxX, minX, nclass, median);
//console.log(medBarId)
svg.select("#bar" + medBarId).style("fill", "#C4071B");
}
},
kde: function (kernel, thresholds, data, bandwidth) {
var binWidth = thresholds[1] - thresholds[0];
return thresholds.map((t) => [
t,
d3.sum(data, function (d) {
return kernel((t - d) / bandwidth) / bandwidth;
}) * binWidth,
]);
},
epanechnikov: function () {
return function (x) {
return Math.abs(x) <= 1 ? 0.75 * (1 - x * x) : 0;
};
},
drawLines: function (oLines) {
var width = this.minWidth - this.margin.left - this.margin.right;
var height = this.minHeight - this.margin.top - this.margin.bottom;
// ceil / floor to a multiple of 0.1
var maxX = Math.ceil(this.maxValue / 0.1) * 0.1;
var minX = Math.floor(this.minValue / 0.1) * 0.1;
var x = d3.scaleLinear().domain([minX, maxX]).rangeRound([0, width]);
var allLines = d3.select(this.$el).select(".lines");
allLines.selectAll("line").remove();
allLines.selectAll("path").remove();
var lines = allLines.selectAll("line").data(oLines).enter();
lines
.append("line")
.attr("x1", function (d) {
return x(d.value);
})
.attr("y1", 0)
.attr("x2", function (d) {
return x(d.value);
})
.attr("y2", height)
.style("stroke-width", 2)
.style("stroke", function (d) {
return d.color;
})
.style("stroke-dasharray", function (d) {
return d.dash;
})
.style("fill", "none");
lines
.append("path")
.attr("d", function (d) {
return d.marker;
})
.attr("transform", function (d) {
return "translate(" + x(d.value) + "," + height * 0.25 + ")";
})
.style("fill", function (d) {
return d.color;
});
},
getMedian: function (valarray) {
var medval;
if (valarray.length % 2 === 0) {
// if even
var val1where = valarray.length / 2;
var val2where = valarray.length / 2 + 1;
var val1 = valarray[val1where];
var val2 = valarray[val2where];
medval = (val1 + val2) / 2;
} else {
var where = (valarray.length + 1) / 2;
medval = valarray[where];
}
return medval;
},
findMedBar: function (maxX, minX, nclass, median) {
var size = (maxX - minX) / nclass;
var found, i;
for (i = 0; i <= nclass; i++) {
if (minX + size * i < median && median < minX + size * (i + 1)) {
found = i;
}
}
return found;
},
init: function () {
d3.select(this.$el).select("svg").remove();
var oData = this.chartData;
if (typeof oData !== "undefined" && oData.length > 0) {
this.drawPlot(oData);
}
},
},
mounted: function () {
this.init();
},
};
</script>
<style>
@import "./GenericHistogram.css.prdb";
</style>