10
10
'use strict' ;
11
11
12
12
var d3 = require ( 'd3' ) ;
13
- var tinycolor = require ( 'tinycolor2' ) ;
14
13
var isNumeric = require ( 'fast-isnumeric' ) ;
15
14
16
15
var Lib = require ( '../../lib' ) ;
@@ -246,9 +245,7 @@ function quadrature(dx, dy) {
246
245
247
246
// size and display constants for hover text
248
247
var HOVERARROWSIZE = constants . HOVERARROWSIZE ,
249
- HOVERTEXTPAD = constants . HOVERTEXTPAD ,
250
- HOVERFONTSIZE = constants . HOVERFONTSIZE ,
251
- HOVERFONT = constants . HOVERFONT ;
248
+ HOVERTEXTPAD = constants . HOVERTEXTPAD ;
252
249
253
250
// fx.hover: highlight data on hover
254
251
// evt can be a mousemove event, or an object with data about what points
@@ -755,23 +752,36 @@ function cleanPoint(d, hovermode) {
755
752
return d ;
756
753
}
757
754
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
+ */
758
784
fx . 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
775
785
var pointData = {
776
786
color : hoverItem . color || Color . defaultLine ,
777
787
x0 : hoverItem . x0 || hoverItem . x || 0 ,
@@ -785,6 +795,12 @@ fx.loneHover = function(hoverItem, opts) {
785
795
name : hoverItem . name ,
786
796
idealAlign : hoverItem . idealAlign ,
787
797
798
+ // optional extra bits of styling
799
+ borderColor : hoverItem . borderColor ,
800
+ fontFamily : hoverItem . fontFamily ,
801
+ fontSize : hoverItem . fontSize ,
802
+ fontColor : hoverItem . fontColor ,
803
+
788
804
// filler to make createHoverText happy
789
805
trace : {
790
806
index : 0 ,
@@ -830,6 +846,12 @@ function createHoverText(hoverData, opts) {
830
846
container = opts . container ,
831
847
outerContainer = opts . outerContainer ,
832
848
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
+
833
855
c0 = hoverData [ 0 ] ,
834
856
xa = c0 . xa ,
835
857
ya = c0 . ya ,
@@ -874,7 +896,7 @@ function createHoverText(hoverData, opts) {
874
896
lpath . enter ( ) . append ( 'path' )
875
897
. style ( { fill : Color . defaultLine , 'stroke-width' : '1px' , stroke : Color . background } ) ;
876
898
ltext . enter ( ) . append ( 'text' )
877
- . call ( Drawing . font , HOVERFONT , HOVERFONTSIZE , Color . background )
899
+ . call ( Drawing . font , fontFamily , fontSize , Color . background )
878
900
// prohibit tex interpretation until we can handle
879
901
// tex and regular text together
880
902
. attr ( 'data-notex' , 1 ) ;
@@ -955,13 +977,12 @@ function createHoverText(hoverData, opts) {
955
977
// trace name label (rect and text.name)
956
978
g . append ( 'rect' )
957
979
. 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 ) ;
960
981
// trace data label (path and text.nums)
961
982
g . append ( 'path' )
962
983
. style ( 'stroke-width' , '1px' ) ;
963
984
g . append ( 'text' ) . classed ( 'nums' , true )
964
- . call ( Drawing . font , HOVERFONT , HOVERFONTSIZE ) ;
985
+ . call ( Drawing . font , fontFamily , fontSize ) ;
965
986
} ) ;
966
987
hoverLabels . exit ( ) . remove ( ) ;
967
988
@@ -977,8 +998,7 @@ function createHoverText(hoverData, opts) {
977
998
traceColor = Color . combine ( baseColor , bgColor ) ,
978
999
979
1000
// 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 ) ;
982
1002
983
1003
// to get custom 'name' labels pass cleanPoint
984
1004
if ( d . nameOverride !== undefined ) d . name = d . nameOverride ;
@@ -1023,7 +1043,10 @@ function createHoverText(hoverData, opts) {
1023
1043
1024
1044
// main label
1025
1045
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 )
1027
1050
. call ( Drawing . setPosition , 0 , 0 )
1028
1051
. text ( text )
1029
1052
. attr ( 'data-notex' , 1 )
@@ -1036,7 +1059,10 @@ function createHoverText(hoverData, opts) {
1036
1059
1037
1060
// secondary label for non-empty 'name'
1038
1061
if ( name && name !== text ) {
1039
- tx2 . style ( 'fill' , traceColor )
1062
+ tx2 . call ( Drawing . font ,
1063
+ d . fontFamily || fontFamily ,
1064
+ d . fontSize || fontSize ,
1065
+ traceColor )
1040
1066
. text ( name )
1041
1067
. call ( Drawing . setPosition , 0 , 0 )
1042
1068
. attr ( 'data-notex' , 1 )
0 commit comments