generated from sgratzl/ts-library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathVennDiagramController.ts
214 lines (188 loc) · 6.21 KB
/
VennDiagramController.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
import {
Chart,
DatasetController,
TooltipItem,
UpdateMode,
ChartItem,
ScriptableAndArrayOptions,
ControllerDatasetOptions,
CommonHoverOptions,
ChartConfiguration,
LinearScale,
ScriptableContext,
Scale,
CoreChartOptions,
CartesianScaleTypeRegistry,
} from 'chart.js';
import { ArcSlice, IArcSliceOptions } from '../elements';
import layout, { IVennDiagramLayout } from '../model/layout';
import type { IArcSlice, IBoundingBox, ICircle, IEllipse } from '../model/interfaces';
import patchController from './patchController';
import { ISet } from 'src/data';
export class VennDiagramController extends DatasetController<'venn', ArcSlice> {
static readonly id: string = 'venn';
/**
* @hidden
*/
static readonly defaults = {
dataElementType: ArcSlice.id,
};
/**
* @hidden
*/
static readonly overrides: any = {
plugins: {
tooltip: {
callbacks: {
title() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
},
label(item: TooltipItem<'venn'>) {
const labels = item.chart.data.labels as string[];
const d = item.chart.data.datasets?.[item.datasetIndex].data?.[item.dataIndex] as any;
return `${labels[item.dataIndex]}: ${d ? d.values || d.value.toLocaleString() : ''}`;
},
},
},
},
scales: {
x: {
type: 'linear',
min: -1,
max: 1,
display: false,
},
y: {
type: 'linear',
min: -1,
max: 1,
display: false,
},
},
};
/**
* @hidden
*/
initialize(): void {
super.initialize();
this.enableOptionSharing = true;
}
/**
* @hidden
*/
update(mode: UpdateMode): void {
super.update(mode);
const meta = this._cachedMeta;
const slices = (meta.data || []) as unknown as ArcSlice[];
this.updateElements(slices, 0, slices.length, mode);
}
protected computeLayout(size: IBoundingBox): IVennDiagramLayout {
const nSets = Math.log2(this._cachedMeta.data.length + 1);
return layout(nSets, size);
}
/**
* @hidden
*/
updateElements(slices: ArcSlice[], start: number, count: number, mode: UpdateMode): void {
const xScale = this._cachedMeta.xScale as Scale & { left: number; right: number };
const yScale = this._cachedMeta.yScale as Scale & { top: number; bottom: number };
const w = xScale.right - xScale.left;
const h = yScale.bottom - yScale.top;
const l = this.computeLayout({
x: xScale.left,
y: yScale.top,
width: w,
height: h,
});
(this._cachedMeta as any)._layout = l;
(this._cachedMeta as any)._setLayoutFont = {
...(xScale as any)._resolveTickFontOptions(0),
color: (xScale as any).options.ticks.color,
};
(this._cachedMeta as any)._labelLayoutFont = {
...(yScale as any)._resolveTickFontOptions(0),
color: (yScale as any).options.ticks.color,
};
const firstOpts = this.resolveDataElementOptions(start, mode);
const sharedOptions = this.getSharedOptions(firstOpts) as any;
const includeOptions = this.includeOptions(mode, sharedOptions);
for (let i = start; i < start + count; i += 1) {
const slice = slices[i];
const properties: IArcSlice & { options?: IArcSliceOptions; refs: (ICircle | IEllipse)[] } = {
refs: l.sets,
...l.intersections[i],
};
if (includeOptions) {
properties.options = sharedOptions || (this.resolveDataElementOptions(i, mode) as unknown as IArcSliceOptions);
}
this.updateElement(slice, i, properties as any, mode);
}
this.updateSharedOptions(sharedOptions, mode, firstOpts);
}
/**
* @hidden
*/
draw(): void {
const meta = this._cachedMeta;
const elements = meta.data;
const { ctx } = this.chart;
elements.forEach((elem) => elem.draw(ctx));
this.drawLabels(ctx);
}
private drawLabels(ctx: CanvasRenderingContext2D): void {
const meta = this._cachedMeta;
ctx.save();
const l = (meta as any)._layout as IVennDiagramLayout;
const setLayoutScale = meta.xScale as LinearScale;
const setLayoutFont = (meta as any)._setLayoutFont;
const labelLayoutScale = meta.yScale as LinearScale;
const labelLayoutFont = (meta as any)._labelLayoutFont;
if (labelLayoutScale?.options.ticks.display) {
// set labels
ctx.font = labelLayoutFont.string;
ctx.fillStyle = labelLayoutFont.color;
ctx.textBaseline = 'middle';
const labels = this.chart.data.labels as string[];
l.sets.forEach((set, i) => {
ctx.textAlign = set.align === 'middle' ? 'center' : set.align;
ctx.textBaseline = set.verticalAlign;
ctx.fillText(labels[i], set.text.x, set.text.y);
});
}
if (setLayoutScale?.options.ticks.display) {
ctx.font = setLayoutFont.string;
ctx.fillStyle = setLayoutFont.color;
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const values = (this.getDataset() as any).data as { value: number }[];
l.intersections.forEach((intersection, i) => {
ctx.fillText(values[i].value.toLocaleString(), intersection.text.x, intersection.text.y);
});
}
ctx.restore();
}
}
export interface IVennDiagramControllerDatasetOptions
extends ControllerDatasetOptions,
ScriptableAndArrayOptions<IArcSliceOptions, ScriptableContext<'venn'>>,
ScriptableAndArrayOptions<CommonHoverOptions, ScriptableContext<'venn'>> {}
declare module 'chart.js' {
interface ChartTypeRegistry {
venn: {
chartOptions: CoreChartOptions<'venn'>;
datasetOptions: IVennDiagramControllerDatasetOptions;
defaultDataPoint: number | ISet<number>;
metaExtensions: Record<string, never>;
parsedDataType: { x: number; y: number };
scales: keyof CartesianScaleTypeRegistry;
};
}
}
export class VennDiagramChart<DATA extends unknown[] = number[], LABEL = string> extends Chart<'venn', DATA, LABEL> {
static id = VennDiagramController.id as 'venn';
constructor(item: ChartItem, config: Omit<ChartConfiguration<'venn', DATA, LABEL>, 'type'>) {
super(item, patchController('venn', config, VennDiagramController, ArcSlice, [LinearScale]));
}
}