【问题标题】:Adding tooltip in responsive d3.js chart在响应式 d3.js 图表中添加工具提示
【发布时间】:2014-05-21 13:46:03
【问题描述】:

我正在学习使用 javascript 和 d3.js。我用工具提示做了一个简单的折线图,效果很好(见here)。 我决定让它响应,所以使用模板制作一个新的(见here)。 响应能力还可以,但我无法添加工具提示 div。 有什么帮助吗?

这是有问题的代码:

    chart2.selectAll("dot")    
    .data(data)         
    .enter().append("rect")             
    .attr("width", 1)
    .attr("height", 120)      
    .style("opacity", 0)      // set the element opacity
    .style("stroke", "#6f6f6f")    // set the line colour
    .attr("xScale", function(d) { return xScale(d.date); })       
    .attr("yScale", function(d) { return yScale(d.close)-60; })   
    .on("mouseover", function(d)
     { 
     d3.select(this).attr("width", 1).style("opacity", .8)   ; //il punto cambia al mousover (bellissmo)

         div.transition()        
            .duration(70)      
            .style("opacity", .8)
            .style("border", "1px");      
        div .html(formatTime(d.date) + "<br/>"  + d.close)  
            .style("left", (d3.event.pageX) + "px")     
            .style("top", (d3.event.pageY - 64) + "px");    
        })               
    .on("mouseout", function(d) {       

       d3.select(this).attr("r", 5.1).style("opacity", .0);   
        div.transition()        
        .duration(200)      
        .style("opacity", 0);   
    });

div 在这里定义:

 var div = d3.select("#chart2").append("div")   
    .attr("class", "tooltip")               
    .style("opacity", 0); 

在css中是:

 div.tooltip {   
  position: absolute;           
  text-align: center;           
  width: 95px;                  
  height: 40px;                 
  padding: 2px;             
  font: 15px arial;
  font-weight: bold;
  color: black;        
 background: #f0f0f0;
  pointer-events: none;         
}

【问题讨论】:

  • 什么是div?它是在哪里定义的?
  • 在这里定义 var div = d3.select("#chart2").append("div") .attr("class", "tooltip") .style("opacity", 0) ; (我也编辑了这个问题,我澄清一下)
  • 您正在将 div 元素附加到 SVG。这行不通——SVG 中没有div 元素。将其附加到 HTML 或使用 text 元素或类似的东西。

标签: javascript d3.js responsive-design data-visualization


【解决方案1】:

将 div 定义如下,然后选择“body”并在此处附加 div

var div = d3.select('body').append("div")
      .attr("id", "chart")
      .attr("class", "tooltip")
      .style("opacity", 0);

【讨论】:

    猜你喜欢
    • 2013-12-02
    • 1970-01-01
    • 2014-05-14
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2016-02-01
    • 1970-01-01
    相关资源
    最近更新 更多