-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
BoxPlotController.ts
106 lines (95 loc) · 3.37 KB
/
BoxPlotController.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
import {
Chart,
BarController,
ControllerDatasetOptions,
ScriptableAndArrayOptions,
CommonHoverOptions,
ChartItem,
ChartConfiguration,
LinearScale,
CategoryScale,
AnimationOptions,
ScriptableContext,
CartesianScaleTypeRegistry,
} from 'chart.js';
import { merge } from 'chart.js/helpers';
import { asBoxPlotStats, IBoxPlot, IBoxplotOptions } from '../data';
import { baseDefaults, StatsBase, defaultOverrides } from './StatsBase';
import { BoxAndWiskers, IBoxAndWhiskersOptions } from '../elements';
import patchController from './patchController';
import { boxOptionsKeys } from '../elements/BoxAndWiskers';
export class BoxPlotController extends StatsBase<IBoxPlot, Required<IBoxplotOptions>> {
/**
* @hidden
*/
// eslint-disable-next-line class-methods-use-this
protected _parseStats(value: unknown, config: IBoxplotOptions): IBoxPlot | undefined {
return asBoxPlotStats(value, config);
}
/**
* @hidden
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
protected _transformStats<T>(target: any, source: IBoxPlot, mapper: (v: number) => T): void {
super._transformStats(target, source, mapper);
for (const key of ['whiskerMin', 'whiskerMax']) {
// eslint-disable-next-line no-param-reassign
target[key] = mapper(source[key as 'whiskerMin' | 'whiskerMax']);
}
}
static readonly id = 'boxplot';
/**
* @hidden
*/
static readonly defaults: any = /* #__PURE__ */ merge({}, [
BarController.defaults,
baseDefaults(boxOptionsKeys),
{
animations: {
numbers: {
type: 'number',
properties: (BarController.defaults as any).animations.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'whiskerMin', 'whiskerMax', 'mean'],
boxOptionsKeys.filter((c) => !c.endsWith('Color'))
),
},
},
dataElementType: BoxAndWiskers.id,
},
]);
/**
* @hidden
*/
static readonly overrides: any = /* #__PURE__ */ merge({}, [(BarController as any).overrides, defaultOverrides()]);
}
export interface BoxPlotControllerDatasetOptions
extends ControllerDatasetOptions,
IBoxplotOptions,
ScriptableAndArrayOptions<IBoxAndWhiskersOptions, ScriptableContext<'boxplot'>>,
ScriptableAndArrayOptions<CommonHoverOptions, ScriptableContext<'boxplot'>>,
AnimationOptions<'boxplot'> {}
export type BoxPlotDataPoint = number[] | (Partial<IBoxPlot> & Pick<IBoxPlot, 'min' | 'max' | 'median' | 'q1' | 'q3'>);
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IBoxPlotChartOptions extends IBoxplotOptions {}
declare module 'chart.js' {
export interface ChartTypeRegistry {
boxplot: {
chartOptions: IBoxPlotChartOptions;
datasetOptions: BoxPlotControllerDatasetOptions;
defaultDataPoint: BoxPlotDataPoint;
scales: keyof CartesianScaleTypeRegistry;
metaExtensions: {};
parsedDataType: IBoxPlot & ChartTypeRegistry['bar']['parsedDataType'];
};
}
}
export class BoxPlotChart<DATA extends unknown[] = BoxPlotDataPoint[], LABEL = string> extends Chart<
'boxplot',
DATA,
LABEL
> {
static id = BoxPlotController.id;
constructor(item: ChartItem, config: Omit<ChartConfiguration<'boxplot', DATA, LABEL>, 'type'>) {
super(item, patchController('boxplot', config, BoxPlotController, BoxAndWiskers, [LinearScale, CategoryScale]));
}
}