Skip to content

Commit cb7cb7f

Browse files
committed
get cone colorbar and vx/vy/vz mesh grid to work
1 parent 5736290 commit cb7cb7f

File tree

6 files changed

+147
-95
lines changed

6 files changed

+147
-95
lines changed

src/traces/cone/attributes.js

+36-26
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,70 @@ var extendFlat = require('../../lib/extend').extendFlat;
1919
var attrs = {
2020
x: {
2121
valType: 'data_array',
22-
editType: 'calc',
23-
description: ''
22+
role: 'info',
23+
editType: 'calc+clearAxisTypes',
24+
description: [
25+
'Sets the x positions of the cones.',
26+
'When `vx`, `vy`, `vz` are not set,',
27+
' these are also the x coordinates of the u/v/w vector field.'
28+
].join(' ')
2429
},
2530
y: {
2631
valType: 'data_array',
27-
editType: 'calc'
32+
role: 'info',
33+
editType: 'calc+clearAxisTypes',
34+
description: [
35+
'Sets the y positions of the cones.',
36+
'When `vx`, `vy`, `vz` are not set,',
37+
' these are also the y coordinates of the u/v/w vector field.'
38+
].join(' ')
2839
},
2940
z: {
3041
valType: 'data_array',
31-
editType: 'calc'
42+
role: 'info',
43+
editType: 'calc+clearAxisTypes',
44+
description: [
45+
'Sets the z positions of the cones.',
46+
'When `vx`, `vy`, `vz` are not set,',
47+
' these are also the z coordinates of the u/v/w vector field.'
48+
].join(' ')
3249
},
3350

3451
u: {
3552
valType: 'data_array',
3653
editType: 'calc',
37-
description: [
38-
].join(' ')
54+
description: 'Sets the x components of the vector field.'
3955
},
4056
v: {
4157
valType: 'data_array',
4258
editType: 'calc',
43-
description: [
44-
].join(' ')
45-
59+
description: 'Sets the y components of the vector field.'
4660
},
4761
w: {
4862
valType: 'data_array',
4963
editType: 'calc',
50-
description: [
51-
].join(' ')
52-
64+
description: 'Sets the z components of the vector field.'
5365
},
5466

55-
cx: {
67+
vx: {
5668
valType: 'data_array',
57-
editType: 'calc+clearAxisTypes',
58-
description: [
59-
].join(' ')
69+
editType: 'calc',
70+
description: 'Sets the x coordinates of the vector field mesh.'
6071
},
61-
cy: {
72+
vy: {
6273
valType: 'data_array',
63-
editType: 'calc+clearAxisTypes',
64-
description: [
65-
].join(' ')
74+
editType: 'calc',
75+
description: 'Sets the y coordinates of the vector field mesh.'
6676
},
67-
cz: {
77+
vz: {
6878
valType: 'data_array',
69-
editType: 'calc+clearAxisTypes',
70-
description: [
71-
].join(' ')
79+
editType: 'calc',
80+
description: 'Sets the z coordinates of the vector field mesh.'
7281
},
7382

7483
// TODO
7584
// sizemode: {},
76-
// sizescale: {},
85+
// sizeref: {},
7786

7887
text: {
7988
valType: 'string',
@@ -87,10 +96,11 @@ var attrs = {
8796
}
8897
};
8998

90-
extendFlat(attrs, colorAttrs('', 'calc', false), {
99+
extendFlat(attrs, colorAttrs('', 'calc', true), {
91100
showscale: colorscaleAttrs.showscale,
92101
colorbar: colorbarAttrs
93102
});
103+
delete attrs.color;
94104

95105
var fromMesh3d = ['opacity', 'flatshading', 'lightposition', 'lighting'];
96106

src/traces/cone/calc.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
'use strict';
1010

11+
var cone2mesh = require('./helpers').cone2mesh;
1112
var colorscaleCalc = require('../../components/colorscale/calc');
1213

1314
module.exports = function calc(gd, trace) {
14-
if(trace.intensity) {
15-
colorscaleCalc(trace, trace.intensity, '', 'c');
16-
}
15+
var fullLayout = gd._fullLayout;
16+
17+
// TODO skip when 'cmin' and 'cmax' are set
18+
// TODO find way to "merge" this cone2mesh call with the one in convert.js
19+
//
20+
// TODO should show in absolute or normalize length?
21+
22+
var meshData = cone2mesh(trace, fullLayout[trace.scene]);
23+
24+
colorscaleCalc(trace, meshData.vertexIntensity, '', 'c');
1725
};

src/traces/cone/colorbar.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@
88

99
'use strict';
1010

11-
var isNumeric = require('fast-isnumeric');
12-
13-
var Lib = require('../../lib');
1411
var Plots = require('../../plots/plots');
1512
var Colorscale = require('../../components/colorscale');
1613
var drawColorbar = require('../../components/colorbar/draw');
1714

1815
module.exports = function colorbar(gd, cd) {
19-
var trace = cd[0].trace,
20-
cbId = 'cb' + trace.uid,
21-
cmin = trace.cmin,
22-
cmax = trace.cmax,
23-
vals = trace.intensity || [];
24-
25-
if(!isNumeric(cmin)) cmin = Lib.aggNums(Math.min, null, vals);
26-
if(!isNumeric(cmax)) cmax = Lib.aggNums(Math.max, null, vals);
16+
var trace = cd[0].trace;
17+
var cbId = 'cb' + trace.uid;
18+
var cmin = trace.cmin;
19+
var cmax = trace.cmax;
2720

2821
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
2922

@@ -34,12 +27,8 @@ module.exports = function colorbar(gd, cd) {
3427

3528
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
3629
var sclFunc = Colorscale.makeColorScaleFunc(
37-
Colorscale.extractScale(
38-
trace.colorscale,
39-
cmin,
40-
cmax
41-
),
42-
{ noNumericCheck: true }
30+
Colorscale.extractScale(trace.colorscale, cmin, cmax),
31+
{noNumericCheck: true}
4332
);
4433

4534
cb.fillcolor(sclFunc)

src/traces/cone/convert.js

+4-38
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99

1010
'use strict';
1111

12-
var cone2mesh = require('gl-cone3d');
13-
var createMesh = cone2mesh.createConeMesh;
12+
var createMesh = require('gl-cone3d').createConeMesh;
13+
var cone2mesh = require('./helpers').cone2mesh;
1414

1515
function Mesh3DTrace(scene, mesh, uid) {
1616
this.scene = scene;
1717
this.uid = uid;
1818
this.mesh = mesh;
19-
this.name = '';
20-
this.color = '#fff';
2119
this.data = null;
22-
this.showContour = false;
2320
}
2421

2522
var proto = Mesh3DTrace.prototype;
@@ -45,40 +42,9 @@ proto.handlePick = function(selection) {
4542
}
4643
};
4744

48-
function zip3(x, y, z) {
49-
var result = new Array(x.length);
50-
for(var i = 0; i < x.length; ++i) {
51-
result[i] = [x[i], y[i], z[i]];
52-
}
53-
return result;
54-
}
55-
5645
function convert(scene, trace) {
57-
var layout = scene.fullSceneLayout;
58-
59-
// Unpack position data
60-
function toDataCoords(axis, coord, scale) {
61-
return coord.map(function(x) {
62-
return axis.d2l(x) * scale;
63-
});
64-
}
65-
66-
var meshData = cone2mesh({
67-
positions: zip3(
68-
toDataCoords(layout.xaxis, trace.cx, scene.dataScale[0]),
69-
toDataCoords(layout.yaxis, trace.cy, scene.dataScale[1]),
70-
toDataCoords(layout.zaxis, trace.cz, scene.dataScale[2])
71-
),
72-
vectors: zip3(
73-
toDataCoords(layout.xaxis, trace.u, scene.dataScale[0]),
74-
toDataCoords(layout.yaxis, trace.v, scene.dataScale[1]),
75-
toDataCoords(layout.zaxis, trace.w, scene.dataScale[2])
76-
),
77-
colormap: 'portland'
78-
});
79-
80-
return meshData;
81-
};
46+
return cone2mesh(trace, scene.fullSceneLayout, scene.dataScale);
47+
}
8248

8349
proto.update = function(data) {
8450
this.data = data;

src/traces/cone/defaults.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,29 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
1919
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2020
}
2121

22-
// TODO do 2D versions of these work?
22+
var u = coerce('u');
23+
var v = coerce('v');
24+
var w = coerce('w');
2325

2426
var x = coerce('x');
2527
var y = coerce('y');
2628
var z = coerce('z');
2729

28-
traceOut._xlength = x.length;
29-
traceOut._ylength = y.length;
30-
traceOut._zlength = z.length;
31-
32-
coerce('u');
33-
coerce('v');
34-
coerce('w');
30+
if(
31+
!u || !u.length || !v || !v.length || !w || !w.length ||
32+
!x || !x.length || !y || !y.length || !z || !z.length
33+
) {
34+
traceOut.visible = false;
35+
return;
36+
}
3537

36-
coerce('cx');
37-
coerce('cy');
38-
coerce('cz');
38+
coerce('vx');
39+
coerce('vy');
40+
coerce('vz');
3941

4042
coerce('text');
4143

44+
// TODO do these attributes work?
4245
coerce('lighting.ambient');
4346
coerce('lighting.diffuse');
4447
coerce('lighting.specular');
@@ -50,5 +53,12 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
5053
coerce('lightposition.y');
5154
coerce('lightposition.z');
5255

56+
// TODO should the default be viridis
57+
// ... and should we restrict cmin,cmax > 0 ???
5358
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'});
59+
60+
// disable 1D transforms
61+
// x/y/z should match lengths, u/v/w and vx/vy/vz should match as well, but
62+
// the two sets have different lengths so transforms wouldn't work.
63+
traceOut._length = null;
5464
};

src/traces/cone/helpers.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var conePlot = require('gl-cone3d');
12+
var simpleMap = require('../../lib').simpleMap;
13+
var parseColorScale = require('../../lib/gl_format_color').parseColorScale;
14+
15+
function zip3(x, y, z) {
16+
var result = new Array(x.length);
17+
for(var i = 0; i < x.length; i++) {
18+
result[i] = [x[i], y[i], z[i]];
19+
}
20+
return result;
21+
}
22+
23+
var axisName2scaleIndex = {xaxis: 0, yaxis: 1, zaxis: 2};
24+
25+
exports.cone2mesh = function cone2mesh(trace, sceneLayout, dataScale) {
26+
var coneOpts = {};
27+
var toDataCoords;
28+
29+
if(Array.isArray(dataScale)) {
30+
toDataCoords = function(arr, axisName) {
31+
var ax = sceneLayout[axisName];
32+
var scale = dataScale[axisName2scaleIndex[axisName]];
33+
return simpleMap(arr, function(v) { return ax.d2l(v) * scale; });
34+
};
35+
} else {
36+
toDataCoords = function(arr, axisName) {
37+
return simpleMap(arr, sceneLayout[axisName].d2l);
38+
};
39+
}
40+
41+
coneOpts.vectors = zip3(
42+
toDataCoords(trace.u, 'xaxis'),
43+
toDataCoords(trace.v, 'yaxis'),
44+
toDataCoords(trace.w, 'zaxis')
45+
);
46+
47+
coneOpts.positions = zip3(
48+
toDataCoords(trace.x, 'xaxis'),
49+
toDataCoords(trace.y, 'yaxis'),
50+
toDataCoords(trace.z, 'zaxis')
51+
);
52+
53+
if(trace.vx && trace.vy && trace.vz) {
54+
coneOpts.meshgrid = [
55+
toDataCoords(trace.vx, 'xaxis'),
56+
toDataCoords(trace.vy, 'yaxis'),
57+
toDataCoords(trace.vz, 'zaxis')
58+
];
59+
}
60+
61+
coneOpts.colormap = parseColorScale(trace.colorscale);
62+
coneOpts.vertexIntensityBounds = [trace.cmin, trace.cmax];
63+
64+
// sizemode:
65+
// sizeref,
66+
coneOpts.coneSize = 2;
67+
68+
return conePlot(coneOpts);
69+
};

0 commit comments

Comments
 (0)