2017-10-11 71 views
0

我有以下的HTMLD3更新工具提示HTML

   <div id="tooltip" className="hidden"> 
        <table> 
         <thead> 
         <tr> 
          <td colSpan="3"> 
           <strong id="headDate"></strong> 
          </td> 
         </tr> 
         </thead> 
         <tbody id="toolTipBody"> 
         </tbody> 
        </table> 
       </div> 

下方的D3代码是工作的罚款:

   // ToolTip 
       d3.select('#tooltip') 
        .classed("hidden", false) 
        .style("left", d3.event.pageX + "px") 
        .style("top", d3.event.pageY + "px") 
        .select('#headDate').text(d0.date); 

我需要注入/从D3追加HTML刀尖进入#toolTipBody元素。它需要是动态的行数和行数可以改变(否则我可以硬编码的HTML和只使用D3。选择&。文本进行更新。

如何添加说以下内容#toolTipBody

     <tr> 
          <td className="legend-color-guide"><div></div></td> 
          <td id="key">1.0E-6MHz</td> 
          <td id="value">46.50</td> 
         </tr> 

感谢 亚当

enter image description here

+0

什么是触发提示的人口的动作(和你有可视化的模型或屏幕截图)? –

+0

也,我看到你正在使用'className',所以我假设你正在使用React? –

+0

是的使用反应...我会添加一个截图...将添加所有的代码,但大多数是不相关的。 – Adam

回答

1

您可以动态地更改HTML部分,如下图所示。

d3.select('#tooltip').html(newHTML); 

参考:https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#html

演示 - (将鼠标悬停在圈子)

var testData = [{ 
 
    data: [{ 
 
    key: "1.0E-6MHz", 
 
    value: "46.50" 
 
    }, { 
 
    key: "2.0E-6MHz", 
 
    value: "50.50" 
 
    }], 
 
    date: "Apr 10", 
 
    color: "red" 
 
}, { 
 
    data: [{ 
 
    key: "1.0E-6MHz", 
 
    value: "46.50" 
 
    }, { 
 
    key: "2.0E-6MHz", 
 
    value: "50.50" 
 
    }, { 
 
    key: "1.8E-6MHz", 
 
    value: "10.50" 
 
    }], 
 
    date: "Feb 19", 
 
    color: "green" 
 
}]; 
 

 
d3.select("svg") 
 
    .selectAll(".node") 
 
    .data(testData) 
 
    .enter() 
 
    .append("circle") 
 
    .attr("r", 5) 
 
    .attr("cx", function(d, i) { 
 
    return (i + 1) * 100 
 
    }).attr("cy", 50) 
 
    .on("mouseover", function(d) { 
 
    d3.select('#tooltip') 
 
     .classed("hidden", false) 
 
     .style("left", d3.event.pageX + "px") 
 
     .style("top", d3.event.pageY + "px") 
 
     .select('#headDate').text(d.date); 
 

 
    var newHtml = []; 
 
    d.data.forEach(function(h) { 
 
     newHtml.push('<tr>', 
 
     '<td id="key">' + h.key + '</td>', 
 
     '<td id="value">' + h.value + '</td>', 
 
     '</tr>'); 
 
    }); 
 

 
    d3.select('#tooltip') 
 
     .select("#toolTipBody").html(newHtml.join("")) 
 
    }) 
 
    .on("mouseout", function(d) { 
 
    d3.select('#tooltip') 
 
     .classed("hidden", true); 
 
    });
#tooltip { 
 
    position: absolute; 
 
    background-color: #cecece; 
 
} 
 

 
.hidden { 
 
    display: none; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<div id="tooltip" class="hidden"> 
 
    <table> 
 
    <thead> 
 
     <tr> 
 
     <td colSpan="3"> 
 
      <strong id="headDate"></strong> 
 
     </td> 
 
     </tr> 
 
    </thead> 
 
    <tbody id="toolTipBody"> 
 
    </tbody> 
 
    </table> 
 
</div> 
 
<svg width=400 height=200> 
 
</svg>