Skip to content

Commit c03fb09

Browse files
committed
fix: better clear cache detection
closes #239
1 parent 9b143bd commit c03fb09

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

samples/geo.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77

88
<body>
9-
<div>
9+
<div style="width: 90vw; height: 90vh">
1010
<canvas id="canvas"></canvas>
1111
</div>
1212
<script>
@@ -31,7 +31,8 @@
3131
],
3232
},
3333
options: {
34-
lscales: {
34+
// responsive: true,
35+
scales: {
3536
projection: {
3637
axis: 'x',
3738
projection: 'albersUsa',

src/controllers/ChoroplethController.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ export class ChoroplethController extends GeoController<'choropleth', GeoFeature
6868
for (let i = start; i < start + count; i += 1) {
6969
const elem = elems[i];
7070
elem.projectionScale = scale;
71-
elem.feature = (this as any)._data[i].feature;
72-
elem.center = (this as any)._data[i].center;
73-
elem.pixelRatio = this.chart.currentDevicePixelRatio;
74-
const center = elem.getCenterPoint();
71+
const center = elem.updateExtras({
72+
scale,
73+
feature: (this as any)._data[i].feature,
74+
center: (this as any)._data[i].center,
75+
pixelRatio: this.chart.currentDevicePixelRatio,
76+
mode,
77+
});
7578

7679
const properties: IGeoFeatureProps & { options?: PointOptions } = {
7780
x: center.x,

src/controllers/GeoController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class GeoController<
9898
const meta = this.getMeta();
9999

100100
const scale = this.getProjectionScale();
101-
const dirtyCache = scale.updateBounds();
101+
const dirtyCache = scale.updateBounds() || mode === 'resize' || mode === 'reset';
102102

103103
if (this.showOutline()) {
104104
const elem = meta.dataset!;
@@ -122,10 +122,10 @@ export class GeoController<
122122
(meta as any).graticule = patchDatasetElementOptions(this.resolveDatasetElementOptions(mode));
123123
}
124124

125-
this.updateElements(meta.data, 0, meta.data.length, mode);
126125
if (dirtyCache) {
127126
meta.data.forEach((elem) => delete (elem as any).cache);
128127
}
128+
this.updateElements(meta.data, 0, meta.data.length, mode);
129129
}
130130

131131
resolveOutline(): any {

src/elements/GeoFeature.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ScriptableAndArrayOptions,
99
CommonHoverOptions,
1010
ScriptableContext,
11+
UpdateMode,
1112
} from 'chart.js';
1213
import { geoContains, GeoPath, GeoProjection } from 'd3-geo';
1314
import type { ProjectionScale } from '../scales';
@@ -103,6 +104,37 @@ export class GeoFeature extends Element<IGeoFeatureProps, IGeoFeatureOptions> im
103104
*/
104105
pixelRatio?: number;
105106

107+
updateExtras({
108+
scale,
109+
feature,
110+
center,
111+
pixelRatio,
112+
mode,
113+
}: {
114+
scale: ProjectionScale;
115+
feature: Feature;
116+
center?: { longitude: number; latitude: number };
117+
pixelRatio: number;
118+
mode: UpdateMode;
119+
}): Point {
120+
const changed =
121+
mode === 'resize' ||
122+
mode === 'reset' ||
123+
this.projectionScale !== scale ||
124+
this.feature !== feature ||
125+
this.center?.longitude !== center?.longitude ||
126+
this.center?.latitude !== center?.latitude ||
127+
this.pixelRatio !== pixelRatio;
128+
this.projectionScale = scale;
129+
this.feature = feature;
130+
this.center = center;
131+
this.pixelRatio = pixelRatio;
132+
if (changed) {
133+
this.cache = undefined;
134+
}
135+
return this.getCenterPoint();
136+
}
137+
106138
/**
107139
* @hidden
108140
*/

0 commit comments

Comments
 (0)