gpt4 book ai didi

javascript - 如何将 D3 工具提示放入 div 中?

转载 作者:行者123 更新时间:2023-11-30 15:26:06 25 4
gpt4 key购买 nike

我正在尝试控制 D3 工具提示的位置,使其位于特定的 div 内;因此可以使用 position: absolute 适当放置。

但是,似乎无论我如何布局 svg 元素的代码,工具提示的 div 类都落在父 div 之外(之后)。因此绝对定位变成相对于页面,这是我不想要的。

我尝试了多种将 div 标签放置在不同位置的方法。我还尝试通过添加 d3.select("outline").tip(); 将工具提示放在父 div 中,但这会引发错误。

这是我目前使用的完整代码。我试图将上部工具提示放在 div“#outline”的右上角。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
font: 10px sans-serif;
}

#outline {
width: 1000px;
position: relative;
border-color: #000000;
border-style: solid;
border-width: 1px;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.bar {
fill: orange;
}

.bar:hover {
fill: orangered ;
}

.x.axis path {
display: none;
}

.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}

/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}

/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}

.d3-tip2 {
position: absolute;
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}

/* Style northward tooltips differently */
.d3-tip2.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body><br><br><br><br><br><br><div id="outline">
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="d3.tip.v0.6.3.js"></script>
<script>

var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var data =
[
{letter:"A", frequency2:.08167},
{letter:"B", frequency2:.01492},
{letter:"C", frequency2:.02780},
{letter:"D", frequency2:.04253},
{letter:"E", frequency2:.12702},
{letter:"F", frequency2:.02288},
{letter:"G", frequency2:.02022},
{letter:"H", frequency2:.06094},
{letter:"I", frequency2:.06973},
{letter:"J", frequency2:.00153},
{letter:"K", frequency2:.00747},
{letter:"L", frequency2:.04025},
{letter:"M", frequency2:.02517},
{letter:"N", frequency2:.06749}
];

var formatPercent = d3.format(".0%");

var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);

var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency2:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
})

var tip2 = d3.tip()
.attr('class', 'd3-tip2')
.html(function(d) {
return "<strong>Other Variables:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
})

var svg = d3.select("#outline").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.call(tip);
svg.call(tip2);

data.forEach(d=>{
d.frequency = +d.frequency;
});

x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency2; })]);

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency 2");

svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency2); })
.attr("height", function(d) { return height - y(d.frequency2); })
.on("mouseover", function(d) {
tip.show(d);
tip2.show(d)
.attr("position", "absolute")
.style("top", "16px")
.style("left","860px");
}).on("mouseout", function(d) {
tip.hide();
tip2.hide();
});

function type(d) {
d.frequency2 = +d.frequency2;
return d;
}

</script>
</div>
</body>

编辑: 我需要通过将工具提示放在 div 中来完成此操作,以便它相对于轮廓移动。我不能使用相对于页面的绝对定位,因为这个 SVG block 将放置在不同页面的不同位置。

最佳答案

默认情况下,d3.tip() div 附加到 document.body

应该一个d3.tip()解决方案,即tip.rootElement :

You can also specify the root element, which is document.body by default.

但是,tip.rootElement 不工作,它给出了 tip.rootElement is not a function 错误(这里完全公开:我不知道为什么,我不知道没关系,我从未使用过 d3.tip(),因为我在我的元素中制作了自己的工具提示)。

解决方案:使用createDocumentFragment,我们可以将工具提示移动到所需的 div(查看源和目标的 ID):

var fragment = document.createDocumentFragment();
fragment.appendChild(document.getElementById('sourceID'));
document.getElementById('destinationID').appendChild(fragment);

这是您的工作代码(我在顶部放了一个Lorem ipsum):http://bl.ocks.org/anonymous/raw/d127ae48bbd8eb1c8050c0e3b417c251/

关于javascript - 如何将 D3 工具提示放入 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42942078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com