如何在新行上分别显示来自一组对象的链接?

问题描述

在我的 JavaScript 对象数组中,我在为每个国家/地区对应的对象分别显示多个链接时遇到问题。

当前循环获取与国家对应的所有链接显示国家名称

链接的问题是,如果一个国家有多个链接,我无法单独显示它们。

例如,这三个 DE 链接在工具提示显示一个链接https%3A%2F%2Fwwww.example1.com%2Chttps%3A%2F%2Fwwww.example2.com%2Chttps%3A%2F%2Fwwww.example3.com 并且我希望每个链接都在单独的行上。

如何更好地编写循环以实现我想要的?

当前循环如下所示:

//Loop for displaying links corresponding to each country
        group.data.forEach(function(link){
            let polygonTemplate = series.mappolygons.template;
        // Instead of our custom country,we Could also use {name} which comes from geodata  
          polygonTemplate.tooltipHTML = '<b>{country}</b><br><a href="{link.urlEncode()}">More info</a>';
          polygonTemplate.fill = am4core.color("blue");
        });

您还可以查看 JSfiddle 代码段以更清楚地了解我的意思。

任何帮助将不胜感激。

谢谢!

am4core.ready(function() {

    // Themes begin
    am4core.useTheme(am4themes_animated);
    // Themes end
    
    // Create map instance
    let chart = am4core.create("map",am4maps.MapChart);
    
    // Set map deFinition
    chart.geodata = am4geodata_worldHigh;
    
    // Set projection
    chart.projection = new am4maps.projections.Mercator();
    
    // Zoom control
    chart.zoomControl = new am4maps.ZoomControl();
    
    let homeButton = new am4core.Button();
    homeButton.events.on("hit",function() {
      chart.goHome();
    });
    
    homeButton.icon = new am4core.Sprite();
    homeButton.padding(7,5,7,5);
    homeButton.width = 30;
    homeButton.icon.path = "M16,8 L14,16 L10,10 L6,16 L2,8 L0,8 L8,0 L16,8 Z M16,8";
    homeButton.marginBottom = 10;
    homeButton.parent = chart.zoomControl;
    homeButton.insertBefore(chart.zoomControl.plusButton);
    
    // Center on the groups by default
    chart.homeZoomLevel = 5;
    chart.homeGeoPoint = { longitude: 10,latitude: 52 };
    
    let groupData = [
      {
        "color": chart.colors.getIndex(0),"data": [
          {
            "country": "Germany","id": "DE",// With MappolygonSeries.useGeodata = true,it will try and match this id,then apply the other properties as custom data
            "link": ["https://wwww.example1.com","https://wwww.example2.com","https://wwww.example3.com"],},{
            "country": "France","id": "FR","link": ["https://wwww.example4.com"],{
            "country": "Belgium","id": "BE","link": ["https://wwww.example5.com","https://wwww.example6.com"]
            
          },{
            "country": "Netherlands","id": "NL","link": ["https://wwww.example7.com"]
          }
        ]
      }
    ];
    
    // This array will be populated with country IDs to exclude from the world series
    let excludedCountries = ["AQ"];
    
    // Create a series for each group,and populate the above array
    groupData.forEach(function(group) {
      let series = chart.series.push(new am4maps.MappolygonSeries());
      series.name = group.name;
      series.useGeodata = true;
      let includedCountries = [];
      // Make a loop to display a link for the group of countries
      group.data.forEach(function(country) {
        includedCountries.push(country.id);
        excludedCountries.push(country.id);
        
                //Loop for displaying links corresponding to each country
    group.data.forEach(function(link){
        let polygonTemplate = series.mappolygons.template;
    // Instead of our custom country,we Could also use {name} which comes from geodata  
      //polygonTemplate.tooltipHTML = '<b>{country}</b><br><a href="{link.urlEncode()}">More info</a>';
      polygonTemplate.tooltipHTML = '<b>{country}</b>' + link.link.map(url =>
  '<br><a href="{url.urlEncode()}">More info</a>').join("");
      polygonTemplate.fill = am4core.color("blue");
    });
        
      });
      series.include = includedCountries;
    
      series.fill = am4core.color(group.color);
      series.setStateOnChildren = true;
      series.calculateVisualCenter = true;
      series.tooltip.label.interactionsEnabled = true;
            series.tooltip.keepTargetHover = true;
    

      // Country shape properties & behaviors
      let mappolygonTemplate = series.mappolygons.template;
      mappolygonTemplate.fill = am4core.color(group.color);
      mappolygonTemplate.fillOpacity = 0.8;
      mappolygonTemplate.nonScalingstroke = true;
      mappolygonTemplate.tooltipPosition = "fixed";
            
    
      mappolygonTemplate.events.on("over",function(event) {
        series.mappolygons.each(function(mappolygon) {
          mappolygon.isHover = false;
        })
        event.target.isHover = false;
        event.target.isHover = true;
      })
    
      mappolygonTemplate.events.on("out",function(event) {
        series.mappolygons.each(function(mappolygon) {
          mappolygon.isHover = false;
        })
        
      })
    
      // States  
      let hoverState = mappolygonTemplate.states.create("hover");
      hoverState.properties.fill = am4core.color("#9985e3");
    
      series.data = JSON.parse(JSON.stringify(group.data));
      
    });
    
    // The rest of the world.
    let worldSeries = chart.series.push(new am4maps.MappolygonSeries());
    let worldSeriesName = "world";
    worldSeries.name = worldSeriesName;
    worldSeries.useGeodata = true;
    worldSeries.exclude = excludedCountries;
    worldSeries.fillOpacity = 0.5;
    worldSeries.hiddenInLegend = true;
    worldSeries.mappolygons.template.nonScalingstroke = true;


});
body {
    font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
  }
  #map {
    width: 100%;
    height: 600px;
  }
  a {
    cursor: pointer;
    color: rgb(4,46);
  }
<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Some countries</title>
    <link rel="stylesheet" href="css/site.css">
</head>
<!-- Scripts for loading AmCharts Map -->
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/maps.js"></script>
<script src="https://cdn.amcharts.com/lib/4/geodata/worldHigh.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<script src="js/custom.js"></script>
<body>
    <div id="map"></div>
</body>
</html>

解决方法

使用 map() 迭代 link 属性并使每个 URL 在 tooltipHTML 属性中单独一行。

polygonTemplate.tooltipHTML = '<b>{country.country}</b>' + country.link.map(url =>
  '<br><a href="{url.urlEncode()}">More info</a>').join("");
,
mapPolygonTemplate.events.on("over",function(event) {
        series.mapPolygons.each(function(mapPolygon) {
          mapPolygon.isHover = false;
        })
        event.target.isHover = false;
      group.data.forEach(function(country) {
          event.target.tooltipHTML = '<b>{country}</b>' + country.link.map(url =>
  '<br><a href="{link.urlEncode()}">More info</a>').join("");
          polygonTemplate.fill = am4core.color("blue");
});
        event.target.isHover = true;
      });

这就是它的解决方案的样子。我没有在小提琴上试过这个,但我很确定流程。看看是否可以在调用 setToolTip() 时设置 toolTipHtml。

还有一件事,如果您可以将悬停的多边形与您的 JSON 数据相关联,那么您不需要遍历“group.data”,而是从数组中获取对象并附加链接进入 toolTipHtml。