Skip to content

Commit

Permalink
fix: better clear cache detection
Browse files Browse the repository at this point in the history
closes #239
  • Loading branch information
sgratzl committed Dec 17, 2024
1 parent 9b143bd commit c03fb09
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
5 changes: 3 additions & 2 deletions samples/geo.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>

<body>
<div>
<div style="width: 90vw; height: 90vh">
<canvas id="canvas"></canvas>
</div>
<script>
Expand All @@ -31,7 +31,8 @@
],
},
options: {
lscales: {
// responsive: true,
scales: {
projection: {
axis: 'x',
projection: 'albersUsa',
Expand Down
11 changes: 7 additions & 4 deletions src/controllers/ChoroplethController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ export class ChoroplethController extends GeoController<'choropleth', GeoFeature
for (let i = start; i < start + count; i += 1) {
const elem = elems[i];
elem.projectionScale = scale;
elem.feature = (this as any)._data[i].feature;
elem.center = (this as any)._data[i].center;
elem.pixelRatio = this.chart.currentDevicePixelRatio;
const center = elem.getCenterPoint();
const center = elem.updateExtras({
scale,
feature: (this as any)._data[i].feature,
center: (this as any)._data[i].center,
pixelRatio: this.chart.currentDevicePixelRatio,
mode,
});

const properties: IGeoFeatureProps & { options?: PointOptions } = {
x: center.x,
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/GeoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class GeoController<
const meta = this.getMeta();

const scale = this.getProjectionScale();
const dirtyCache = scale.updateBounds();
const dirtyCache = scale.updateBounds() || mode === 'resize' || mode === 'reset';

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

this.updateElements(meta.data, 0, meta.data.length, mode);
if (dirtyCache) {
meta.data.forEach((elem) => delete (elem as any).cache);
}
this.updateElements(meta.data, 0, meta.data.length, mode);
}

resolveOutline(): any {
Expand Down
32 changes: 32 additions & 0 deletions src/elements/GeoFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ScriptableAndArrayOptions,
CommonHoverOptions,
ScriptableContext,
UpdateMode,
} from 'chart.js';
import { geoContains, GeoPath, GeoProjection } from 'd3-geo';
import type { ProjectionScale } from '../scales';
Expand Down Expand Up @@ -103,6 +104,37 @@ export class GeoFeature extends Element<IGeoFeatureProps, IGeoFeatureOptions> im
*/
pixelRatio?: number;

updateExtras({
scale,
feature,
center,
pixelRatio,
mode,
}: {
scale: ProjectionScale;
feature: Feature;
center?: { longitude: number; latitude: number };
pixelRatio: number;
mode: UpdateMode;
}): Point {
const changed =
mode === 'resize' ||
mode === 'reset' ||
this.projectionScale !== scale ||
this.feature !== feature ||
this.center?.longitude !== center?.longitude ||
this.center?.latitude !== center?.latitude ||
this.pixelRatio !== pixelRatio;
this.projectionScale = scale;
this.feature = feature;
this.center = center;
this.pixelRatio = pixelRatio;
if (changed) {
this.cache = undefined;
}
return this.getCenterPoint();
}

/**
* @hidden
*/
Expand Down

0 comments on commit c03fb09

Please sign in to comment.