使用鼠标位置更改D3地图的工具提示的类

拉斐达西尔瓦(Raphadasilva)

我最近使用D3js和插件d3.tip.js制作了一个交互式地图,以编写一个不错的工具提示。我为工具提示的样式写了以下这些行:

.d3-tip {  

  line-height: 1.5;
  font-weight: normal;
  font-family: Arial;
  padding: 5px;
  background: rgba(125, 125, 125, 0.9);
  color: white;
  border-radius: 4px;
  font-size: 11px!important;

  margin-left: 25px;
  margin-top: 205px;
}

margin-left和margin-top属性使工具提示显示在鼠标光标下方,如下所示:工具提示

工具提示在我的大部分地区都可以正常使用,但是iframe的右上角存在问题,如下所示:

工具提示问题

因此,我想知道是否可以通过测试iframe中鼠标的x位置来应用具有不同的margin-left值的其他类。

如果x位置在我的限制之后,则工具提示会在光标的左侧更多,否则会在右侧更多。

Do you think these is it possible ? I searched without finding anything which I could adapt. Here's a link to fork all the code in gitHub.

Thanks in advance !

AmeliaBR

You can modify the classes on the tip element in your event handler function. Instead of passing tip.show directly to the mouseover event, you can create your own event handler that calculates whether the tip needs to be re-positioned, sets the classes accordingly, and also calls tip.show() with the correct parameters.

Things to be aware of:

  • d3.tip doesn't implement the classed() method to toggle individual classes, so you have to reset the entire class attribute.

  • tip.show() resets the default classes itself, so you need to set your custom classes after you show the tip.

  • 您需要将d,i变量传递tip.show()html更新功能中要使用变量

下面是一个简化的示例。提示默认显示在矩形的顶部边缘,我检查是否将提示推离页面的顶部边缘,并设置一个类来翻译提示(如果这样)。

/* Initialize tooltip */
tip = d3.tip()
  .html(function(d, i) { return "Rectangle #" + i; });

/* Invoke the tip in the context of your visualization */
vis = d3.select("svg")
vis.call(tip)

vis.selectAll('rect')
  .datum(function(d){ 
    //save the old data (null in this example)
    //as a sub-property of a new data object
    //which also contains position information
    return {data:d, position:(this.x, this.y)}; 
  })
  .on('mouseover', function(d, i){
    //save the moused-over graphical element in a variable
    var rect = this;
  
    tip.show(d, i); //show the tip (will call the .html function)
  
    tip.attr('class', function(){
      //use the properties of the moused-over element
      //to modify the classes on the tip object:
      if (rect.getBBox().y < 50)
        return 'd3-tip n down'
      else
        return 'd3-tip n';
    })
  })
  .on('mouseout', tip.hide)
rect {
  fill: yellow;
  stroke: blue;
}

.d3-tip {
  background: white;
  border: solid gray;
  border-radius: 0.5em;
  padding: 0.25em;
}
.d3-tip.down {
  -webkit-transform: translate(0, 2em);
  transform: translate(0, 2em);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.6.3/d3-tip.min.js"></script>

<svg width="400px" height="400px">
  <rect x="0" y="0" width="100" height="100" />
  <rect x="100" y="0" width="100" height="100" />
  <rect x="0" y="100" width="100" height="100" />
  <rect x="100" y="100" width="100" height="100" />
</svg>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用鼠标位置更改D3地图工具提示的类

来自分类Dev

D3弧形图:圆形鼠标悬停上的工具提示不显示

来自分类Dev

d3地图鼠标悬停事件响应缓慢

来自分类Dev

托管在共享点上时,D3 工具提示位置与鼠标的偏移

来自分类Dev

轻柔地放大D3地图的中心

来自分类Dev

鼠标悬停在d3上的工具提示

来自分类Dev

鼠标悬停在d3上的工具提示

来自分类Dev

D3地图,“ d”属性

来自分类Dev

D3地图SVG性能

来自分类Dev

D3工具提示修复位置

来自分类Dev

D3工具提示修复位置

来自分类Dev

较小屏幕尺寸下的 D3 位置工具提示

来自分类Dev

使用javascript折线图中的d3工具提示位置问题

来自分类Dev

d3 响应式分组条形图,带有使用 json 文件的工具提示

来自分类Dev

D3地图投影不显示地图

来自分类Dev

将D3地图放在Google地图上

来自分类Dev

如何在D3中更改饼图的位置?

来自分类Dev

如何在D3中更改饼图的位置?

来自分类Dev

D3缩放和鼠标悬停工具提示

来自分类Dev

D3工具提示或条形图上的鼠标悬停

来自分类Dev

d3/topojson choropleth 地图中的工具提示不起作用

来自分类Dev

使用D3在鼠标位置绘制圆onClick

来自分类Dev

D3 X轴上带有工具提示的条形图

来自分类Dev

带有工具提示中链接的d3饼图

来自分类Dev

如何将悬停(工具提示)添加到D3。气泡运动图?

来自分类Dev

d3 v4 获取条形图工具提示的数据

来自分类Dev

图表中的D3工具提示

来自分类Dev

设置d3的工具提示样式

来自分类Dev

D3 Heatmap画布的工具提示