-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
GeoController.ts
273 lines (238 loc) · 7.5 KB
/
GeoController.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
import {
DatasetController,
ChartDataset,
ScriptableAndArrayOptions,
UpdateMode,
Element,
VisualElement,
ScriptableContext,
ChartTypeRegistry,
AnimationOptions,
} from 'chart.js';
import { clipArea, unclipArea, valueOrDefault } from 'chart.js/helpers';
import { geoGraticule, geoGraticule10, ExtendedFeature } from 'd3-geo';
import { ProjectionScale } from '../scales';
import type { GeoFeature, IGeoFeatureOptions } from '../elements';
export const geoDefaults = {
showOutline: false,
showGraticule: false,
clipMap: true,
};
export const geoOverrides = {
scales: {
projection: {
axis: 'x',
type: ProjectionScale.id,
position: 'chartArea',
display: false,
},
},
};
function patchDatasetElementOptions(options: any) {
// patch the options by removing the `outline` or `hoverOutline` option;
// see https://github.com/chartjs/Chart.js/issues/7362
const r: any = { ...options };
Object.keys(options).forEach((key) => {
let targetKey = key;
if (key.startsWith('outline')) {
const sub = key.slice('outline'.length);
targetKey = sub[0].toLowerCase() + sub.slice(1);
} else if (key.startsWith('hoverOutline')) {
targetKey = `hover${key.slice('hoverOutline'.length)}`;
} else {
return;
}
delete r[key];
r[targetKey] = options[key];
});
return r;
}
export class GeoController<
TYPE extends keyof ChartTypeRegistry,
TElement extends Element & VisualElement,
> extends DatasetController<TYPE, TElement, GeoFeature> {
getGeoDataset(): ChartDataset<'choropleth' | 'bubbleMap'> & IGeoControllerDatasetOptions {
return super.getDataset() as unknown as ChartDataset<'choropleth' | 'bubbleMap'> & IGeoControllerDatasetOptions;
}
getGeoOptions(): IGeoChartOptions {
return this.chart.options as unknown as IGeoChartOptions;
}
getProjectionScale(): ProjectionScale {
return this.getScaleForId('projection') as ProjectionScale;
}
linkScales(): void {
const dataset = this.getGeoDataset();
const meta = this.getMeta();
meta.xAxisID = 'projection';
dataset.xAxisID = 'projection';
meta.yAxisID = 'projection';
dataset.yAxisID = 'projection';
meta.xScale = this.getScaleForId('projection');
meta.yScale = this.getScaleForId('projection');
this.getProjectionScale().computeBounds(this.resolveOutline());
}
showOutline(): IGeoChartOptions['showOutline'] {
return valueOrDefault(this.getGeoDataset().showOutline, this.getGeoOptions().showOutline);
}
clipMap(): IGeoChartOptions['clipMap'] {
return valueOrDefault(this.getGeoDataset().clipMap, this.getGeoOptions().clipMap);
}
getGraticule(): IGeoChartOptions['showGraticule'] {
return valueOrDefault(this.getGeoDataset().showGraticule, this.getGeoOptions().showGraticule);
}
update(mode: UpdateMode): void {
super.update(mode);
const meta = this.getMeta();
const scale = this.getProjectionScale();
const dirtyCache = scale.updateBounds();
if (this.showOutline()) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const elem = meta.dataset!;
if (dirtyCache) {
delete elem.cache;
}
elem.projectionScale = scale;
elem.pixelRatio = this.chart.currentDevicePixelRatio;
if (mode !== 'resize') {
const options = patchDatasetElementOptions(this.resolveDatasetElementOptions(mode));
const properties = {
feature: this.resolveOutline(),
options,
};
this.updateElement(elem, undefined, properties, mode);
if (this.getGraticule()) {
(meta as any).graticule = options;
}
}
} else if (this.getGraticule() && mode !== 'resize') {
(meta as any).graticule = patchDatasetElementOptions(this.resolveDatasetElementOptions(mode));
}
this.updateElements(meta.data, 0, meta.data.length, mode);
if (dirtyCache) {
// eslint-disable-next-line no-param-reassign
meta.data.forEach((elem) => delete (elem as any).cache);
}
}
resolveOutline(): any {
const ds = this.getGeoDataset();
const outline = ds.outline || { type: 'Sphere' };
if (Array.isArray(outline)) {
return {
type: 'FeatureCollection',
features: outline,
};
}
return outline;
}
showGraticule(): void {
const g = this.getGraticule();
const options = (this.getMeta() as any).graticule;
if (!g || !options) {
return;
}
const { ctx } = this.chart;
const scale = this.getProjectionScale();
const path = scale.geoPath.context(ctx);
ctx.save();
ctx.beginPath();
if (typeof g === 'boolean') {
if (g) {
path(geoGraticule10());
}
} else {
const geo = geoGraticule();
if (g.stepMajor) {
geo.stepMajor(g.stepMajor as unknown as [number, number]);
}
if (g.stepMinor) {
geo.stepMinor(g.stepMinor as unknown as [number, number]);
}
path(geo());
}
ctx.strokeStyle = options.graticuleBorderColor;
ctx.lineWidth = options.graticuleBorderWidth;
ctx.stroke();
ctx.restore();
}
draw(): void {
const { chart } = this;
const clipMap = this.clipMap();
// enable clipping based on the option
let enabled = false;
if (clipMap === true || clipMap === 'outline' || clipMap === 'outline+graticule') {
enabled = true;
clipArea(chart.ctx, chart.chartArea);
}
if (this.showOutline() && this.getMeta().dataset) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(this.getMeta().dataset!.draw.call as any)(this.getMeta().dataset!, chart.ctx, chart.chartArea);
}
if (clipMap === true || clipMap === 'graticule' || clipMap === 'outline+graticule') {
if (!enabled) {
clipArea(chart.ctx, chart.chartArea);
}
} else if (enabled) {
enabled = false;
unclipArea(chart.ctx);
}
this.showGraticule();
if (clipMap === true || clipMap === 'items') {
if (!enabled) {
clipArea(chart.ctx, chart.chartArea);
}
} else if (enabled) {
enabled = false;
unclipArea(chart.ctx);
}
this.getMeta().data.forEach((elem) => (elem.draw.call as any)(elem, chart.ctx, chart.chartArea));
if (enabled) {
enabled = false;
unclipArea(chart.ctx);
}
}
}
export interface IGeoChartOptions {
/**
* Outline used to scale and centralize the projection in the chart area.
* By default a sphere is used
* @default { type: 'Sphere" }
*/
outline: any[];
/**
* option to render the outline in the background, see also the outline... styling option
* @default false
*/
showOutline: boolean;
/**
* option to render a graticule in the background, see also the outline... styling option
* @default false
*/
showGraticule:
| boolean
| {
stepMajor: [number, number];
stepMinor: [number, number];
};
/**
* option whether to clip the rendering to the chartArea of the graph
* @default choropleth: true bubbleMap: 'outline+graticule'
*/
clipMap: boolean | 'outline' | 'graticule' | 'outline+graticule' | 'items';
}
export interface IGeoControllerDatasetOptions
extends IGeoChartOptions,
ScriptableAndArrayOptions<IGeoFeatureOptions, ScriptableContext<'choropleth' | 'bubbleMap'>>,
AnimationOptions<'choropleth' | 'bubbleMap'> {
xAxisID?: string;
yAxisID?: string;
rAxisID?: string;
iAxisID?: string;
vAxisID?: string;
}
export interface IGeoDataPoint {
feature: ExtendedFeature;
center?: {
longitude: number;
latitude: number;
};
}