制作特定于图层的图例(隐藏未激活图层的图例)

问题描述 投票:0回答:1

我正在制作一张需要有七个基础图层的地图。图例太多,无法全部显示在屏幕上 - 这使得有必要添加地图功能,使图例仅在其基础层被激活时出现。

到目前为止,使用

htmlwidgets
,我已经让所有图例消失了,但我无法让激活层的图例变得可见。

以下是图层控件:

  addLayersControl(                                                #layer control 
    baseGroups = c("Parishes, plain",
                   "Unemployment rate", "Labor force participation rate", 
                   "FITAP, Parish","SNAP, Parish", "KCSP, Parish", 
                   "SNAP, zip"),
    overlayGroups = c("Community colleges", 
                      "Department of Corrections facilities", 
                      "Veterans Affairs Facilites"
    ),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  showGroup("Parishes, plain")

我尝试过使用

"hidegroup"
作为图例组,但我没有运气。 到目前为止,使用
htmlwidgets
,我已经让所有图例消失了,但我无法让激活层的图例变得可见。

javascript html r r-leaflet htmlwidgets
1个回答
0
投票

我从CountriesSHP获取了我的shp数据,因为我没有完整的图片,我只是使用了一些毫无意义的样本数据。您需要为每个图例添加唯一的类名。然后使用

htmlwidgets::onRender
通过 jQuery 管理图例可见性。具体来说,我们可以使用事件侦听器来进行基础层更改。

library(leaflet)
library(htmlwidgets)
setwd(dirname(rstudioapi::getSourceEditorContext()$path)) # set the current script's location as working directory

# Sample data
parishes <- sf::st_read("ne_110m_admin_0_countries.shp")  # Replace with your data
unemployment <- c(5.2, 6.1, 4.8)  # Sample values
labor_force <- c(65.3, 62.1, 67.8)  # Sample values

leaflet() %>%
  addTiles() %>%
  # Base layers
  addPolygons(data = parishes, group = "Parishes, plain", 
              fillColor = "white", weight = 1) %>%
  addPolygons(data = parishes, group = "Unemployment rate",
              fillColor = ~colorNumeric("YlOrRd", unemployment)(unemployment),
              label = ~paste0(unemployment, "%")) %>%
  addPolygons(data = parishes, group = "Labor force participation rate",
              fillColor = ~colorNumeric("Blues", labor_force)(labor_force),
              label = ~paste0(labor_force, "%")) %>%
  
  # Add layer controls
  addLayersControl(
    baseGroups = c("Parishes, plain",
                   "Unemployment rate", "Labor force participation rate", 
                   "FITAP, Parish", "SNAP, Parish", "KCSP, Parish", 
                   "SNAP, zip"),
    overlayGroups = c("Community colleges", 
                      "Department of Corrections facilities", 
                      "Veterans Affairs Facilites"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  
  # Add legends with initial hidden state
  addLegend(position = "bottomright",
            pal = colorNumeric("YlOrRd", unemployment),
            values = unemployment,
            title = "Unemployment Rate",
            group = "legend_unemployment",
            className = "info legend unemployment") %>%
  addLegend(position = "bottomright",
            pal = colorNumeric("Blues", labor_force),
            values = labor_force,
            title = "Labor Force Participation",
            group = "legend_labor",
            className = "info legend labor") %>%
  # Add other legends similarly...
  
  # Hide all legends initially
  htmlwidgets::onRender("
    function(el, x) {
      var map = this;
      
      // Hide all legends initially
      $('.legend').hide();
      
      // Show/hide legends based on active base layer
      map.on('baselayerchange', function(e) {
        $('.legend').hide();
        if (e.name === 'Unemployment rate') {
          $('.legend.unemployment').show();
        } else if (e.name === 'Labor force participation rate') {
          $('.legend.labor').show();
        }
        // Add more conditions for other layers
      });
    }
  ")
© www.soinside.com 2019 - 2024. All rights reserved.