繁体   English   中英

如何在 D3.js 中显示触摸事件的文本?

[英]How do I display text for touch events in D3.js?

我有一张地图,上面有一个区域,当鼠标悬停在它上面时它会显示文本(使用 touchstart 和 touchend 事件)。 但是,在我的 iPad 上,尽管触摸时区域会改变颜色,但文本并未显示。 有谁知道如何让它显示文本?

 function create_canada(canada) {
 var provinces = svg.append("g")
    .attr("class", "provinces")
 .selectAll("path")
 .data(topojson.object(canada, canada.objects.provinces).geometries)
 .enter().append("path")
 .attr("class", "province")
 .attr("d", path)
 .style("fill", function(d) {
   return "#"+Math.floor(Math.random()*16777215).toString(16);
  }) 
 .style("opacity","0.7")
 .on("touchstart", function(d) {
  colour = d3.select(this).style("fill");
  d3.select(this).style("fill", "red");
  }) // end mouseover
  .on("touchend", function(d) {
               d3.select(this).style("fill", function(d) {
                  return colour;
                }); // end mouseout
            }); // end var provinces
 provinces.append("svg:title") 
     .text(function(d) {
          return (d.id + " " + (get_Percapita(d)) + " per million"); // d.id is the Province name
          });

return provinces;

你应该像这样你的文本中添加一个tspan

<text><tspan>My text</tspan></text>

并且touchstarttouchend动作应该可以正常工作。 请参阅https://developer.mozilla.org/en-US/docs/Web/SVG/Element/tspan

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM