简体   繁体   中英

Adding tooltip in responsive d3.js chart

i'm learning to use javascript and d3.js. I made a simple linechart with tool tip that works perfectly (see here ). I decided to make it responsive, so using a template a make a new one (see here ). responsiveness is ok but i can't add the tooltip div. any help?

here the piece of code that it' problematic:

    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);   
    });

the div is definied here:

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

and in the css is:

 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;         
}

define div as following and select "body" and append div there

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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