Skip to content

Commit 3344864

Browse files
committed
Honor colorscale
1 parent a2799e2 commit 3344864

3 files changed

Lines changed: 59 additions & 29 deletions

File tree

src/traces/quiver/plot.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var d3 = require('@plotly/d3');
44

55
var Lib = require('../../lib');
66
var Drawing = require('../../components/drawing');
7-
var Colorscale = require('../../components/colorscale');
7+
var colorscaleStroke = require('./style').colorscaleStroke;
88

99
module.exports = function plot(gd, plotinfo, cdscatter, scatterLayer, transitionOpts, makeOnCompleteCallback) {
1010
var join, onComplete;
@@ -179,22 +179,9 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
179179
var lineColor = Lib.isArrayOrTypedArray(marker.color) ? undefined : marker.color;
180180
Drawing.lineGroupStyle(lineSegments, markerLine.width, lineColor, markerLine.dash);
181181

182-
// If colorscale present, color arrows by marker.color or magnitude |(u,v)|
183-
if(trace._hasColorscale) {
184-
var colorFunc = Colorscale.makeColorScaleFuncFromTrace(marker);
185-
lineSegments.style('stroke', function(cdi) {
186-
var markerColor = marker.color;
187-
var value;
188-
if(Lib.isArrayOrTypedArray(markerColor) && markerColor.length > cdi.i && isFinite(markerColor[cdi.i])) {
189-
value = markerColor[cdi.i];
190-
} else {
191-
var uVal = (trace.u && trace.u[cdi.i]) || 0;
192-
var vVal = (trace.v && trace.v[cdi.i]) || 0;
193-
value = Math.sqrt(uVal * uVal + vVal * vVal);
194-
}
195-
return colorFunc(value);
196-
});
197-
}
182+
// If colorscale present, color arrows by marker.color or magnitude |(u,v)|.
183+
// Shared with style.js so the static render and restyle stay in sync.
184+
if(trace._hasColorscale) colorscaleStroke(lineSegments, trace);
198185

199186
// Render text labels at data points
200187
var textGroup = d3.select(element).selectAll('g.text')

src/traces/quiver/style.js

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,30 @@ var Lib = require('../../lib');
55

66
var Drawing = require('../../components/drawing');
77
var Color = require('../../components/color');
8+
var Colorscale = require('../../components/colorscale');
89
var 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+
1032
function 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

64105
module.exports = {
65106
style: style,
66-
styleOnSelect: styleOnSelect
107+
styleOnSelect: styleOnSelect,
108+
colorscaleStroke: colorscaleStroke
67109
};

test/image/mocks/quiver_colorscale.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"sizeref": 0.3,
1111
"anchor": "tail",
1212
"marker": {
13+
"color": [1.41, 2.24, 3.16, 2.24, 2.83, 3.61, 3.16, 3.61, 4.24],
1314
"colorscale": "Viridis",
1415
"showscale": true,
1516
"colorbar": {

0 commit comments

Comments
 (0)