【发布时间】:2017-03-07 16:01:31
【问题描述】:
我想为之前在这里讨论过的项目添加工具提示:
How to apply specific colors to D3.js map based on data values?
我为工具提示容器添加了如下样式:
<style>
#tooltip-container{
background: #eee;
box-shadow: 0 0 5px #999999;
color: #333;
display: none;
font-size: 12px;
padding: 10px;
position: absolute;
text-align: center;
right: 200px;
top: 300px;
width: 140px;
height: 230px;
z-index: 10;
}
</style>
我还在 div 中添加了 html 工具提示容器:
<div id="tooltip-container"></div>
我已将 drawMap() 函数修改如下:
function drawMap(conus) {
svg.selectAll(".feature")
.data(conus.features)
.enter().append("path")
.attr("class", "county")
.attr("id", function (d) { return d.properties.ID_1; }, true)
.attr("d", path)
.on("mouseover", function (d) {
$("#tooltip-container").show();
})
.on("mouseout", function () {
$("#tooltip-container").hide();
});
//Fill county colors
var dataRange = getDataRange();
d3.selectAll('.county')
.attr('fill', function (d) {
return getColor(d.properties[attributeArray[currentAttribute]], dataRange);
});
}
我现在需要做的事情如下:
当用户单击“停止”按钮时,工具提示容器应显示“warnings.csv”文件中当天的相应警告文本。可以从此处下载 csv 文件:https://nlet.brc.tamus.edu/Home/Swat),方法是选择 SWAT 下载部分的管理选项卡并选择“警告 csv 文件”。
然后,当用户将鼠标悬停在地图上时,工具提示容器应切换到该县并显示该县当天的相应警告。
感谢任何帮助。提前致谢。
【问题讨论】:
-
无法读取 csv 文件:.on("mouseover", function (d) { $("#tooltip-container").show(); var html = ""; html += "
"; d3.csv("/data/warnings.csv", function (d,i) { return { County: d.County, id: d.id, warn: d[ i] }; }); tooltip.html(html); })" + d.properties.County + "
" + "ID:" + d.properties.ID_1 + " -
能够读取 csv 文件,但不会显示。我按 id 对我的 csv 文件进行了排序: var mywarning = loadWarning(d.properties.ID_1, day);函数 loadWarning(id, day) { var warning = ""; d3.csv("/data/warnings2.csv", function (error, data) { for (var i in data) { if (data[i].id == id) { warning = data[i][day] ; 休息; } } });返回警告; }