简体   繁体   English

D3.js雷达图工具提示

[英]D3.js radar chart tooltip

Based on this example of a D3.js radar chart, I'm trying to have a more advanced type of tooltips. 基于 D3.js雷达图示例,我正在尝试使用更高级的工具提示类型。 Instead of just a number, I want to show a list of names and numbers, some of them based on my dataset. 我不仅要显示数字,还想显示一个名称和数字的列表,其中一些是基于我的数据集的。

The problem is in this part of the code: 问题出在代码的这一部分:

blobCircleWrapper.selectAll(".radarInvisibleCircle")
                        .data(function(d,i) { return d; })
                        .enter().append("circle")
                        .attr("class", "radarInvisibleCircle")
                        .attr("r", cfg.dotRadius*1.5)
                        .attr("cx", function(d,i){ return rScale(d.value) * Math.cos(angleSlice*i - Math.PI/2); })
                        .attr("cy", function(d,i){ return rScale(d.value) * Math.sin(angleSlice*i - Math.PI/2); })
                        .style("fill", "none")
                        .style("pointer-events", "all")
                        .on("click", function(d,i) {   // TOOLTIP WITH SUBJECTS AND AVERAGES HERE
                            newX =  parseFloat(d3.select(this).attr('cx')) - 10;
                            newY =  parseFloat(d3.select(this).attr('cy')) - 10;

                            tooltip
                                .attr('x', newX)
                                .attr('y', newY)
                                .html(d.value+ "<br>" + "To:")
                                .transition().duration(200)
                                .style('opacity', 1);
                        })
                        .on("mouseout", function(){
                            tooltip.transition().duration(200)
                                .style("opacity", 0);
                        });


                    //Set up the small tooltip for when you hover over a circle
                    var tooltip = g.append("text")
                        .attr("class", "tooltip")
                        .style("opacity", 0);

The tooltip is set for the circles of the radar, and when I try to create a div element instead of the text element, the tooltip stops showing, although the element is created and well positioned. 工具提示是为雷达的圆周设置的,当我尝试创建div元素而不是text元素时,尽管该元素已创建且位置正确,但工具提示会停止显示。 I was trying something like this: 我正在尝试这样的事情:

var tooltip = g.append("div")
                        .attr("class", "tooltip")
                        .style("opacity", 0);

I'm sure I'm missing some attributes here, but is there a way to have more complete tooltips? 我确定我在这里缺少一些属性,但是有没有办法提供更完整的工具提示? Thank you. 谢谢。

You're using a <div> as tooltip. 您正在使用<div>作为工具提示。 Thus, you have some different rules here. 因此,您在这里有一些不同的规则。

First, you cannot append a div to a SVG (I suppose that the g selection in your snippet is a SVG group). 首先,您不能将div附加到SVG(我想您的代码段中的g选择是SVG组)。 You'll have to append the div to the body (or any other HTML element). 您必须将div附加到正文(或任何其他HTML元素)。

Second, as you're appending the div to the HTML, you cannot set the x and y positions as attributes. 其次,在将div附加到HTML时,不能将xy位置设置为属性。 Instead of that, you'll have to use event.pageX and event.pageY , with an absolute position for the div. 取而代之的是,您必须使用event.pageXevent.pageY ,并在div中使用absolute位置。

Here is a very simple demo with the basics os what I just said: 这是一个非常简单的演示,其中包含我刚才所说的基础知识:

 var tip = d3.select("body") .append("div") .attr("class", "tip") .style("opacity", 0); d3.select("svg") .selectAll("foo") .data(d3.range(7)) .enter() .append("circle") .attr("cx", d => 20 + d * 40) .attr("cy", 50) .attr("r", 15) .attr("fill", "teal") .on("mousemove", (d, i) => { tip.html("This is the<br>circle number " + i) .style("left", d3.event.pageX + 10 + "px") .style("top", d3.event.pageY + "px") .style("opacity", 1) }).on("mouseout", () => { tip.style("opacity", 0) }) 
 .tip{ position: absolute; padding: 10px; background: lightgray; border: 1px solid black; pointer-events: none; } 
 <script src="https://d3js.org/d3.v4.min.js"></script> <svg></svg> 

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

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