1010'use strict' ;
1111
1212var d3 = require ( 'd3' ) ;
13- var tinycolor = require ( 'tinycolor2' ) ;
1413var isNumeric = require ( 'fast-isnumeric' ) ;
1514
1615var Lib = require ( '../../lib' ) ;
@@ -246,9 +245,7 @@ function quadrature(dx, dy) {
246245
247246// size and display constants for hover text
248247var HOVERARROWSIZE = constants . HOVERARROWSIZE ,
249- HOVERTEXTPAD = constants . HOVERTEXTPAD ,
250- HOVERFONTSIZE = constants . HOVERFONTSIZE ,
251- HOVERFONT = constants . HOVERFONT ;
248+ HOVERTEXTPAD = constants . HOVERTEXTPAD ;
252249
253250// fx.hover: highlight data on hover
254251// evt can be a mousemove event, or an object with data about what points
@@ -755,23 +752,36 @@ function cleanPoint(d, hovermode) {
755752 return d ;
756753}
757754
755+ /*
756+ * Draw a single hover item in a pre-existing svg container somewhere
757+ * hoverItem should have keys:
758+ * - x and y (or x0, x1, y0, and y1):
759+ * the pixel position to mark, relative to opts.container
760+ * - xLabel, yLabel, zLabel, text, and name:
761+ * info to go in the label
762+ * - color:
763+ * the background color for the label.
764+ * - idealAlign (optional):
765+ * 'left' or 'right' for which side of the x/y box to try to put this on first
766+ * - borderColor (optional):
767+ * color for the border, defaults to strongest contrast with color
768+ * - fontFamily (optional):
769+ * string, the font for this label, defaults to constants.HOVERFONT
770+ * - fontSize (optional):
771+ * the label font size, defaults to constants.HOVERFONTSIZE
772+ * - fontColor (optional):
773+ * defaults to borderColor
774+ * opts should have keys:
775+ * - bgColor:
776+ * the background color this is against, used if the trace is
777+ * non-opaque, and for the name, which goes outside the box
778+ * - container:
779+ * a <svg> or <g> element to add the hover label to
780+ * - outerContainer:
781+ * normally a parent of `container`, sets the bounding box to use to
782+ * constrain the hover label and determine whether to show it on the left or right
783+ */
758784fx . loneHover = function ( hoverItem , opts ) {
759- // draw a single hover item in a pre-existing svg container somewhere
760- // hoverItem should have keys:
761- // - x and y (or x0, x1, y0, and y1):
762- // the pixel position to mark, relative to opts.container
763- // - xLabel, yLabel, zLabel, text, and name:
764- // info to go in the label
765- // - color:
766- // the background color for the label. text & outline color will
767- // be chosen black or white to contrast with this
768- // opts should have keys:
769- // - bgColor:
770- // the background color this is against, used if the trace is
771- // non-opaque, and for the name, which goes outside the box
772- // - container:
773- // a dom <svg> element - must be big enough to contain the whole
774- // hover label
775785 var pointData = {
776786 color : hoverItem . color || Color . defaultLine ,
777787 x0 : hoverItem . x0 || hoverItem . x || 0 ,
@@ -785,6 +795,12 @@ fx.loneHover = function(hoverItem, opts) {
785795 name : hoverItem . name ,
786796 idealAlign : hoverItem . idealAlign ,
787797
798+ // optional extra bits of styling
799+ borderColor : hoverItem . borderColor ,
800+ fontFamily : hoverItem . fontFamily ,
801+ fontSize : hoverItem . fontSize ,
802+ fontColor : hoverItem . fontColor ,
803+
788804 // filler to make createHoverText happy
789805 trace : {
790806 index : 0 ,
@@ -830,6 +846,12 @@ function createHoverText(hoverData, opts) {
830846 container = opts . container ,
831847 outerContainer = opts . outerContainer ,
832848
849+ // opts.fontFamily/Size are used for the common label
850+ // and as defaults for each hover label, though the individual labels
851+ // can override this.
852+ fontFamily = opts . fontFamily || constants . HOVERFONT ,
853+ fontSize = opts . fontSize || constants . HOVERFONTSIZE ,
854+
833855 c0 = hoverData [ 0 ] ,
834856 xa = c0 . xa ,
835857 ya = c0 . ya ,
@@ -874,7 +896,7 @@ function createHoverText(hoverData, opts) {
874896 lpath . enter ( ) . append ( 'path' )
875897 . style ( { fill : Color . defaultLine , 'stroke-width' : '1px' , stroke : Color . background } ) ;
876898 ltext . enter ( ) . append ( 'text' )
877- . call ( Drawing . font , HOVERFONT , HOVERFONTSIZE , Color . background )
899+ . call ( Drawing . font , fontFamily , fontSize , Color . background )
878900 // prohibit tex interpretation until we can handle
879901 // tex and regular text together
880902 . attr ( 'data-notex' , 1 ) ;
@@ -955,13 +977,12 @@ function createHoverText(hoverData, opts) {
955977 // trace name label (rect and text.name)
956978 g . append ( 'rect' )
957979 . call ( Color . fill , Color . addOpacity ( bgColor , 0.8 ) ) ;
958- g . append ( 'text' ) . classed ( 'name' , true )
959- . call ( Drawing . font , HOVERFONT , HOVERFONTSIZE ) ;
980+ g . append ( 'text' ) . classed ( 'name' , true ) ;
960981 // trace data label (path and text.nums)
961982 g . append ( 'path' )
962983 . style ( 'stroke-width' , '1px' ) ;
963984 g . append ( 'text' ) . classed ( 'nums' , true )
964- . call ( Drawing . font , HOVERFONT , HOVERFONTSIZE ) ;
985+ . call ( Drawing . font , fontFamily , fontSize ) ;
965986 } ) ;
966987 hoverLabels . exit ( ) . remove ( ) ;
967988
@@ -977,8 +998,7 @@ function createHoverText(hoverData, opts) {
977998 traceColor = Color . combine ( baseColor , bgColor ) ,
978999
9791000 // find a contrasting color for border and text
980- contrastColor = tinycolor ( traceColor ) . getBrightness ( ) > 128 ?
981- '#000' : Color . background ;
1001+ contrastColor = d . borderColor || Color . contrast ( traceColor ) ;
9821002
9831003 // to get custom 'name' labels pass cleanPoint
9841004 if ( d . nameOverride !== undefined ) d . name = d . nameOverride ;
@@ -1023,7 +1043,10 @@ function createHoverText(hoverData, opts) {
10231043
10241044 // main label
10251045 var tx = g . select ( 'text.nums' )
1026- . style ( 'fill' , contrastColor )
1046+ . call ( Drawing . font ,
1047+ d . fontFamily || fontFamily ,
1048+ d . fontSize || fontSize ,
1049+ d . fontColor || contrastColor )
10271050 . call ( Drawing . setPosition , 0 , 0 )
10281051 . text ( text )
10291052 . attr ( 'data-notex' , 1 )
@@ -1036,7 +1059,10 @@ function createHoverText(hoverData, opts) {
10361059
10371060 // secondary label for non-empty 'name'
10381061 if ( name && name !== text ) {
1039- tx2 . style ( 'fill' , traceColor )
1062+ tx2 . call ( Drawing . font ,
1063+ d . fontFamily || fontFamily ,
1064+ d . fontSize || fontSize ,
1065+ traceColor )
10401066 . text ( name )
10411067 . call ( Drawing . setPosition , 0 , 0 )
10421068 . attr ( 'data-notex' , 1 )
0 commit comments