javascript - 如何在 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>
并且touchstart
ortouchend
动作应该可以正常工作。请参阅https://developer.mozilla.org/en-US/docs/Web/SVG/Element/tspan
推荐阅读
- mysql - 将 varchar 用于具有多个值的单个子类别会更好吗?
- javascript - 功能是非破坏性的,但它正在修改我的变量
- python - 用于图像数组的 Django 序列化器会减慢速度
- django - Django:如何在管理页面中根据类别排列产品
- python - 如何让python命令调用python3.7?
- dart - dart 中的 json_serializable 枚举值
- vue.js - 将字符串推送到 dynamodb 表中的数组属性中而不重复
- java - JTextpane 不在文件中保存属性文本和图标
- powershell - 如何在线运行我的 powershell 脚本?
- java - 使用透明度时渲染到纹理时渲染覆盖透明度