Skip to content

Commit e82a826

Browse files
committed
reduced opacityscale code - updated baseline - revised jasmine test
1 parent 64652f9 commit e82a826

File tree

6 files changed

+77
-183
lines changed

6 files changed

+77
-183
lines changed

src/lib/coerce.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var tinycolor = require('tinycolor2');
1313

1414
var baseTraceAttrs = require('../plots/attributes');
1515
var colorscales = require('../components/colorscale/scales');
16-
var opacityscales = require('../traces/volume/opacityscale');
1716
var DESELECTDIM = require('../constants/interactions').DESELECTDIM;
1817

1918
var nestedProperty = require('./nested_property');
@@ -177,21 +176,6 @@ exports.valObjectMeta = {
177176
propOut.set(colorscales.get(v, dflt));
178177
}
179178
},
180-
opacityscale: {
181-
description: [
182-
'A Plotly opacityscale either picked by a name:',
183-
'(any of', Object.keys(opacityscales.scales).join(', '), ')',
184-
'customized as an {array} of 2-element {arrays} where',
185-
'the first element is the normalized opacity level value',
186-
'(starting at *0* and ending at *1*),',
187-
'and the second item is a valid opacity value between 0 and 1.'
188-
].join(' '),
189-
requiredOpts: [],
190-
otherOpts: ['dflt'],
191-
coerceFunction: function(v, propOut, dflt) {
192-
propOut.set(opacityscales.get(v, dflt));
193-
}
194-
},
195179
angle: {
196180
description: [
197181
'A number (in degree) between -180 and 180.'

src/traces/volume/attributes.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
'use strict';
1010

11-
var opacityscaleAttrs = require('./opacityscale').attributes;
1211
var colorscaleAttrs = require('../../components/colorscale/attributes');
1312
var isosurfaceAttrs = require('../isosurface/attributes');
1413
var baseAttrs = require('../../plots/attributes');
@@ -56,10 +55,6 @@ var attrs = module.exports = overrideAll(extendFlat({
5655
hovertemplate: isosurfaceAttrs.hovertemplate
5756
},
5857

59-
opacityscaleAttrs('', {
60-
editTypeOverride: 'calc'
61-
}),
62-
6358
colorscaleAttrs('', {
6459
colorAttr: '`value`',
6560
showScaleDflt: true,
@@ -68,6 +63,24 @@ colorscaleAttrs('', {
6863

6964
colorbar: isosurfaceAttrs.colorbar,
7065
opacity: isosurfaceAttrs.opacity,
66+
opacityscale: {
67+
valType: 'any',
68+
role: 'style',
69+
editType: 'calc',
70+
description: [
71+
'Sets the opacityscale.',
72+
' The opacityscale must be an array containing',
73+
' arrays mapping a normalized value to an opacity value.',
74+
' At minimum, a mapping for the lowest (0) and highest (1)',
75+
' values are required. For example,',
76+
' `[[0, 1], [0.5, 0.2], [1, 1]]` means that higher/lower values would have',
77+
' higher opacity values and those in the middle would be more transparent',
78+
' Alternatively, `opacityscale` may be a palette name string',
79+
' of the following list: \'min\', \'max\', \'extremes\' and \'uniform\'.',
80+
' The default is \'uniform\'.'
81+
].join('')
82+
},
83+
7184
lightposition: isosurfaceAttrs.lightposition,
7285
lighting: isosurfaceAttrs.lighting,
7386
flatshading: isosurfaceAttrs.flatshading,

src/traces/volume/defaults.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,63 @@
99
'use strict';
1010

1111
var isosurfaceDefaults = require('../isosurface/defaults');
12-
var opacityscaleDefaults = require('./opacityscale').defaults;
1312
var Lib = require('../../lib');
1413
var attributes = require('./attributes');
1514

15+
var MIN = 0.1; // Note: often we don't want the data cube to be disappeared
16+
17+
function createWave(n, minOpacity) {
18+
var arr = [];
19+
var steps = 32; // Max: 256
20+
for(var i = 0; i < steps; i++) {
21+
var u = i / (steps - 1);
22+
var v = minOpacity + (1 - minOpacity) * (1 - Math.pow(Math.sin(n * u * Math.PI), 2));
23+
arr.push([
24+
u,
25+
Math.max(1, Math.min(0, v))
26+
]);
27+
}
28+
return arr;
29+
}
30+
1631
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
1732
function coerce(attr, dflt) {
1833
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
1934
}
2035

2136
isosurfaceDefaults(traceIn, traceOut, defaultColor, layout);
2237

23-
opacityscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: ''});
38+
var opacityscale = coerce('opacityscale');
39+
if(opacityscale === 'max') {
40+
traceOut.opacityscale = [[0, MIN], [1, 1]];
41+
} else if(opacityscale === 'min') {
42+
traceOut.opacityscale = [[0, 1], [1, MIN]];
43+
} else if(opacityscale === 'extremes') {
44+
traceOut.opacityscale = createWave(1, MIN);
45+
} else if(!isValidScaleArray(opacityscale)) {
46+
traceOut.opacityscale = undefined;
47+
}
48+
2449
};
50+
51+
function isValidScaleArray(scl) {
52+
var highestVal = 0;
53+
54+
if(!Array.isArray(scl) || scl.length < 2) return false;
55+
56+
if(!scl[0] || !scl[scl.length - 1]) return false;
57+
58+
if(+scl[0][0] !== 0 || +scl[scl.length - 1][0] !== 1) return false;
59+
60+
for(var i = 0; i < scl.length; i++) {
61+
var si = scl[i];
62+
63+
if(si.length !== 2 || +si[0] < highestVal) {
64+
return false;
65+
}
66+
67+
highestVal = +si[0];
68+
}
69+
70+
return true;
71+
}

src/traces/volume/opacityscale.js

Lines changed: 0 additions & 150 deletions
This file was deleted.
19.1 KB
Loading

test/jasmine/tests/volume_test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ describe('Test volume', function() {
314314
});
315315
});
316316

317-
describe('@noCI hover', function() {
317+
describe('hover', function() {
318318

319319
var gd;
320320

@@ -358,8 +358,8 @@ describe('Test volume', function() {
358358
nums: [
359359
'x: 0.4',
360360
'y: 100μ',
361-
'z: −16',
362-
'value: −1.32'
361+
'z: −8',
362+
'value: −1.31'
363363
].join('\n')
364364
});
365365
})
@@ -368,10 +368,10 @@ describe('Test volume', function() {
368368
.then(function() {
369369
assertHoverLabelContent({
370370
nums: [
371-
'x: 0.3',
371+
'x: 0.4',
372372
'y: 0.001',
373373
'z: −16',
374-
'value: −0.27'
374+
'value: −0.32'
375375
].join('\n')
376376
});
377377
})
@@ -382,8 +382,8 @@ describe('Test volume', function() {
382382
nums: [
383383
'x: 0.3',
384384
'y: 100μ',
385-
'z: −16',
386-
'value: −1.2'
385+
'z: −8',
386+
'value: −1.19'
387387
].join('\n')
388388
});
389389
})
@@ -393,9 +393,9 @@ describe('Test volume', function() {
393393
assertHoverLabelContent({
394394
nums: [
395395
'x: 0.4',
396-
'y: 100μ',
397-
'z: −4',
398-
'value: −1.3'
396+
'y: 0.001',
397+
'z: −2',
398+
'value: −1.26'
399399
].join('\n')
400400
});
401401
})

0 commit comments

Comments
 (0)