Skip to content

Commit 731e86e

Browse files
Merge pull request #21 from fcwheat/fix_heapmap_props
Fix bug where radius, blur, and max were not being used when passed i…
2 parents de3d767 + 47f3d10 commit 731e86e

File tree

5 files changed

+157
-26
lines changed

5 files changed

+157
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.0.2 Release
2+
- Fix bug where radius, blur, and max were not being used when passed in as props.
3+
14
# 1.0.1 Release
25
- Fix bug in componentWillUnmount->safeRemoveLayer where getPanes doesn't return anything so .contains is called on undefined.
36

example/index.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ class MapExample extends React.Component {
99
state = {
1010
mapHidden: false,
1111
layerHidden: false,
12-
addressPoints
12+
addressPoints,
13+
radius: 4,
14+
blur: 8,
15+
max: 0.5,
16+
limitAddressPoints: true,
1317
};
1418

15-
componentDidMount() {
16-
setTimeout(() => {
17-
this.setState({ addressPoints: addressPoints.slice(500, 1000) });
18-
}, 5000);
19+
/**
20+
* Toggle limiting the address points to test behavior with refocusing/zooming when data points change
21+
*/
22+
toggleLimitedAddressPoints() {
23+
if (this.state.limitAddressPoints) {
24+
this.setState({ addressPoints: addressPoints.slice(500, 1000), limitAddressPoints: false });
25+
} else {
26+
this.setState({ addressPoints, limitAddressPoints: true });
27+
}
1928
}
2029

2130
render() {
@@ -48,6 +57,9 @@ class MapExample extends React.Component {
4857
latitudeExtractor={m => m[0]}
4958
gradient={gradient}
5059
intensityExtractor={m => parseFloat(m[2])}
60+
radius={Number(this.state.radius)}
61+
blur={Number(this.state.blur)}
62+
max={Number.parseFloat(this.state.max)}
5163
/>
5264
}
5365
<TileLayer
@@ -65,6 +77,45 @@ class MapExample extends React.Component {
6577
value="Toggle Layer"
6678
onClick={() => this.setState({ layerHidden: !this.state.layerHidden })}
6779
/>
80+
<input
81+
type="button"
82+
value="Toggle Limited Data"
83+
onClick={this.toggleLimitedAddressPoints.bind(this)}
84+
/>
85+
86+
<div>
87+
Radius
88+
<input
89+
type="range"
90+
min={1}
91+
max={40}
92+
value={this.state.radius}
93+
onChange={(e) => this.setState({ radius: e.currentTarget.value })}
94+
/> {this.state.radius}
95+
</div>
96+
97+
<div>
98+
Blur
99+
<input
100+
type="range"
101+
min={1}
102+
max={20}
103+
value={this.state.blur}
104+
onChange={(e) => this.setState({ blur: e.currentTarget.value })}
105+
/> {this.state.blur}
106+
</div>
107+
108+
<div>
109+
Max
110+
<input
111+
type="range"
112+
min={0.1}
113+
max={3}
114+
step={0.1}
115+
value={this.state.max}
116+
onChange={(e) => this.setState({ max: e.currentTarget.value })}
117+
/> {this.state.max}
118+
</div>
68119
</div>
69120
);
70121
}

lib/HeatmapLayer.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ var HeatmapLayer = function (_MapLayer) {
126126
if (this.props.fitBoundsOnLoad) {
127127
this.fitBounds();
128128
}
129-
130129
this.attachEvents();
131130
this.updateHeatmapProps(this.getHeatmapProps(this.props));
132131
};
@@ -163,20 +162,64 @@ var HeatmapLayer = function (_MapLayer) {
163162
};
164163

165164
HeatmapLayer.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
166-
this.updateHeatmapProps(this.getHeatmapProps(nextProps));
165+
var currentProps = this.props;
166+
var nextHeatmapProps = this.getHeatmapProps(nextProps);
167+
168+
this.updateHeatmapGradient(nextHeatmapProps.gradient);
169+
170+
var hasRadiusUpdated = nextHeatmapProps.radius !== currentProps.radius;
171+
var hasBlurUpdated = nextHeatmapProps.blur !== currentProps.blur;
172+
173+
if (hasRadiusUpdated || hasBlurUpdated) {
174+
this.updateHeatmapRadius(nextHeatmapProps.radius, nextHeatmapProps.blur);
175+
}
176+
177+
if (nextHeatmapProps.max !== currentProps.max) {
178+
this.updateHeatmapMax(nextHeatmapProps.max);
179+
}
167180
};
168181

169-
HeatmapLayer.prototype.updateHeatmapProps = function updateHeatmapProps(nextProps) {
170-
if (nextProps.radius && (!this.props || nextProps.radius !== this.props.radius)) {
171-
this._heatmap.radius(nextProps.radius);
182+
/**
183+
* Update various heatmap properties like radius, gradient, and max
184+
*/
185+
186+
187+
HeatmapLayer.prototype.updateHeatmapProps = function updateHeatmapProps(props) {
188+
this.updateHeatmapRadius(props.radius, props.blur);
189+
this.updateHeatmapGradient(props.gradient);
190+
this.updateHeatmapMax(props.max);
191+
};
192+
193+
/**
194+
* Update the heatmap's radius and blur (blur is optional)
195+
*/
196+
197+
198+
HeatmapLayer.prototype.updateHeatmapRadius = function updateHeatmapRadius(radius, blur) {
199+
if (radius) {
200+
this._heatmap.radius(radius, blur);
172201
}
202+
};
203+
204+
/**
205+
* Update the heatmap's gradient
206+
*/
173207

174-
if (nextProps.gradient) {
175-
this._heatmap.gradient(nextProps.gradient);
208+
209+
HeatmapLayer.prototype.updateHeatmapGradient = function updateHeatmapGradient(gradient) {
210+
if (gradient) {
211+
this._heatmap.gradient(gradient);
176212
}
213+
};
214+
215+
/**
216+
* Update the heatmap's maximum
217+
*/
218+
177219

178-
if (nextProps.max && (!this.props || nextProps.max !== this.props.max)) {
179-
this._heatmap.max(nextProps.max);
220+
HeatmapLayer.prototype.updateHeatmapMax = function updateHeatmapMax(maximum) {
221+
if (maximum) {
222+
this._heatmap.max(maximum);
180223
}
181224
};
182225

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-leaflet-heatmap-layer",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A custom layer for heatmaps in react-leaflet",
55
"main": "lib/HeatmapLayer.js",
66
"scripts": {

src/HeatmapLayer.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export default class HeatmapLayer extends MapLayer {
127127
if (this.props.fitBoundsOnLoad) {
128128
this.fitBounds();
129129
}
130-
131130
this.attachEvents();
132131
this.updateHeatmapProps(this.getHeatmapProps(this.props));
133132
}
@@ -164,22 +163,57 @@ export default class HeatmapLayer extends MapLayer {
164163
}
165164

166165
componentWillReceiveProps(nextProps: Object): void {
167-
this.updateHeatmapProps(this.getHeatmapProps(nextProps));
166+
const currentProps = this.props;
167+
const nextHeatmapProps = this.getHeatmapProps(nextProps);
168+
169+
this.updateHeatmapGradient(nextHeatmapProps.gradient);
170+
171+
const hasRadiusUpdated = nextHeatmapProps.radius !== currentProps.radius;
172+
const hasBlurUpdated = nextHeatmapProps.blur !== currentProps.blur;
173+
174+
if (hasRadiusUpdated || hasBlurUpdated) {
175+
this.updateHeatmapRadius(nextHeatmapProps.radius, nextHeatmapProps.blur);
176+
}
177+
178+
if (nextHeatmapProps.max !== currentProps.max) {
179+
this.updateHeatmapMax(nextHeatmapProps.max);
180+
}
181+
182+
}
183+
184+
/**
185+
* Update various heatmap properties like radius, gradient, and max
186+
*/
187+
updateHeatmapProps(props: Object) {
188+
this.updateHeatmapRadius(props.radius, props.blur);
189+
this.updateHeatmapGradient(props.gradient);
190+
this.updateHeatmapMax(props.max);
168191
}
169192

170-
updateHeatmapProps(nextProps: Object) {
171-
if (nextProps.radius
172-
&& (!this.props || nextProps.radius !== this.props.radius)) {
173-
this._heatmap.radius(nextProps.radius);
193+
/**
194+
* Update the heatmap's radius and blur (blur is optional)
195+
*/
196+
updateHeatmapRadius(radius: number, blur: ?number): void {
197+
if (radius) {
198+
this._heatmap.radius(radius, blur);
174199
}
200+
}
175201

176-
if (nextProps.gradient) {
177-
this._heatmap.gradient(nextProps.gradient);
202+
/**
203+
* Update the heatmap's gradient
204+
*/
205+
updateHeatmapGradient(gradient: Object): void {
206+
if (gradient) {
207+
this._heatmap.gradient(gradient);
178208
}
209+
}
179210

180-
if (nextProps.max
181-
&& (!this.props || nextProps.max !== this.props.max)) {
182-
this._heatmap.max(nextProps.max);
211+
/**
212+
* Update the heatmap's maximum
213+
*/
214+
updateHeatmapMax(maximum: number): void {
215+
if (maximum) {
216+
this._heatmap.max(maximum);
183217
}
184218
}
185219

0 commit comments

Comments
 (0)