/
BoxplotCellRenderer.ts
303 lines (275 loc) · 10.3 KB
/
BoxplotCellRenderer.ts
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
import { IBoxPlotData, IAdvancedBoxPlotData, round } from '../internal';
import {
NumberColumn,
IBoxPlotColumn,
INumberColumn,
isBoxPlotColumn,
Column,
IDataRow,
isNumberColumn,
isMapAbleColumn,
IOrderedGroup,
} from '../model';
import { BOX_PLOT, CANVAS_HEIGHT, DOT, cssClass } from '../styles';
import { colorOf } from './impose';
import {
IRenderContext,
ERenderMode,
ICellRendererFactory,
IImposer,
ICellRenderer,
IGroupCellRenderer,
ISummaryRenderer,
} from './interfaces';
import { renderMissingCanvas } from './missing';
import { tasksAll } from '../provider';
import { adaptColor, BIG_MARK_LIGHTNESS_FACTOR, SMALL_MARK_LIGHTNESS_FACTOR } from './utils';
const BOXPLOT = `<div title="">
<div class="${cssClass('boxplot-whisker')}">
<div class="${cssClass('boxplot-box')}"></div>
<div class="${cssClass('boxplot-median')}"></div>
<div class="${cssClass('boxplot-mean')}"></div>
</div>
</div>`;
const MAPPED_BOXPLOT = `<div title="">
<div class="${cssClass('boxplot-whisker')}">
<div class="${cssClass('boxplot-box')}"></div>
<div class="${cssClass('boxplot-median')}"></div>
<div class="${cssClass('boxplot-mean')}"></div>
</div>
<span class="${cssClass('mapping-hint')}"></span><span class="${cssClass('mapping-hint')}"></span>
</div>`;
/** @internal */
export function computeLabel(col: INumberColumn, v: IBoxPlotData | IAdvancedBoxPlotData) {
if (v == null) {
return '';
}
const f = col.getNumberFormat();
const mean =
(v as IAdvancedBoxPlotData).mean != null ? `mean = ${f((v as IAdvancedBoxPlotData).mean)} (dashed line)\n` : '';
return `min = ${f(v.min)}\nq1 = ${f(v.q1)}\nmedian = ${f(v.median)}\n${mean}q3 = ${f(v.q3)}\nmax = ${f(v.max)}`;
}
export default class BoxplotCellRenderer implements ICellRendererFactory {
readonly title: string = 'Box Plot';
canRender(col: Column, mode: ERenderMode): boolean {
return (isBoxPlotColumn(col) && mode === ERenderMode.CELL) || (isNumberColumn(col) && mode !== ERenderMode.CELL);
}
create(col: IBoxPlotColumn, context: IRenderContext, imposer?: IImposer): ICellRenderer {
const sortMethod = col.getSortMethod() as keyof IBoxPlotData;
const sortedByMe = col.isSortedByMe().asc !== undefined;
const width = context.colWidth(col);
return {
template: BOXPLOT,
update: (n: HTMLElement, d: IDataRow) => {
const data = col.getBoxPlotData(d);
n.classList.toggle(cssClass('missing'), !data);
if (!data) {
return;
}
const label = col.getRawBoxPlotData(d)!;
renderDOMBoxPlot(col, n, data!, label, sortedByMe ? sortMethod : '', colorOf(col, d, imposer));
},
render: (ctx: CanvasRenderingContext2D, d: IDataRow) => {
if (renderMissingCanvas(ctx, col, d, width)) {
return;
}
// Rectangle
const data = col.getBoxPlotData(d);
if (!data) {
return;
}
const scaled = {
min: data.min * width,
median: data.median * width,
mean: (data as IAdvancedBoxPlotData).mean != null ? (data as IAdvancedBoxPlotData).mean * width : undefined,
q1: data.q1 * width,
q3: data.q3 * width,
max: data.max * width,
outlier: data.outlier ? data.outlier.map((d) => d * width) : undefined,
whiskerLow: data.whiskerLow != null ? data.whiskerLow * width : undefined,
whiskerHigh: data.whiskerHigh != null ? data.whiskerHigh * width : undefined,
};
renderBoxPlot(ctx, scaled, sortedByMe ? sortMethod : '', colorOf(col, d, imposer), CANVAS_HEIGHT, 0);
},
};
}
createGroup(col: INumberColumn, context: IRenderContext, imposer?: IImposer): IGroupCellRenderer {
const sort = col instanceof NumberColumn && col.isGroupSortedByMe().asc !== undefined ? col.getSortMethod() : '';
return {
template: BOXPLOT,
update: (n: HTMLElement, group: IOrderedGroup) => {
return tasksAll([
context.tasks.groupBoxPlotStats(col, group, false),
context.tasks.groupBoxPlotStats(col, group, true),
]).then((data) => {
if (typeof data === 'symbol') {
return;
}
// render
const isMissing =
data == null ||
data[0] == null ||
data[0].group.count === 0 ||
data[0].group.count === data[0].group.missing;
n.classList.toggle(cssClass('missing'), isMissing);
if (isMissing) {
return;
}
renderDOMBoxPlot(col, n, data[0].group, data[1].group, sort, colorOf(col, null, imposer));
});
},
};
}
createSummary(
col: INumberColumn,
context: IRenderContext,
_interactive: boolean,
imposer?: IImposer
): ISummaryRenderer {
return {
template: isMapAbleColumn(col) ? MAPPED_BOXPLOT : BOXPLOT,
update: (n: HTMLElement) => {
return tasksAll([
context.tasks.summaryBoxPlotStats(col, false),
context.tasks.summaryBoxPlotStats(col, true),
]).then((data) => {
if (typeof data === 'symbol') {
return;
}
const isMissing =
data == null ||
data[0] == null ||
data[0].summary.count === 0 ||
data[0].summary.count === data[0].summary.missing;
n.classList.toggle(cssClass('missing'), isMissing);
if (isMissing) {
return;
}
const mappedSummary = data[0].summary;
const rawSummary = data[1].summary;
const sort =
col instanceof NumberColumn && col.isGroupSortedByMe().asc !== undefined ? col.getSortMethod() : '';
if (isMapAbleColumn(col)) {
const range = col.getRange();
Array.from(n.getElementsByTagName('span')).forEach((d: HTMLElement, i) => (d.textContent = range[i]));
}
renderDOMBoxPlot(col, n, mappedSummary, rawSummary, sort, colorOf(col, null, imposer), isMapAbleColumn(col));
});
},
};
}
}
function renderDOMBoxPlot(
col: INumberColumn,
n: HTMLElement,
data: IBoxPlotData | IAdvancedBoxPlotData,
label: IBoxPlotData | IAdvancedBoxPlotData,
sort: string,
color: string | null,
hasRange = false
) {
n.title = computeLabel(col, label);
const whiskers = n.firstElementChild as HTMLElement;
const box = whiskers.firstElementChild as HTMLElement;
const median = box.nextElementSibling as HTMLElement;
const mean = whiskers.lastElementChild as HTMLElement;
const leftWhisker =
data.whiskerLow != null ? data.whiskerLow : Math.max(data.q1 - 1.5 * (data.q3 - data.q1), data.min);
const rightWhisker =
data.whiskerHigh != null ? data.whiskerHigh : Math.min(data.q3 + 1.5 * (data.q3 - data.q1), data.max);
whiskers.style.left = `${round(leftWhisker * 100, 2)}%`;
const range = rightWhisker - leftWhisker;
whiskers.style.width = `${round(range * 100, 2)}%`;
//relative within the whiskers
box.style.left = `${round(((data.q1 - leftWhisker) / range) * 100, 2)}%`;
box.style.width = `${round(((data.q3 - data.q1) / range) * 100, 2)}%`;
box.style.backgroundColor = adaptColor(color, BIG_MARK_LIGHTNESS_FACTOR);
//relative within the whiskers
median.style.left = `${round(((data.median - leftWhisker) / range) * 100, 2)}%`;
if ((data as IAdvancedBoxPlotData).mean != null) {
mean.style.left = `${round((((data as IAdvancedBoxPlotData).mean - leftWhisker) / range) * 100, 2)}%`;
mean.style.display = null;
} else {
mean.style.display = 'none';
}
// match lengths
const outliers = Array.from(n.children).slice(1, hasRange ? -2 : undefined) as HTMLElement[];
const numOutliers = data.outlier ? data.outlier.length : 0;
outliers.splice(numOutliers, outliers.length - numOutliers).forEach((v) => v.remove());
whiskers.dataset.sort = sort;
if (!data.outlier || numOutliers === 0) {
return;
}
for (let i = outliers.length; i < numOutliers; ++i) {
const p = n.ownerDocument!.createElement('div');
p.classList.add(cssClass('boxplot-outlier'));
outliers.unshift(p);
whiskers.insertAdjacentElement('afterend', p);
}
data.outlier.forEach((v, i) => {
delete outliers[i].dataset.sort;
outliers[i].style.left = `${round(v * 100, 2)}%`;
});
if (sort === 'min' && data.outlier[0] <= leftWhisker) {
// first outliers is the min
whiskers.dataset.sort = '';
outliers[0].dataset.sort = 'min';
if (outliers.length > 1) {
// append at the end of the DOM to be on top
outliers[outliers.length - 1].insertAdjacentElement('afterend', outliers[0]);
}
} else if (sort === 'max' && data.outlier[outliers.length - 1] >= rightWhisker) {
// last outlier is the max
whiskers.dataset.sort = '';
outliers[outliers.length - 1].dataset.sort = 'max';
}
}
function renderBoxPlot(
ctx: CanvasRenderingContext2D,
box: IBoxPlotData,
sort: string,
color: string | null,
height: number,
topPadding: number
) {
const left = box.whiskerLow != null ? box.whiskerLow : Math.max(box.q1 - 1.5 * (box.q3 - box.q1), box.min);
const right = box.whiskerHigh != null ? box.whiskerHigh : Math.min(box.q3 + 1.5 * (box.q3 - box.q1), box.max);
ctx.fillStyle = adaptColor(color || BOX_PLOT.box, BIG_MARK_LIGHTNESS_FACTOR);
ctx.strokeStyle = adaptColor(BOX_PLOT.stroke, SMALL_MARK_LIGHTNESS_FACTOR);
ctx.beginPath();
ctx.rect(box.q1, 0, box.q3 - box.q1, height);
ctx.fill();
ctx.stroke();
//Line
const bottomPos = height - topPadding;
const middlePos = height / 2;
ctx.beginPath();
ctx.moveTo(left, middlePos);
ctx.lineTo(box.q1, middlePos);
ctx.moveTo(left, topPadding);
ctx.lineTo(left, bottomPos);
ctx.moveTo(box.median, 0);
ctx.lineTo(box.median, height);
ctx.moveTo(box.q3, middlePos);
ctx.lineTo(right, middlePos);
ctx.moveTo(right, topPadding);
ctx.lineTo(right, bottomPos);
ctx.stroke();
ctx.fill();
if (sort !== '') {
ctx.strokeStyle = BOX_PLOT.sort;
ctx.beginPath();
ctx.moveTo(box[sort as keyof IBoxPlotData] as number, topPadding);
ctx.lineTo(box[sort as keyof IBoxPlotData] as number, height - topPadding);
ctx.stroke();
ctx.fill();
}
if (!box.outlier) {
return;
}
ctx.fillStyle = BOX_PLOT.outlier;
box.outlier.forEach((v) => {
// currently dots with 3px
ctx.fillRect(Math.max(v - DOT.size / 2, 0), middlePos - DOT.size / 2, DOT.size, DOT.size);
});
}