我正在尝试控制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块将被放置在不同页面上的不同位置。
答案 0 :(得分:1)
默认情况下,d3.tip()
div会附加到document.body
。
应该是 d3.tip()
解决方案,tip.rootElement:
您还可以指定根元素,默认情况下为document.body。
然而,tip.rootElement
无法正常工作,它会出现tip.rootElement is not a function
错误(此处完全披露:我不知道为什么而且我不在乎,我从未使用过{{1}因为我在我的项目中制作了自己的工具提示。)
解决方案:使用d3.tip()
,我们可以将工具提示移动到所需的div(查看源和目标的ID):
createDocumentFragment
这是你的工作代码(我把 Lorem ipsum 放在顶部):http://bl.ocks.org/anonymous/raw/d127ae48bbd8eb1c8050c0e3b417c251/
答案 1 :(得分:1)
我修改了你的代码,因为你可以看到工具提示在outlineParent
div内。
var margin = {
top: 40,
right: 20,
bottom: 30,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
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')
.attr('id', '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')
.attr('id', '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);
// Moving tooltips
document.getElementById('outlineParent').appendChild(
document.getElementById('d3-tip')
);
document.getElementById('outlineParent').appendChild(
document.getElementById('d3-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;
}
&#13;
body {
font: 10px sans-serif;
}
#outlineParent {
width: 1600px;
height: 1000px;
position: absolute;
border-color: #000000;
border-style: solid;
border-width: 1px;
background-color: #FFA500;
}
#outline {
width: 1000px;
position: absolute;
border-color: #000000;
border-style: solid;
border-width: 1px;
background-color: #FFF;
}
.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;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>
<div id="outlineParent">
<div id="outline">
</div>
</div>
&#13;