在基组之间切换时是否可以在多个图例之间切换?

时间:2018-09-18 19:21:09

标签: r leaflet r-leaflet

我正在使用r中的传单来可视化一些矢量数据,其中包含用户可能感兴趣的多个非空间变量。我想允许用户选择一个确定要素颜色的变量。我使用baseGroups函数的addLayersControl参数来完成此操作,其中每个组都是具有不同调色板的相同数据。这对于切换要素本身的颜色效果很好,但不允许我在图例之间进行切换-即使我在group中使用了适当的addLegend,也始终显示添加的每个图例。请参见下面的示例代码和屏幕截图:

data <- data.frame(long = c(-93.2, -93, -93.5), lat = c(44.9, 45, 44.9), 
               var1 = c(1,2,3), var2 = c(10, 9, 1))

pal1 <- colorNumeric(palette = "Blues", domain = data$var1)
pal2 <- colorNumeric(palette = "Reds", domain = data$var2)

leaflet(data) %>%
  addCircleMarkers(color = ~pal1(var1), group = "var1") %>%
  addCircleMarkers(color = ~pal2(var2), group = "var2") %>%
  addLegend(pal = pal1, values = ~var1, group = "var1") %>%
  addLegend(pal = pal2, values = ~var2, group = "var2") %>%
  addLayersControl(baseGroups = c("var1", "var2"), position = "topleft")

如果在图层控件中将baseGroups替换为overlayGroups,则此操作按预期运行,并且仅显示所选组的图例。但是,此选项并不理想,因为我不希望用户能够选择多个组或取消选择所有组。

enter image description here

These questions与我的非常相似,但是公认的解决方案都使用overlayGroups,而我想坚持使用baseGroups。我还希望尽可能避免使用光泽。

1 个答案:

答案 0 :(得分:1)

似乎baseGroups中的图例不会像在overlayGroups中那样被删除/重新添加,即使调用hideGroup("var1")之后,图例的持久性也可以进一步证明这一点。

一个粗略的解决方法是添加一个事件处理程序,以使用baseGroups作为键,根据当前选择的group = "<groupName>"组来隐藏/取消隐藏图例,而无需进行其他任何更改。例如:

htmlwidgets::onRender("
    function(el, x) {
      var updateLegend = function () {
          var selectedGroup = document.querySelectorAll('input:checked')[0].nextSibling.innerText.substr(1);

          document.querySelectorAll('.legend').forEach(a => a.hidden=true);
          document.querySelectorAll('.legend').forEach(l => {
            if (l.children[0].children[0].innerText == selectedGroup) l.hidden=false;
          });
      };
      updateLegend();
      this.on('baselayerchange', e => updateLegend());
    }")

演示

demo of workaround

演示来源

require(leaflet)


data <- data.frame(long = c(-93.2, -93, -93.5), lat = c(44.9, 45, 44.9), 
                   var1 = c(1,2,3), var2 = c(10, 9, 1))

pal1 <- colorNumeric(palette = "Blues", domain = data$var1)
pal2 <- colorNumeric(palette = "Reds", domain = data$var2)

leaflet(data) %>%
  addCircleMarkers(color = ~pal1(var1), group = "var1") %>%
  addCircleMarkers(color = ~pal2(var2), group = "var2") %>%
  addLegend(pal = pal1, values = ~var1, group = "var1") %>%
  addLegend(pal = pal2, values = ~var2, group = "var2") %>%
  addLayersControl(baseGroups = c("var1", "var2"), 
                   position = "topleft",
                   options = layersControlOptions(collapsed=F)) %>%
  htmlwidgets::onRender("
    function(el, x) {
      var updateLegend = function () {
          var selectedGroup = document.querySelectorAll('input:checked')[0].nextSibling.innerText.substr(1);

          document.querySelectorAll('.legend').forEach(a => a.hidden=true);
          document.querySelectorAll('.legend').forEach(l => {
            if (l.children[0].children[0].innerText == selectedGroup) l.hidden=false;
          });
      };
      updateLegend();
      this.on('baselayerchange', e => updateLegend());
    }")