Skip to content

Commit 913c6e6

Browse files
committed
reduce opacityscale and move to volume folder - pass 1
1 parent 401e390 commit 913c6e6

File tree

7 files changed

+149
-175
lines changed

7 files changed

+149
-175
lines changed

src/components/opacityscale/attributes.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/components/opacityscale/defaults.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/components/opacityscale/scales.js

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/lib/coerce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var tinycolor = require('tinycolor2');
1313

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

1919
var nestedProperty = require('./nested_property');

src/traces/volume/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var opacityscaleAttrs = require('../../components/opacityscale/attributes');
11+
var opacityscaleAttrs = require('./opacityscale').attributes;
1212
var colorscaleAttrs = require('../../components/colorscale/attributes');
1313
var isosurfaceAttrs = require('../isosurface/attributes');
1414
var baseAttrs = require('../../plots/attributes');

src/traces/volume/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
var isosurfaceDefaults = require('../isosurface/defaults');
12-
var opacityscaleDefaults = require('../../components/opacityscale/defaults');
12+
var opacityscaleDefaults = require('./opacityscale').defaults;
1313
var Lib = require('../../lib');
1414
var attributes = require('./attributes');
1515

src/traces/volume/opacityscale.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* Copyright 2012-2019, 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 min = 0.2;
12+
13+
var scales = {
14+
'max': [
15+
[0, min], [1, 1]
16+
],
17+
18+
'min': [
19+
[0, 1], [1, min]
20+
],
21+
22+
'extremes': createWave(1, min)
23+
};
24+
25+
var defaultScale = scales.uniform;
26+
27+
var paletteStr = Object.keys(scales);
28+
29+
/**
30+
* Make opacityscale attribute declarations for
31+
*
32+
* - opacityscale,
33+
*
34+
* @param {string} context (dflt: '', i.e. from trace root):
35+
* the container this is in ('', *marker*, *marker.line* etc)
36+
*
37+
* @param {object} opts:
38+
* - opacityscaleDflt {string}:
39+
* overrides the opacityscale dflt
40+
*
41+
* - editTypeOverride {boolean} (dflt: ''):
42+
* most of these attributes already require a recalc, but the ones that do not
43+
* have editType *style* or *plot* unless you override (presumably with *calc*)
44+
*
45+
* @return {object}
46+
*/
47+
48+
function attributes(context, opts) {
49+
context = context || '';
50+
opts = opts || {};
51+
52+
var opacityscaleDflt = typeof opts.opacityscaleDflt === 'string' ? scales[opts.opacityscaleDflt] : null;
53+
54+
var attrs = {};
55+
56+
attrs.opacityscale = {
57+
valType: 'opacityscale',
58+
role: 'style',
59+
editType: 'calc',
60+
dflt: opacityscaleDflt,
61+
description: [
62+
'Sets the opacityscale.',
63+
' The opacityscale must be an array containing',
64+
' arrays mapping a normalized value to an opacity value.',
65+
' At minimum, a mapping for the lowest (0) and highest (1)',
66+
' values are required. For example,',
67+
' `[[0, 1], [0.5, 0.2], [1, 1]]` means that higher/lower values would have',
68+
' higher opacity values and those in the middle would be more transparent',
69+
' Alternatively, `opacityscale` may be a palette name string',
70+
' of the following list: ' + paletteStr + '.'
71+
].join('')
72+
};
73+
74+
return attrs;
75+
}
76+
77+
function defaults(traceIn, traceOut, layout, coerce, opts) {
78+
var prefix = opts.prefix;
79+
80+
coerce(prefix + 'opacityscale');
81+
}
82+
83+
function createWave(n, minOpacity) {
84+
var arr = [];
85+
var steps = 32; // Max: 256
86+
for(var i = 0; i < steps; i++) {
87+
var u = i / (steps - 1);
88+
var v = minOpacity + (1 - minOpacity) * (1 - Math.pow(Math.sin(n * u * Math.PI), 2));
89+
arr.push([
90+
u,
91+
Math.max(1, Math.min(0, v))
92+
]);
93+
}
94+
return arr;
95+
}
96+
97+
function getScale(scl, dflt) {
98+
if(!dflt) dflt = defaultScale;
99+
if(!scl) return dflt;
100+
101+
function parseScale() {
102+
try {
103+
scl = palette[scl] || JSON.parse(scl);
104+
} catch(e) {
105+
scl = dflt;
106+
}
107+
}
108+
109+
if(typeof scl === 'string') {
110+
parseScale();
111+
// occasionally scl is double-JSON encoded...
112+
if(typeof scl === 'string') parseScale();
113+
}
114+
115+
if(!isValidScaleArray(scl)) return dflt;
116+
return scl;
117+
}
118+
119+
function isValidScaleArray(scl) {
120+
var highestVal = 0;
121+
122+
if(!Array.isArray(scl) || scl.length < 2) return false;
123+
124+
if(!scl[0] || !scl[scl.length - 1]) return false;
125+
126+
if(+scl[0][0] !== 0 || +scl[scl.length - 1][0] !== 1) return false;
127+
128+
for(var i = 0; i < scl.length; i++) {
129+
var si = scl[i];
130+
131+
if(si.length !== 2 || +si[0] < highestVal) {
132+
return false;
133+
}
134+
135+
highestVal = +si[0];
136+
}
137+
138+
return true;
139+
}
140+
141+
module.exports = {
142+
attributes: attributes,
143+
defaults: defaults,
144+
scales: scales,
145+
get: getScale
146+
};

0 commit comments

Comments
 (0)