Skip to content

Commit d7e283a

Browse files
authored
Merge pull request #1719 from plotly/mesh3d-colorscale-settings
Mesh3d colorscale settings
2 parents e37eeae + 76a103f commit d7e283a

15 files changed

+140
-19
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@
6666
"fast-isnumeric": "^1.1.1",
6767
"gl-contour2d": "^1.1.2",
6868
"gl-error2d": "^1.2.1",
69-
"gl-error3d": "^1.0.5",
69+
"gl-error3d": "^1.0.6",
7070
"gl-heatmap2d": "^1.0.3",
7171
"gl-line2d": "^1.4.1",
7272
"gl-line3d": "^1.1.0",
7373
"gl-mat4": "^1.1.2",
74-
"gl-mesh3d": "^1.2.0",
74+
"gl-mesh3d": "^1.3.0",
7575
"gl-plot2d": "^1.2.0",
7676
"gl-plot3d": "^1.5.4",
7777
"gl-pointcloud2d": "^1.0.0",

src/plot_api/plot_api.js

+4
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,7 @@ function _restyle(gd, aobj, _traces) {
13831383
];
13841384

13851385
var zscl = ['zmin', 'zmax'],
1386+
cscl = ['cmin', 'cmax'],
13861387
xbins = ['xbins.start', 'xbins.end', 'xbins.size'],
13871388
ybins = ['ybins.start', 'ybins.end', 'ybins.size'],
13881389
contourAttrs = ['contours.start', 'contours.end', 'contours.size'];
@@ -1482,6 +1483,9 @@ function _restyle(gd, aobj, _traces) {
14821483
if(zscl.indexOf(ai) !== -1) {
14831484
doextra('zauto', false, i);
14841485
}
1486+
if(cscl.indexOf(ai) !== -1) {
1487+
doextra('cauto', false, i);
1488+
}
14851489
else if(ai === 'colorscale') {
14861490
doextra('autocolorscale', false, i);
14871491
}

src/traces/mesh3d/attributes.js

+4
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ module.exports = {
165165
width: extendFlat({}, surfaceAtts.contours.x.width)
166166
},
167167

168+
cauto: colorscaleAttrs.zauto,
169+
cmin: colorscaleAttrs.zmin,
170+
cmax: colorscaleAttrs.zmax,
168171
colorscale: colorscaleAttrs.colorscale,
169172
reversescale: colorscaleAttrs.reversescale,
173+
autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}),
170174
showscale: colorscaleAttrs.showscale,
171175
colorbar: colorbarAttrs,
172176

src/traces/mesh3d/calc.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright 2012-2017, 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 colorscaleCalc = require('../../components/colorscale/calc');
12+
13+
module.exports = function calc(gd, trace) {
14+
if(trace.intensity) {
15+
colorscaleCalc(trace, trace.intensity, '', 'c');
16+
}
17+
};

src/traces/mesh3d/colorbar.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2012-2017, 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 isNumeric = require('fast-isnumeric');
12+
13+
var Lib = require('../../lib');
14+
var Plots = require('../../plots/plots');
15+
var Colorscale = require('../../components/colorscale');
16+
var drawColorbar = require('../../components/colorbar/draw');
17+
18+
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);
27+
28+
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
29+
30+
if(!trace.showscale) {
31+
Plots.autoMargin(gd, cbId);
32+
return;
33+
}
34+
35+
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
36+
var sclFunc = Colorscale.makeColorScaleFunc(
37+
Colorscale.extractScale(
38+
trace.colorscale,
39+
cmin,
40+
cmax
41+
),
42+
{ noNumericCheck: true }
43+
);
44+
45+
cb.fillcolor(sclFunc)
46+
.filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254})
47+
.options(trace.colorbar)();
48+
};

src/traces/mesh3d/convert.js

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ proto.update = function(data) {
124124
if(data.intensity) {
125125
this.color = '#fff';
126126
config.vertexIntensity = data.intensity;
127+
config.vertexIntensityBounds = [data.cmin, data.cmax];
127128
config.colormap = parseColorScale(data.colorscale);
128129
}
129130
else if(data.vertexcolor) {

src/traces/mesh3d/defaults.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

1212
var Registry = require('../../registry');
1313
var Lib = require('../../lib');
14-
var colorbarDefaults = require('../../components/colorbar/defaults');
14+
var colorscaleDefaults = require('../../components/colorscale/defaults');
1515
var attributes = require('./attributes');
1616

17-
1817
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
1918
function coerce(attr, dflt) {
2019
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
@@ -77,23 +76,12 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7776

7877
if('intensity' in traceIn) {
7978
coerce('intensity');
80-
coerce('showscale', true);
81-
}
82-
else {
79+
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'});
80+
} else {
8381
traceOut.showscale = false;
8482

8583
if('facecolor' in traceIn) coerce('facecolor');
8684
else if('vertexcolor' in traceIn) coerce('vertexcolor');
8785
else coerce('color', defaultColor);
8886
}
89-
90-
if(traceOut.reversescale) {
91-
traceOut.colorscale = traceOut.colorscale.map(function(si) {
92-
return [1 - si[0], si[1]];
93-
}).reverse();
94-
}
95-
96-
if(traceOut.showscale) {
97-
colorbarDefaults(traceIn, traceOut, layout);
98-
}
9987
};

src/traces/mesh3d/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var Mesh3D = {};
1313

1414
Mesh3D.attributes = require('./attributes');
1515
Mesh3D.supplyDefaults = require('./defaults');
16-
Mesh3D.colorbar = require('../heatmap/colorbar');
16+
Mesh3D.calc = require('./calc');
17+
Mesh3D.colorbar = require('./colorbar');
1718
Mesh3D.plot = require('./convert');
1819

1920
Mesh3D.moduleType = 'trace';

src/traces/surface/attributes.js

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ module.exports = {
122122
].join(' ')
123123
},
124124

125-
// Todo this block has a structure of colorscale/attributes.js but with colorscale/color_attributes.js names
126125
cauto: colorscaleAttrs.zauto,
127126
cmin: colorscaleAttrs.zmin,
128127
cmax: colorscaleAttrs.zmax,
-994 Bytes
Loading

test/image/baselines/gl3d_bunny.png

-1.07 KB
Loading
-1.07 KB
Loading
-393 Bytes
Loading

test/image/mocks/gl3d_tetrahedra.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
[1, "rgb(0, 0, 255)"]
1717
],
1818
"intensity": [0, 0.33, 0.66, 1],
19+
"cmin": -0.5,
20+
"cmax": 0.5,
1921
"colorbar": {
2022
"x": 0,
2123
"title": "for colorscale + intensity tetrahedra",

test/jasmine/tests/mesh3d_test.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var Plotly = require('@lib');
2+
var createGraphDiv = require('../assets/create_graph_div');
3+
var destroyGraphDiv = require('../assets/destroy_graph_div');
4+
var fail = require('../assets/fail_test');
5+
6+
describe('Test mesh3d restyle', function() {
7+
afterEach(destroyGraphDiv);
8+
9+
it('should clear *cauto* when restyle *cmin* and/or *cmax*', function(done) {
10+
var gd = createGraphDiv();
11+
12+
function _assert(user, full) {
13+
var trace = gd.data[0];
14+
var fullTrace = gd._fullData[0];
15+
16+
expect(trace.cauto).toBe(user[0], 'user cauto');
17+
expect(trace.cmin).toEqual(user[1], 'user cmin');
18+
expect(trace.cmax).toEqual(user[2], 'user cmax');
19+
expect(fullTrace.cauto).toBe(full[0], 'full cauto');
20+
expect(fullTrace.cmin).toEqual(full[1], 'full cmin');
21+
expect(fullTrace.cmax).toEqual(full[2], 'full cmax');
22+
}
23+
24+
Plotly.plot(gd, [{
25+
type: 'mesh3d',
26+
x: [0, 1, 2, 0],
27+
y: [0, 0, 1, 2],
28+
z: [0, 2, 0, 1],
29+
i: [0, 0, 0, 1],
30+
j: [1, 2, 3, 2],
31+
k: [2, 3, 1, 3],
32+
intensity: [0, 0.33, 0.66, 3]
33+
}])
34+
.then(function() {
35+
_assert([true, 0, 3], [true, 0, 3]);
36+
37+
return Plotly.restyle(gd, 'cmin', 0);
38+
})
39+
.then(function() {
40+
_assert([false, 0, 3], [false, 0, 3]);
41+
42+
return Plotly.restyle(gd, 'cmax', 10);
43+
})
44+
.then(function() {
45+
_assert([false, 0, 10], [false, 0, 10]);
46+
47+
return Plotly.restyle(gd, 'cauto', true);
48+
})
49+
.then(function() {
50+
_assert([true, 0, 3], [true, 0, 3]);
51+
52+
return Plotly.purge(gd);
53+
})
54+
.catch(fail)
55+
.then(done);
56+
});
57+
});

0 commit comments

Comments
 (0)