Skip to content

[WIP] Add setPathStyle(map, group, pathOptions()) #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export(renderLeaflet)
export(safeLabel)
export(scaleBarOptions)
export(setMaxBounds)
export(setStyle)
export(setStyleFast)
export(setView)
export(showGroup)
export(tileOptions)
Expand Down
25 changes: 25 additions & 0 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ setView <- function(map, lng, lat, zoom, options = list()) {
)
}

#' Efficiently style a group that has already been added to the map
#'
#' Call with a group and a vector of style lists of length N. The first N
#' features of the group will be restyled.
#'
#' @examples
#' \donttest{
#' renderLeaflet("map", {
#' leaflet() %>% addPolygons(data = zones, group = "zones", color = "red")
#' })
#' colour = "blue"
#' styles = lapply(pal(values), function(colour) {list(fillColor=colour, color=colour)})
#' leafletProxy("map") %>%
#' setStyle("zones", styles)
#' }
#' @export
setStyle = function(map, group, styles, label = NULL, offset = 0) {
invokeMethod(map, NULL, "setStyle", group, styles, label, offset - 1)
}

#' @export
setStyleFast = function(map, group, color = NULL, weight = NULL, label = NULL, stroke = NULL, fill = NULL) {
invokeMethod(map, NULL, "setStyleFast", group, color, weight, label, stroke, fill)
}

#' @describeIn map-methods Flys to a given location/zoom-level using smooth pan-zoom.
#' @export
flyTo <- function(map, lng, lat, zoom, options = list()) {
Expand Down
71 changes: 69 additions & 2 deletions inst/htmlwidgets/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,13 @@ _htmlwidgets2.default.widget({
_shiny2.default.onInputChange(map.id + "_click", {
lat: e.latlng.lat,
lng: e.latlng.lng,
".nonce": Math.random() // Force reactivity if lat/lng hasn't changed
".nonce": Math.random(), // Force reactivity if lat/lng hasn't changed
modifiers: {
alt: e.originalEvent.altKey,
ctrl: e.originalEvent.ctrlKey,
meta: e.originalEvent.metaKey,
shift: e.originalEvent.shiftKey
}
});
});

Expand Down Expand Up @@ -1282,6 +1288,61 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var methods = {};
exports.default = methods;

/** Much more performant way to style loaded geometry */

methods.setStyle = function (group, styles, labels) {
var offset = arguments.length <= 3 || arguments[3] === undefined ? 0 : arguments[3];

window.map = this;
var layers = this.layerManager.getLayerGroup(group).getLayers();

if (styles) {
for (var i = 0; i < styles.length; i++) {
layers[i + offset].setStyle(styles[i]);
}
}
if (labels) {
for (var _i = 0; _i < styles.length; _i++) {
layers[_i + offset].bindTooltip(labels[_i]);
}
}
};

/** Much more performant way to style loaded geometry */
methods.setStyleFast = function (group, colors, weights, labels, strokes, fills) {
window.map = this;
var layers = this.layerManager.getLayerGroup(group).getLayers();

if (labels) {
for (var i = 0; i < labels.length; i++) {
layers[i].bindTooltip(labels[i]);
}
}

if (colors) {
for (var _i2 = 0; _i2 < colors.length; _i2++) {
layers[_i2].setStyle({ color: colors[_i2], fillColor: colors[_i2] });
}
}

if (weights) {
for (var _i3 = 0; _i3 < weights.length; _i3++) {
layers[_i3].setStyle({ weight: weights[_i3] });
}
}

if (strokes) {
for (var _i4 = 0; _i4 < strokes.length; _i4++) {
layers[_i4].setStyle({ stroke: strokes[_i4] });
}
}

if (fills) {
for (var _i5 = 0; _i5 < fills.length; _i5++) {
layers[_i5].setStyle({ fill: fills[_i5] });
}
}
};

function mouseHandler(mapId, layerId, group, eventName, extraInfo) {
return function (e) {
Expand All @@ -1297,7 +1358,13 @@ function mouseHandler(mapId, layerId, group, eventName, extraInfo) {
}
var eventInfo = _jquery2.default.extend({
id: layerId,
".nonce": Math.random() // force reactivity
".nonce": Math.random(), // force reactivity
modifiers: {
alt: e.originalEvent.altKey,
ctrl: e.originalEvent.ctrlKey,
meta: e.originalEvent.metaKey,
shift: e.originalEvent.shiftKey
}
}, group !== null ? { group: group } : null, latLng, extraInfo);

_shiny2.default.onInputChange(mapId + "_" + eventName, eventInfo);
Expand Down
8 changes: 7 additions & 1 deletion javascript/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ HTMLWidgets.widget({
Shiny.onInputChange(map.id + "_click", {
lat: e.latlng.lat,
lng: e.latlng.lng,
".nonce": Math.random() // Force reactivity if lat/lng hasn't changed
".nonce": Math.random(), // Force reactivity if lat/lng hasn't changed
modifiers: {
alt: e.originalEvent.altKey,
ctrl: e.originalEvent.ctrlKey,
meta: e.originalEvent.metaKey,
shift: e.originalEvent.shiftKey
}
});
});

Expand Down
60 changes: 59 additions & 1 deletion javascript/src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,58 @@ import Mipmapper from "./mipmapper";
let methods = {};
export default methods;

/** Much more performant way to style loaded geometry */
methods.setStyle = function(group, styles, labels, offset = 0) {
window.map = this;
let layers = this.layerManager.getLayerGroup(group).getLayers();

if (styles) {
for (let i = 0; i < styles.length; i++) {
layers[i + offset].setStyle(styles[i]);
}
}
if (labels) {
for (let i = 0; i < styles.length; i++) {
layers[i + offset].bindTooltip(labels[i]);
}
}
};

/** Much more performant way to style loaded geometry */
methods.setStyleFast = function(group, colors, weights, labels, strokes, fills) {
window.map = this;
let layers = this.layerManager.getLayerGroup(group).getLayers();

if (labels) {
for (let i = 0; i < labels.length; i++) {
layers[i].bindTooltip(labels[i]);
}
}

if (colors) {
for (let i = 0; i < colors.length; i++) {
layers[i].setStyle({color: colors[i], fillColor: colors[i]});
}
}

if (weights) {
for (let i = 0; i < weights.length; i++) {
layers[i].setStyle({weight: weights[i]});
}
}

if (strokes) {
for (let i = 0; i < strokes.length; i++) {
layers[i].setStyle({stroke: strokes[i]});
}
}

if (fills) {
for (let i = 0; i < fills.length; i++) {
layers[i].setStyle({fill: fills[i]});
}
}
};

function mouseHandler(mapId, layerId, group, eventName, extraInfo) {
return function(e) {
Expand All @@ -29,7 +81,13 @@ function mouseHandler(mapId, layerId, group, eventName, extraInfo) {
let eventInfo = $.extend(
{
id: layerId,
".nonce": Math.random() // force reactivity
".nonce": Math.random(), // force reactivity
modifiers: {
alt: e.originalEvent.altKey,
ctrl: e.originalEvent.ctrlKey,
meta: e.originalEvent.metaKey,
shift: e.originalEvent.shiftKey
}
},
group !== null ? {group: group} : null,
latLng,
Expand Down
23 changes: 23 additions & 0 deletions man/setStyle.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.