Javascript 在鼠标上方高亮显示使用D3.js绘制的椭圆弧,并显示工具提示
如何在鼠标上方突出显示绘制的椭圆弧,并使用d3.js在其上显示工具提示?使用d3提示向d3条形图添加工具提示 Index.html:Javascript 在鼠标上方高亮显示使用D3.js绘制的椭圆弧,并显示工具提示,javascript,d3.js,tooltip,tipsy,Javascript,D3.js,Tooltip,Tipsy,如何在鼠标上方突出显示绘制的椭圆弧,并使用d3.js在其上显示工具提示?使用d3提示向d3条形图添加工具提示 Index.html: Using d3-tip to add tooltips to a d3 bar chart. <!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fil
Using d3-tip to add tooltips to a d3 bar chart.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.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;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/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 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>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
})
var svg = d3.select("body").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);
d3.tsv("data.tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
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");
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.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
});
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
使用d3提示向d3条形图添加工具提示。
身体{
字体:10px无衬线;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
形状渲染:边缘清晰;
}
.酒吧{
填充物:橙色;
}
.bar:悬停{
填充:橙色;
}
.x轴路径{
显示:无;
}
.d3提示{
线高:1;
字体大小:粗体;
填充:12px;
背景:rgba(0,0,0,0.8);
颜色:#fff;
边界半径:2px;
}
/*为工具提示创建一个小三角形延长线*/
.d3提示:之后{
框大小:边框框;
显示:内联;
字体大小:10px;
宽度:100%;
线高:1;
颜色:rgba(0,0,0,0.8);
内容:“\25BC”;
位置:绝对位置;
文本对齐:居中;
}
/*以不同方式设置向北工具提示的样式*/
第三点提示n:之后{
保证金:-1px0;
最高:100%;
左:0;
}
var margin={顶部:40,右侧:20,底部:30,左侧:40},
宽度=960-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
var formatPercent=d3.format(“.0%”);
var x=d3.scale.ordinal()
.rangeRoundBands([0,宽度],.1);
变量y=d3.scale.linear()
.范围([高度,0]);
var xAxis=d3.svg.axis()
.比例(x)
.东方(“底部”);
var yAxis=d3.svg.axis()
.比例(y)
.东方(“左”)
.格式(格式百分比);
var tip=d3.tip()
.attr('class','d3 tip')
.偏移量([-10,0])
.html(函数(d){
返回“频率:”+d.Frequency+”;
})
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
svg.call(tip);
d3.tsv(“data.tsv”),类型,功能(错误,数据){
x、 域(data.map(函数(d){返回d.letter;}));
y、 域([0,d3.max(数据,函数(d){返回d.frequency;})];
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis)
.append(“文本”)
.attr(“变换”、“旋转(-90)”)
.attr(“y”,6)
.attr(“dy”,“.71em”)
.style(“文本锚定”、“结束”)
.文本(“频率”);
svg.selectAll(“.bar”)
.数据(数据)
.enter().append(“rect”)
.attr(“类”、“条”)
.attr(“x”,函数(d){返回x(d字母);})
.attr(“宽度”,x.rangeBand())
.attr(“y”,函数(d){返回y(d.frequency);})
.attr(“高度”,函数(d){返回高度-y(d.frequency);})
.on('mouseover',tip.show)
.on('mouseout',tip.hide)
});
功能类型(d){
d、 频率=+d.频率;
返回d;
}
数据。tsv
字母频率
A.08167
B.01492
C.02780
D.04253
E.12702
F.02288
G.02022
H.06094
I.06973
J.00153
K.00747
L.04025
M.02517
第06749号
O.07507
第01929页
问题00098
R.05987
S.06333
T.09056
U.02758
V.01037
W.02465
X.00150
Y.01971
Z.00074
参考资料:
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<style>
body { font: 10px sans-serif; }
.d3-tip {
background: rgba(0, 0, 0, 0.8);
border-radius: 2px;
color: #fff;
font-weight: bold;
line-height: 1;
padding: 12px;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
var w = window.innerWidth,
h = window.innerHeight,
margin = { top: 40, right: 20, bottom: 20, left: 40 };
var svg = d3.select("body").append("svg").attr({
width: w,
height: h
});
var dataset = [
{ toolTip: "one", d: "M 50 200 a 100 50 0 1 1 250 50" },
{ toolTip: "two", d: "M 400 100 a 100 50 30 1 1 250 50" },
{ toolTip: "three", d: "M 400 300 a 100 50 45 1 1 250 50" },
{ toolTip: "four", d: "M 750 200 a 100 50 135 1 1 250 50" }
];
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Elipse:</strong> <span style='color:red'>" + d.toolTip + "</span>";
});
svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("stroke-width", 3)
.attr("stroke", "black")
.attr("fill", "none")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.append("path")
.attr("d", function(d) { return d.d })
.on('mouseover', highLight)
.on('mouseout', unHighLight);
svg.call(tip);
function highLight(){
var foo = d3.select(this);
foo.attr("stroke","red");
}
function unHighLight(){
var foo = d3.select(this);
foo.attr("stroke","black");
}
</script>
</body>
</html>
正文{font:10px无衬线;}
.d3提示{
背景:rgba(0,0,0,0.8);
边界半径:2px;
颜色:#fff;
字体大小:粗体;
线高:1;
填充:12px;
}
var w=窗内宽度,
h=窗内高度,
边距={顶部:40,右侧:20,底部:20,左侧:40};
var svg=d3.select(“body”).append(“svg”).attr({
宽度:w,
身高:h
});
变量数据集=[
{工具提示:“一”,d:“M 50 200 a 100 50 0 1 250 50”},
{工具提示:“两个”,d:“M 400 100 a 100 50 30 1 1 250 50”},
{工具提示:“三”,d:“M 400 300 a 100 50 45 1 250 50”},
{工具提示:“四”,d:“M 750 200 a 100 50 135 1 1 250 50”}
];
var tip=d3.tip()
.attr('class','d3 tip')
.偏移量([-10,0])
.html(函数(d){
返回“省略:”+d.toolTip+”;
});
svg.selectAll(“g”)
.数据(数据集)
.输入()
.附加(“g”)
.attr(“笔划宽度”,3)
.attr(“笔划”、“黑色”)
.attr(“填充”、“无”)
.on('mouseover',tip.show)
.on('mouseout',tip.hide)
.append(“路径”)
.attr(“d”,函数(d){return d.d})
.on('mouseover',突出显示)
.on('mouseout',不亮);
svg.call(tip);
函数高亮显示(){
var foo=d3。选择(此);
食物属性(“笔划”、“红色”);
}
函数取消高亮(){
var foo=d3。选择(此);
foo.attr(“笔划”、“黑色”);
}
这是另一个问题,Jai,请在下面找到我的d3提示答案,如果您需要更多详细信息,请让我确切知道您的要求。感谢布瓦内斯的回复。情况是,我有一个世界地图,显示了3个不同的位置,我用两个椭圆弧连接。现在,将会有许多这样的弧从地图上显示的不同位置连接起来。我的意图是,当用户将鼠标悬停在圆弧的任何部分上时,将弹出一个工具提示,显示我也需要的描述性信息,此外,如果可能,必须高亮显示。在使用Tipsy.js之前,我已经实现了工具提示,但是我无法让它用于此。达米安,我非常感谢你的回答。我能够在代码中成功地实现它。我现在唯一的问题是如何突出显示用户选择悬停的椭圆弧。我的意图