Plotly.js display tooltip on hover over graph title
Asked Answered
O

2

5

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?

Osteopath answered 7/6, 2018 at 19:17 Comment(0)
O
3

Instead 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

Osteopath answered 11/6, 2018 at 19:49 Comment(0)
A
3

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.com/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>
Anterior answered 9/6, 2018 at 9:28 Comment(3)
@Osteopath does this answer help you?Anterior
I used a small but important part of your response, but not your whole solution.Osteopath
@Osteopath Your solution is better than mine!!! glad to be of assistanceAnterior
O
3

Instead 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

Osteopath answered 11/6, 2018 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.