Skip to content

Commit 3fac3cc

Browse files
author
Jeremiah R. Hall
committed
add onStatsUpdate prop
1 parent 95bacaa commit 3fac3cc

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.2.0 Release
2+
3+
- adds an `onStatsUpdate` prop which is called on redraw with a { min, max } object containing the min and max number of items found for a single coordinate
4+
15
# 0.1.0 Release
26

37
- Handle invalid points gracefully

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The `HeatmapLayer` component takes the following props:
5656
- `minOpacity`: minimum opacity for heatmap (default: 0.01)
5757
- `blur`: blur for heatmap points (default: 15)
5858
- `gradient`: object defining gradient stop points for heatmap e.g. `{ 0.4: 'blue', 0.8: 'orange', 1.0: 'red' }` (default: `simpleheat` package default gradient)
59+
- `onStatsUpdate`: called on redraw with a { min, max } object containing the min and max number of items found for a single coordinate
5960

6061
## Example
6162

lib/HeatmapLayer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ var HeatmapLayer = function (_MapLayer) {
262262
var roundResults = function roundResults(results) {
263263
return (0, _lodash4.default)(results, function (result, row) {
264264
return (0, _lodash2.default)(filterUndefined(row), function (cell, key, row) {
265-
return [Math.round(cell[0]), Math.round(cell[1]), Math.min(cell[2], maxIntensity)];
265+
return [Math.round(cell[0]), Math.round(cell[1]), Math.min(cell[2], maxIntensity), cell[3]];
266266
}).concat(result);
267267
}, []);
268268
};
@@ -291,11 +291,12 @@ var HeatmapLayer = function (_MapLayer) {
291291
var k = alt * v;
292292

293293
if (!cell) {
294-
grid[y][x] = [p.x, p.y, k];
294+
grid[y][x] = [p.x, p.y, k, 1];
295295
} else {
296296
cell[0] = (cell[0] * cell[2] + p.x * k) / (cell[2] + k); // x
297297
cell[1] = (cell[1] * cell[2] + p.y * k) / (cell[2] + k); // y
298298
cell[2] += k; // accumulated intensity value
299+
cell[3] += 1;
299300
}
300301

301302
return grid;
@@ -315,6 +316,16 @@ var HeatmapLayer = function (_MapLayer) {
315316
this._heatmap.data(data).draw(this.getMinOpacity(this.props));
316317

317318
this._frame = null;
319+
320+
if (this.props.onStatsUpdate && this.props.points && this.props.points.length > 0) {
321+
var stats = _.reduce(data, function (stats, point) {
322+
stats.max = point[3] > stats.max ? point[3] : stats.max;
323+
stats.min = point[3] < stats.min ? point[3] : stats.min;
324+
return stats;
325+
}, { min: Infinity, max: -Infinity });
326+
327+
this.props.onStatsUpdate(stats);
328+
}
318329
};
319330

320331
HeatmapLayer.prototype.render = function render() {
@@ -345,6 +356,7 @@ HeatmapLayer.propTypes = {
345356
intensityExtractor: _react2.default.PropTypes.func.isRequired,
346357
fitBoundsOnLoad: _react2.default.PropTypes.bool,
347358
fitBoundsOnUpdate: _react2.default.PropTypes.bool,
359+
onStatsUpdate: _react2.default.PropTypes.func,
348360
/* props controlling heatmap generation */
349361
max: _react2.default.PropTypes.number,
350362
radius: _react2.default.PropTypes.number,

src/HeatmapLayer.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default class HeatmapLayer extends MapLayer {
7777
intensityExtractor: React.PropTypes.func.isRequired,
7878
fitBoundsOnLoad: React.PropTypes.bool,
7979
fitBoundsOnUpdate: React.PropTypes.bool,
80+
onStatsUpdate: React.PropTypes.func,
8081
/* props controlling heatmap generation */
8182
max: React.PropTypes.number,
8283
radius: React.PropTypes.number,
@@ -262,7 +263,8 @@ export default class HeatmapLayer extends MapLayer {
262263
return [
263264
Math.round(cell[0]),
264265
Math.round(cell[1]),
265-
Math.min(cell[2], maxIntensity)
266+
Math.min(cell[2], maxIntensity),
267+
cell[3]
266268
];
267269
}).concat(result);
268270
}, []);
@@ -291,11 +293,12 @@ export default class HeatmapLayer extends MapLayer {
291293
const k = alt * v;
292294

293295
if (!cell) {
294-
grid[y][x] = [p.x, p.y, k];
296+
grid[y][x] = [p.x, p.y, k, 1];
295297
} else {
296298
cell[0] = (cell[0] * cell[2] + p.x * k) / (cell[2] + k); // x
297299
cell[1] = (cell[1] * cell[2] + p.y * k) / (cell[2] + k); // y
298300
cell[2] += k; // accumulated intensity value
301+
cell[3] += 1;
299302
}
300303

301304
return grid;
@@ -321,6 +324,16 @@ export default class HeatmapLayer extends MapLayer {
321324
this._heatmap.data(data).draw(this.getMinOpacity(this.props));
322325

323326
this._frame = null;
327+
328+
if (this.props.onStatsUpdate && this.props.points && this.props.points.length > 0) {
329+
const stats = _.reduce(data, (stats, point) => {
330+
stats.max = point[3] > stats.max ? point[3] : stats.max;
331+
stats.min = point[3] < stats.min ? point[3] : stats.min;
332+
return stats;
333+
}, { min: Infinity, max: -Infinity });
334+
335+
this.props.onStatsUpdate(stats);
336+
}
324337
}
325338

326339

0 commit comments

Comments
 (0)