Skip to content

Commit be6ed97

Browse files
committed
Include colorscale attributes in quiver
1 parent 2c8ba24 commit be6ed97

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/traces/quiver/attributes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var fontAttrs = require('../../plots/font_attributes');
66
var dash = require('../../components/drawing/attributes').dash;
77

88
var extendFlat = require('../../lib/extend').extendFlat;
9+
var colorScaleAttrs = require('../../components/colorscale/attributes');
910

1011
var attrs = {
1112
x: {
@@ -223,6 +224,16 @@ var attrs = {
223224
// Extend with base attributes (includes hoverinfo, etc.)
224225
extendFlat(attrs, baseAttrs);
225226

227+
// Colorscale attributes to color arrows by |(u,v)| magnitude
228+
extendFlat(
229+
attrs,
230+
colorScaleAttrs('', {
231+
colorAttr: 'u/v norm',
232+
showScaleDflt: true,
233+
editTypeOverride: 'calc'
234+
})
235+
);
236+
226237
// Add hoverinfo with proper flags for quiver
227238
// We need to create a new object to avoid mutating the shared base attributes
228239
attrs.hoverinfo = extendFlat({}, baseAttrs.hoverinfo, {

src/traces/quiver/defaults.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
var Lib = require('../../lib');
44
var attributes = require('./attributes');
5+
var Colorscale = require('../../components/colorscale');
6+
var colorscaleDefaults = Colorscale.handleDefaults;
7+
var hasColorscale = Colorscale.hasColorscale;
58

69
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
710
// Selection styling - use coerce to set proper defaults
@@ -61,6 +64,26 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
6164
// traceOut.hoverinfo will be set by Lib.coerceHoverinfo in plots.js
6265
traceOut.hovertemplate = traceIn.hovertemplate;
6366

67+
// Colorscale for magnitude coloring: compute cmin/cmax from |(u,v)|
68+
var cmin = Infinity;
69+
var cmax = -Infinity;
70+
for (var k = 0; k < len; k++) {
71+
var uu = (traceOut.u && traceOut.u[k]) || (traceIn.u && traceIn.u[k]) || 0;
72+
var vv = (traceOut.v && traceOut.v[k]) || (traceIn.v && traceIn.v[k]) || 0;
73+
var nrm = Math.sqrt(uu * uu + vv * vv);
74+
if (isFinite(nrm)) {
75+
if (nrm < cmin) cmin = nrm;
76+
if (nrm > cmax) cmax = nrm;
77+
}
78+
}
79+
if (!isFinite(cmin)) cmin = 0;
80+
if (!isFinite(cmax)) cmax = 1;
81+
if (traceIn.cmin === undefined && traceOut.cmin === undefined) traceOut.cmin = cmin;
82+
if (traceIn.cmax === undefined && traceOut.cmax === undefined) traceOut.cmax = cmax;
83+
// Flag colorscale and apply defaults (adds colorscale, showscale, colorbar, etc.)
84+
traceOut._hasColorscale = hasColorscale(traceIn) || true;
85+
colorscaleDefaults(traceIn, traceOut, layout, coerce, { prefix: '', cLetter: 'c' });
86+
6487
// Text
6588
traceOut.text = traceIn.text;
6689
traceOut.textposition = traceIn.textposition || 'middle center';

src/traces/quiver/plot.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var d3 = require('@plotly/d3');
55
var Registry = require('../../registry');
66
var Lib = require('../../lib');
77
var Drawing = require('../../components/drawing');
8+
var Colorscale = require('../../components/colorscale');
89

910
module.exports = function plot(gd, plotinfo, cdscatter, scatterLayer, transitionOpts, makeOnCompleteCallback) {
1011
var join, onComplete;
@@ -184,6 +185,17 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
184185
// Apply styling using Plotly's standard styling system
185186
Drawing.lineGroupStyle(lineSegments, trace.line && trace.line.width, trace.line && trace.line.color, trace.line && trace.line.dash);
186187

188+
// If colorscale present, color arrows by magnitude |(u,v)|
189+
if (trace._hasColorscale) {
190+
var colorFunc = Colorscale.makeColorScaleFuncFromTrace(trace);
191+
lineSegments.style('stroke', function(cdi) {
192+
var uVal = (trace.u && trace.u[cdi.i]) || 0;
193+
var vVal = (trace.v && trace.v[cdi.i]) || 0;
194+
var nVal = Math.sqrt(uVal * uVal + vVal * vVal);
195+
return colorFunc(nVal);
196+
});
197+
}
198+
187199
// Handle transitions
188200
if(transitionOpts && transitionOpts.duration > 0) {
189201
var transition = d3.transition()

0 commit comments

Comments
 (0)