【发布时间】:2018-08-08 23:42:46
【问题描述】:
我有一个折线图,正在尝试将 html 添加到图例上的鼠标悬停/工具提示中。我想循环遍历数组 (full_names),以便在第一个图例行的工具提示中显示“full_names[0]”,第二个显示“full_names[1]”等。目前,它们都显示“full_names[0] ]'。为什么我的 full_names 不能正确循环?
id_list 也是一个数组,我循环遍历以便顺序分配 id。
我的传奇:
var lineLegend = svg.selectAll(".lineLegend").data(id_list)
.enter().append("g")
.attr("class", "lineLegend")
.attr("id", function (d, i) { return id_list[i % id_list.length]
})
.attr("transform", function (d, i) {
return "translate(" + width + "," + (i * 20) + ")";
})
.on("click", function (id) {
var this_chart = d3.select("#temperature_graph")
var liney = this_chart.select("#" + id)
var alldots = this_chart.selectAll("." + id)
var isActive = liney.classed("active");
var dotsActive = alldots.classed("active")
console.log(liney)
liney.classed("active", !isActive);
alldots.classed("active", !dotsActive)
})
.on("mouseover", function (i) {
div.transition()
.duration(100)
.style("opacity", .9);
我想在这里循环遍历数组(full_names):
div.html( function (d, i) { return full_names[i % full_names.length]})
.style('color', '#404040')
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
剩下的:
.on("mouseout", function (d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
lineLegend.append("text").text(function (d) {
return d;}).attr("transform",
"translate(-94,15)").style("font-family", "Sans-
Serif").style('fill', '#5a5a5a'); //align texts with boxes
lineLegend.append("rect")
.attr("fill", function (d, i) { return colors[i %
colors.length] })
.attr("width", 12).attr("height", 10).attr("transform",
"translate(-32,4)").attr("rx", "3");
我认为我的数组可能存在范围问题?如,我可以正确地循环遍历 id_list,而不是 full_names。这两个变量都是在同一个地方创建的。那是因为 id_list 包含在我的 var linelegend 中吗?
非常感谢!!
【问题讨论】:
-
你在哪里定义数组
full_names? -
就在我开始此代码(var linelegend...)的同一级别的上方。
-
你为什么要
full_names[i % full_names.length]?full_names[i]有什么问题 -
它们都将显示全名[0]。由于某种原因 [i] 没有增加
标签: javascript html arrays d3.js