Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly.js display tooltip on hover over 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?

like image 931
synaptik Avatar asked Jun 07 '18 19:06

synaptik


1 Answers

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.

Please go through the below code and let me know if any doubts, also let me know if your issue is resolved, Thanks!

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>
like image 151
Naren Murali Avatar answered Sep 18 '22 19:09

Naren Murali