admin管理员组文章数量:1330576
Plotly.js allows you to specify a graph title, but there doesn't seem to be an option for specifying a longer description to be shown on hovering over the title.
So, I added a title
attribute to the text
element that encloses the title text, and then activated JQueryUI Tooltips on the page. But nothing seems to happen when I hover over the title. Here's how I added the title
attribute and activated the tooltips:
$('div#myDiv g.g-gtitle > text.gtitle')[0].title = 'This is a long description that should show on hover over the title';
$( document ).tooltip();
I've also tried the following, which doesn't work either:
$('div#myDiv g.g-gtitle > text.gtitle').attr('title', 'This is a long description that should show on hover over the title');
$( document ).tooltip();
Full example code here:
Any idea how I can display a tooltip when hovering over the graph title?
Plotly.js allows you to specify a graph title, but there doesn't seem to be an option for specifying a longer description to be shown on hovering over the title.
So, I added a title
attribute to the text
element that encloses the title text, and then activated JQueryUI Tooltips on the page. But nothing seems to happen when I hover over the title. Here's how I added the title
attribute and activated the tooltips:
$('div#myDiv g.g-gtitle > text.gtitle')[0].title = 'This is a long description that should show on hover over the title';
$( document ).tooltip();
I've also tried the following, which doesn't work either:
$('div#myDiv g.g-gtitle > text.gtitle').attr('title', 'This is a long description that should show on hover over the title');
$( document ).tooltip();
Full example code here: https://codepen.io/synaptik/pen/eKBYbE
Any idea how I can display a tooltip when hovering over the graph title?
Share Improve this question edited Jun 7, 2018 at 19:32 synaptik asked Jun 7, 2018 at 19:17 synaptiksynaptik 9,53916 gold badges73 silver badges102 bronze badges2 Answers
Reset to default 3Instead of using JQuery Tooltips, I went with Tippy. However, I still had to modify the Plotly chart like this (thanks to @Naren-Murali):
$('div#myDiv g.g-gtitle > text.gtitle').addClass('graphWrapperPointerEvents');
with the CSS:
.graphWrapperPointerEvents {
pointer-events: all !important;
}
Solution: https://codepen.io/synaptik/pen/LrWMKM
I checked your code: Jquery-ui-tooltip
is causing some problems when working together with Plotly. I don't want to get into specifics, but basically, on hover a new element is getting added to the previous hover element.
This is my version of a solution for your question, Plotly has set all the graph elements to pointer-events:none
where no events will happen on those elements, so I disabled this using the CSS pointer-events: all
. Also we need to wrap the graph inside a div
and a span
containing the contents of the tooltip, thus using JavaScript events mouseover
and mouseout
, we can hide/show the tooltip.
var data_x = ['2018-05-01', '2018-05-02', '2018-05-03', '2018-05-04', '2018-05-05', '2018-05-06', '2018-05-07', '2018-05-08', '2018-05-09'];
// data
var Data = {
type: 'scatter',
x: data_x,
y: [4, 2, -1, 4, -5, -7, 0, 3, 8],
mode: 'lines+markers',
name: 'Data',
showlegend: true,
hoverinfo: 'all',
line: {
color: 'blue',
width: 2
},
marker: {
color: 'blue',
size: 8,
symbol: 'circle'
}
}
// layout
var layout = {
title: 'My Title',
xaxis: {
zeroline: false
},
yaxis: {
range: [data_x[0], data_x[data_x.length - 1]],
zeroline: false
}
}
Plotly.plot('myDiv', [Data], layout);
$('#myDiv text.gtitle').on('mouseover', function(e) {
var hovertext = $('span.custom-tooltip');
var pos = e.target.getBoundingClientRect();
hovertext.css("left", (pos.left - (hovertext.width() / 2) + (pos.width / 2)) + "px");
hovertext.css("top", pos.top * 1.5 + "px");
hovertext.css("visibility", "visible");
})
$('#myDiv text.gtitle').on('mouseout', function(e) {
var hovertext = $('span.custom-tooltip');
hovertext.css("visibility", "hidden");
})
.wrapper {
position: relative;
}
.wrapper .custom-tooltip {
visibility: hidden;
display: inline;
width: fit-content;
position: absolute;
left: 0px;
right: 0px;
}
#myDiv text.gtitle {
pointer-events: all !important;
}
.wrapper .custom-tooltip.show {
visibility: visible;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<!-- Plotly chart will be drawn inside this DIV -->
<div class="wrapper">
<div id="myDiv"></div>
<span class="custom-tooltip">This is a really long title</span>
</div>
<script>
/* JAVASCRIPT CODE GOES HERE */
</script>
本文标签: javascriptPlotlyjs display tooltip on hover over graph titleStack Overflow
版权声明:本文标题:javascript - Plotly.js display tooltip on hover over graph title - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742258890a2442143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论