-
Notifications
You must be signed in to change notification settings - Fork 4
/
FeaturePropHistogram.svelte
128 lines (111 loc) · 3.34 KB
/
FeaturePropHistogram.svelte
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
<div>
{#if showHeader}
<div>histogram: {numQuantiles} buckets, {step.toFixed(1)} wide</div>
{/if}
<table style="width: 100%">
{#if outsideRange[0]}
<tr>
<td>{outsideRange[0]}x</td>
<td></td>
<td style="width: 35%">< {formatNumber(minFilter, step)}</td>
</tr>
{/if}
{#each quantiles as { from, to, fromColor, toColor, count, percent }}
<tr>
<td>{count}x</td>
<td style="width: 65%">
<div style="width: {percent}%; height: 15px;
background: {
(fromColor && toColor) ? `linear-gradient(90deg, ${fromColor}, ${toColor})` : 'lightblue'
}
"> </div>
</td>
<td style="width: 35%">{formatNumber(from, step)} ⇢ {formatNumber(to, step)}</td>
</tr>
{/each}
{#if outsideRange[1]}
<tr>
<td>{outsideRange[1]}x</td>
<td></td>
<td style="width: 35%">> {formatNumber(maxFilter+1, step)}</td>
</tr>
{/if}
</table>
</div>
<script>
import { parseNumber } from './utils';
export default {
data() {
return {
numQuantiles: 7,
minFilter: null,
maxFilter: null,
showHeader: true
}
},
computed: {
range: ({ minFilter, maxFilter }) => maxFilter+1 - minFilter,
step: ({ numQuantiles, range }) => range / numQuantiles,
// track values above and below the filter range
outsideRange: ({ minFilter, maxFilter, valueCounts }) => {
let below = 0, above = 0;
valueCounts.forEach(([value, count]) => {
value = parseNumber(value);
if (value < minFilter) {
below += count;
}
else if (value > maxFilter) {
above += count;
}
});
return [below, above];
},
// calculate buckets for data by range and number of quantiles
quantiles: ({ numQuantiles, minFilter, range, step, valueCounts, valueColorFunction }) => {
if (!valueCounts || !range) {
return [];
}
// add up the things in each bucket
const bucket = [];
for (let i = 0; i < numQuantiles; i++) {
bucket[i] = valueCounts.reduce((total, [value, count]) => {
value = parseNumber(value);
if ((value >= (step * i) + minFilter) && (value < (step * (i+1)) + minFilter)) {
total += count;
}
return total;
}, 0);
}
const quantileMax = Math.max(...bucket);
const quantilePercent = bucket.map(x => x / quantileMax);
return quantilePercent.map((x, index) => {
const count = bucket[index];
const columns = Math.ceil(x*numQuantiles);
const from = (index*step + minFilter);
const to = ((index+1)*step + minFilter);
const percent = columns / numQuantiles * 100;
const fromColor = valueColorFunction(from);
const toColor = valueColorFunction(to);
return {
from,
to,
fromColor,
toColor,
count,
percent
};
});
}
},
helpers: {
formatNumber(value, step) {
if (Math.floor(value) === value) {
return value.toFixed(0); // show integers as integers
}
// adapt the number of digits displayed to the data resolution
const digits = Math.max(0, Math.ceil(Math.log10(1/step)))
return value.toFixed(digits);
}
}
};
</script>