-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
annotation hover text #1573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
annotation hover text #1573
Changes from 2 commits
58966ed
384353a
4ab7986
919580e
da1e9d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ var Plotly = require('../../plotly'); | |
var Plots = require('../../plots/plots'); | ||
var Lib = require('../../lib'); | ||
var Axes = require('../../plots/cartesian/axes'); | ||
var Fx = require('../../plots/cartesian/graph_interact'); | ||
var Color = require('../color'); | ||
var Drawing = require('../drawing'); | ||
var svgTextUtils = require('../../lib/svg_text_utils'); | ||
|
@@ -96,7 +97,16 @@ function drawOne(gd, index) { | |
var annGroup = fullLayout._infolayer.append('g') | ||
.classed('annotation', true) | ||
.attr('data-index', String(index)) | ||
.style('opacity', options.opacity) | ||
.style('opacity', options.opacity); | ||
|
||
// another group for text+background so that they can rotate together | ||
var annTextGroup = annGroup.append('g') | ||
.classed('annotation-text-g', true) | ||
.attr('data-index', String(index)); | ||
|
||
var annTextGroupInner = annTextGroup.append('g') | ||
.style('pointer-events', 'all') | ||
.call(setCursor, 'default') | ||
.on('click', function() { | ||
gd._dragging = false; | ||
gd.emit('plotly_clickannotation', { | ||
|
@@ -106,12 +116,33 @@ function drawOne(gd, index) { | |
}); | ||
}); | ||
|
||
// another group for text+background so that they can rotate together | ||
var annTextGroup = annGroup.append('g') | ||
.classed('annotation-text-g', true) | ||
.attr('data-index', String(index)); | ||
|
||
var annTextGroupInner = annTextGroup.append('g'); | ||
if(options.hovertext) { | ||
annTextGroupInner | ||
.on('mouseover', function() { | ||
var hoverOptions = options.hoverlabel; | ||
var hoverFont = hoverOptions.font; | ||
var bBox = this.getBoundingClientRect(); | ||
var bBoxRef = gd.getBoundingClientRect(); | ||
|
||
Fx.loneHover({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a new attribute This one defaults to false (so data point hover comes through) unless @cldougl once this is merged we should update the (currently broken) example in https://plot.ly/javascript/text-and-annotations/#styling-and-formatting-annotations, and add a section about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it-- just made an issue to keep track. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @cldougl - I moved further discussion of this into the docs issue you made. |
||
x0: bBox.left - bBoxRef.left, | ||
x1: bBox.right - bBoxRef.left, | ||
y: (bBox.top + bBox.bottom) / 2 - bBoxRef.top, | ||
text: options.hovertext, | ||
color: hoverOptions.bgcolor, | ||
borderColor: hoverOptions.bordercolor, | ||
fontFamily: hoverFont.family, | ||
fontSize: hoverFont.size, | ||
fontColor: hoverFont.color | ||
}, { | ||
container: fullLayout._hoverlayer.node(), | ||
outerContainer: fullLayout._paper.node() | ||
}); | ||
}) | ||
.on('mouseout', function() { | ||
Fx.loneUnhover(fullLayout._hoverlayer.node()); | ||
}); | ||
} | ||
|
||
var borderwidth = options.borderwidth, | ||
borderpad = options.borderpad, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ color.defaultLine = colorAttrs.defaultLine; | |
color.lightLine = colorAttrs.lightLine; | ||
color.background = colorAttrs.background; | ||
|
||
/* | ||
* tinyRGB: turn a tinycolor into an rgb string, but | ||
* unlike the built-in tinycolor.toRgbString this never includes alpha | ||
*/ | ||
color.tinyRGB = function(tc) { | ||
var c = tc.toRgb(); | ||
return 'rgb(' + Math.round(c.r) + ', ' + | ||
|
@@ -57,12 +61,23 @@ color.combine = function(front, back) { | |
return tinycolor(fcflat).toRgbString(); | ||
}; | ||
|
||
/* | ||
* Create a color that contrasts with cstr. | ||
* | ||
* If cstr is a dark color, we lighten it; if it's light, we darken. | ||
* | ||
* If lightAmount / darkAmount are used, we adjust by these percentages, | ||
* otherwise we go all the way to white or black. | ||
* TODO: black is what we've always done for hover, but should it be #444 instead? | ||
*/ | ||
color.contrast = function(cstr, lightAmount, darkAmount) { | ||
var tc = tinycolor(cstr); | ||
|
||
var newColor = tc.isLight() ? | ||
tc.darken(darkAmount) : | ||
tc.lighten(lightAmount); | ||
if(tc.getAlpha() !== 1) tc = tinycolor(color.combine(cstr, '#fff')); | ||
|
||
var newColor = tc.isDark() ? | ||
(lightAmount ? tc.lighten(lightAmount) : '#fff') : | ||
(darkAmount ? tc.darken(darkAmount) : '#000'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I switched from Thoughts about my TODO?
(ie There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds good! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 919580e |
||
|
||
return newColor.toString(); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plotly_clickannotation
was present, but didn't actually work before this, as far as I can tell, because it didn't havepointer-events
turned back on. Anyway I had to do this to make hover work. A potential downside to this is that annotations grab mouse interactions even if you don't use them, so if you have a data point that's entirely under an annotation you won't see it in hover. You could imagine only grabbing mouse events if you are using them for hover, but then you won't get click events, and at draw time it's generally not known yet whether click events are going to be captured... so I don't see any way around this.Anyway, I only attached click and hover to the text box, not to the arrows, and both are included in the new test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's sounds like the desired behavior to me.