@@ -5,8 +5,30 @@ var Lib = require('../../lib');
55
66var Drawing = require ( '../../components/drawing' ) ;
77var Color = require ( '../../components/color' ) ;
8+ var Colorscale = require ( '../../components/colorscale' ) ;
89var DESELECTDIM = require ( '../../constants/interactions' ) . DESELECTDIM ;
910
11+ // Stroke each arrow path according to the trace colorscale, using marker.color
12+ // when it is an array of scalar values, otherwise falling back to the vector
13+ // magnitude |(u, v)|. `paths` may be a multi-path selection or a single path.
14+ function colorscaleStroke ( paths , trace ) {
15+ var marker = trace . marker || { } ;
16+ var colorFunc = Colorscale . makeColorScaleFuncFromTrace ( marker ) ;
17+
18+ paths . style ( 'stroke' , function ( cdi ) {
19+ var markerColor = marker . color ;
20+ var value ;
21+ if ( Lib . isArrayOrTypedArray ( markerColor ) && markerColor . length > cdi . i && isFinite ( markerColor [ cdi . i ] ) ) {
22+ value = markerColor [ cdi . i ] ;
23+ } else {
24+ var uVal = ( trace . u && trace . u [ cdi . i ] ) || 0 ;
25+ var vVal = ( trace . v && trace . v [ cdi . i ] ) || 0 ;
26+ value = Math . sqrt ( uVal * uVal + vVal * vVal ) ;
27+ }
28+ return colorFunc ( value ) ;
29+ } ) ;
30+ }
31+
1032function style ( gd ) {
1133 var s = d3 . select ( gd ) . selectAll ( 'g.trace.quiver' ) ;
1234
@@ -16,8 +38,12 @@ function style(gd) {
1638 var markerLine = marker . line || { } ;
1739 var lineColor = Lib . isArrayOrTypedArray ( marker . color ) ? undefined : marker . color ;
1840
19- d3 . select ( this ) . selectAll ( 'path.js-line' )
20- . call ( Drawing . lineGroupStyle , markerLine . width , lineColor , markerLine . dash ) ;
41+ var paths = d3 . select ( this ) . selectAll ( 'path.js-line' ) ;
42+ paths . call ( Drawing . lineGroupStyle , markerLine . width , lineColor , markerLine . dash ) ;
43+
44+ // colorscale strokes must be applied after lineGroupStyle, which would
45+ // otherwise flatten every arrow to a single (line.color) stroke
46+ if ( trace . _hasColorscale ) colorscaleStroke ( paths , trace ) ;
2147 } ) ;
2248}
2349
@@ -26,6 +52,7 @@ function styleOnSelect(gd, cd, sel) {
2652 var marker = trace . marker || { } ;
2753 var markerLine = marker . line || { } ;
2854 var lineColor = Lib . isArrayOrTypedArray ( marker . color ) ? undefined : marker . color ;
55+ var hasColorscale = trace . _hasColorscale ;
2956
3057 if ( ! sel ) return ;
3158
@@ -39,29 +66,44 @@ function styleOnSelect(gd, cd, sel) {
3966 var path = d3 . select ( this ) ;
4067
4168 if ( d . selected ) {
42- var sc = selectedLine . color || lineColor ;
4369 var sw = selectedLine . width !== undefined ? selectedLine . width : markerLine . width ;
44- Drawing . lineGroupStyle ( path , sw , sc , markerLine . dash ) ;
70+ if ( selectedLine . color ) {
71+ Drawing . lineGroupStyle ( path , sw , selectedLine . color , markerLine . dash ) ;
72+ } else if ( hasColorscale ) {
73+ Drawing . lineGroupStyle ( path , sw , lineColor , markerLine . dash ) ;
74+ colorscaleStroke ( path , trace ) ;
75+ path . style ( 'stroke-opacity' , 1 ) ;
76+ } else {
77+ Drawing . lineGroupStyle ( path , sw , lineColor , markerLine . dash ) ;
78+ }
4579 } else {
4680 var uc = unselectedLine . color ;
47- var uw = unselectedLine . width ;
48- if ( ! uc ) {
49- uc = lineColor ? Color . addOpacity ( lineColor , DESELECTDIM ) : undefined ;
81+ var uw = unselectedLine . width !== undefined ? unselectedLine . width : markerLine . width ;
82+ if ( uc ) {
83+ Drawing . lineGroupStyle ( path , uw , uc , markerLine . dash ) ;
84+ } else if ( hasColorscale ) {
85+ // keep colorscale color but dim via opacity
86+ Drawing . lineGroupStyle ( path , uw , lineColor , markerLine . dash ) ;
87+ colorscaleStroke ( path , trace ) ;
88+ path . style ( 'stroke-opacity' , DESELECTDIM ) ;
89+ } else {
90+ var dimColor = lineColor ? Color . addOpacity ( lineColor , DESELECTDIM ) : undefined ;
91+ Drawing . lineGroupStyle ( path , uw , dimColor , markerLine . dash ) ;
5092 }
51- if ( uw === undefined ) uw = markerLine . width ;
52- Drawing . lineGroupStyle ( path , uw , uc , markerLine . dash ) ;
5393 }
5494 } ) ;
5595
5696 Drawing . selectedTextStyle ( sel . selectAll ( 'text' ) , trace ) ;
5797 } else {
58- sel . selectAll ( 'path.js-line' )
59- . call ( Drawing . lineGroupStyle , markerLine . width , lineColor , markerLine . dash ) ;
98+ var paths = sel . selectAll ( 'path.js-line' ) ;
99+ paths . call ( Drawing . lineGroupStyle , markerLine . width , lineColor , markerLine . dash ) ;
100+ if ( hasColorscale ) colorscaleStroke ( paths , trace ) ;
60101 Drawing . textPointStyle ( sel . selectAll ( 'text' ) , trace , gd ) ;
61102 }
62103}
63104
64105module . exports = {
65106 style : style ,
66- styleOnSelect : styleOnSelect
107+ styleOnSelect : styleOnSelect ,
108+ colorscaleStroke : colorscaleStroke
67109} ;
0 commit comments