原问题分成两部分--&应法国人的建议

1)如何在clipPath中显示所有touch 状态的2位缩写,而不被路由线隐藏?

  1. 如果坐标点(.append("circle"))直接位于MapBBox边界,我如何显示一个完整的圆?目前,只显示一个圆圈(实际上是正确的)-参见完整示例,按钮05_1979
  // --- clear svg -----------------------------------------
  d3.select("svg").remove();
  
  // --- get modal size --------------------------------------------------------
  document.querySelector(".modal").addEventListener("shown.bs.modal", function (e) {
    let modalWidth = document.querySelector(".modal-dialog").clientWidth;
    let modalHeight = document.querySelector(".modal-dialog").clientHeight;
  
    // --- load both json files, V7 method -----------------
    Promise.all([
      d3.json("https://gist.githubusercontent.com/manu-75/06b031548def239d7038e7157a3c204c/raw/8da47d28b846b6b232cad57692d737e05ddebee3/USA_CAN.geojson"),
      d3.json(`https://gist.githubusercontent.com/manu-75/06b031548def239d7038e7157a3c204c/raw/8da47d28b846b6b232cad57692d737e05ddebee3/${currentTopo}.topojson`),
      ]).then(function (data) {
      let baseMap = data[0];
      let tourMap = data[1];
      let geojsonTour = topojson.feature(tourMap, tourMap.objects[currentTopo]);
      
    // --- get bbox of tourMap -----------------------------
    let tourMapBbox = d3.geoBounds(geojsonTour);
    
    // --- get aspectRatio from topojson -------------------
    let widthBbox = tourMapBbox[1][0] - tourMapBbox[0][0];
    let heightBbox = tourMapBbox[1][1] - tourMapBbox[0][1];
    let aspectRatio = widthBbox / heightBbox;
    let tourHeightCalc = Math.round(modalWidth / aspectRatio);
    
    // --- margins, width, height --------------------------
    let margin = { top: 20, right: 20, bottom: 20, left: 20 },
      width = modalWidth - margin.left - margin.right,
      height = tourHeightCalc - margin.top - margin.bottom;
    
    // --- svg -------------------------------------------
    let svg = d3
      .select("#tourMap")
      .append("svg")
      .attr("width", width)
      .attr("height", height);

    // --- projection ------------------------------------
    let projection = d3.geoMercator().fitSize([width, height], geojsonTour);

    // --- path ------------------------------------------
    let path = d3.geoPath().projection(projection);

    // --- svg append tour -------------------------------
    svg
      .append("path")
      .datum(geojsonTour)
      .attr("d", path)
      .attr("fill", "none")
      .attr("stroke", "#ff674d")
      .attr("stroke-width", 1.5);

    // --- svg append origin circle ----------------------
    svg
      .append("circle")
      .attr("cx", projection(destArray)[0])
      .attr("cy", projection(destArray)[1])
      .attr("r", 4)
      .attr("fill", "#338bff");

    // --- clipPath defined by tourMapBbox ----------------
    const clipPath = svg
      .append("clipPath")
      .attr("id", "clip")
      .append("rect")
      .attr("x", projection(tourMapBbox[0])[0])
      .attr("y", projection(tourMapBbox[1])[1])
      .attr("width", projection(tourMapBbox[1])[0] - projection(tourMapBbox[0])[0])
      .attr("height", projection(tourMapBbox[0])[1] - projection(tourMapBbox[1])[1]);

    // --- append clipPath -------------------------------
    const g = svg.append("g").attr("clip-path", "url(#clip)");
    
    // --- abbr name for states within clipPath ????
    // --- use "properties":{"name":"Washington","id":"US-WA" ....
    // --- last 2 characters from the id, in this example 'WA'

    // --- svg append baseMap ----------------------------
    svg
      .append("path")
      .datum(baseMap)
      .attr("d", path)
      .attr("fill", "transparent")
      .attr("stroke", "black")
      .attr("stroke-opacity", 0.5)
      .attr("stroke-width", 0.3);

    });
  }, { once: true });

here's my full jsfiddle example

如有需要,敬请惠顾!

推荐答案

要获得一个完整的圆,您必须判断该圆是否在SVG维度之外:

对于第一个按钮,圆在SVG之外,因此您可以这样调整高度:

// --- svg append origin circle ----------------------
svg
  .append("circle")
  .attr("cx", projection(destArray)[0])
  .attr("cy", projection(destArray)[1])
  .attr("r", 4)
  .attr("fill", "#338bff");
        
  const r = parseInt(svg.select("circle").node().getAttribute("r"));
  const cy = parseInt(projection(destArray)[1]) + 1; 
  const tcy = r + cy;
  if (tcy > height)
    svg.node().setAttribute("height",tcy);

如果需要,您可以对宽度执行相同的操作.

你可以勾选fiddle

Javascript相关问答推荐

为什么JavaScript双边字符串文字插值不是二次的?

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

我开始使用/url?q=使用Cheerio

useNavigation更改URL,但不呈现或显示组件

将状态向下传递给映射的子元素

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

Snowflake JavaScript存储过程返回成功,尽管预期失败

Angular 中的类型错误上不存在获取属性

使用Google API无法进行Web抓取

JS:XML insertBefore插入元素

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

MarkLogic-earch.suggest不返回任何值

更新Redux存储中的对象数组

使用Reaction窗体挂钩注册日历组件

有没有办法通过使用不同数组中的值进行排序

为什么NULL不能在构造函数的.Prototype中工作

为什么我的SoupRequest";被重置为初始值,以及如何修复它?

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

用内嵌的含selenium的Java脚本抓取网站

单击时同时 Select 和展开可访问的行