Skip to content

Commit ec64895

Browse files
committed
scaffold cone
1 parent fb75158 commit ec64895

File tree

9 files changed

+367
-0
lines changed

9 files changed

+367
-0
lines changed

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Plotly.register([
2626
require('./scatter3d'),
2727
require('./surface'),
2828
require('./mesh3d'),
29+
require('../src/traces/cone'),
2930

3031
require('./scattergeo'),
3132
require('./choropleth'),

package-lock.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"es6-promise": "^3.0.2",
7171
"fast-isnumeric": "^1.1.1",
7272
"font-atlas-sdf": "^1.3.3",
73+
"gl-cone3d": "[email protected]:gl-vis/gl-cone3d#c675b25e5991e63d0ad23ed24d712c496c28cd72",
7374
"gl-contour2d": "^1.1.4",
7475
"gl-error3d": "^1.0.7",
7576
"gl-heatmap2d": "^1.0.4",

src/traces/cone/attributes.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 colorAttrs = require('../../components/colorscale/color_attributes');
12+
var colorscaleAttrs = require('../../components/colorscale/attributes');
13+
var colorbarAttrs = require('../../components/colorbar/attributes');
14+
var mesh3dAttrs = require('../mesh3d/attributes');
15+
var baseAttrs = require('../../plots/attributes');
16+
17+
var extendFlat = require('../../lib/extend').extendFlat;
18+
19+
var attrs = {
20+
x: {
21+
valType: 'data_array',
22+
editType: 'calc',
23+
description: ''
24+
},
25+
y: {
26+
valType: 'data_array',
27+
editType: 'calc'
28+
},
29+
z: {
30+
valType: 'data_array',
31+
editType: 'calc'
32+
},
33+
34+
u: {
35+
valType: 'data_array',
36+
editType: 'calc',
37+
description: [
38+
].join(' ')
39+
},
40+
v: {
41+
valType: 'data_array',
42+
editType: 'calc',
43+
description: [
44+
].join(' ')
45+
46+
},
47+
w: {
48+
valType: 'data_array',
49+
editType: 'calc',
50+
description: [
51+
].join(' ')
52+
53+
},
54+
55+
cx: {
56+
valType: 'data_array',
57+
editType: 'calc+clearAxisTypes',
58+
description: [
59+
].join(' ')
60+
},
61+
cy: {
62+
valType: 'data_array',
63+
editType: 'calc+clearAxisTypes',
64+
description: [
65+
].join(' ')
66+
},
67+
cz: {
68+
valType: 'data_array',
69+
editType: 'calc+clearAxisTypes',
70+
description: [
71+
].join(' ')
72+
},
73+
74+
// TODO
75+
// sizemode: {},
76+
// sizescale: {},
77+
78+
text: {
79+
valType: 'string',
80+
role: 'info',
81+
dflt: '',
82+
arrayOk: true,
83+
editType: 'calc',
84+
description: [
85+
86+
].join(' ')
87+
}
88+
};
89+
90+
extendFlat(attrs, colorAttrs('', 'calc', false), {
91+
showscale: colorscaleAttrs.showscale,
92+
colorbar: colorbarAttrs
93+
});
94+
95+
var fromMesh3d = ['opacity', 'flatshading', 'lightposition', 'lighting'];
96+
97+
fromMesh3d.forEach(function(k) {
98+
attrs[k] = mesh3dAttrs[k];
99+
});
100+
101+
attrs.hoverinfo = extendFlat({}, baseAttrs.hoverinfo, {editType: 'calc'});
102+
103+
module.exports = attrs;

src/traces/cone/calc.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 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/cone/colorbar.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 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/cone/convert.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
10+
'use strict';
11+
12+
var cone2mesh = require('gl-cone3d');
13+
var createMesh = cone2mesh.createConeMesh;
14+
15+
function Mesh3DTrace(scene, mesh, uid) {
16+
this.scene = scene;
17+
this.uid = uid;
18+
this.mesh = mesh;
19+
this.name = '';
20+
this.color = '#fff';
21+
this.data = null;
22+
this.showContour = false;
23+
}
24+
25+
var proto = Mesh3DTrace.prototype;
26+
27+
proto.handlePick = function(selection) {
28+
if(selection.object === this.mesh) {
29+
var selectIndex = selection.index = selection.data.index;
30+
31+
selection.traceCoordinate = [
32+
this.data.x[selectIndex],
33+
this.data.y[selectIndex],
34+
this.data.z[selectIndex]
35+
];
36+
37+
var text = this.data.text;
38+
if(Array.isArray(text) && text[selectIndex] !== undefined) {
39+
selection.textLabel = text[selectIndex];
40+
} else if(text) {
41+
selection.textLabel = text;
42+
}
43+
44+
return true;
45+
}
46+
};
47+
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+
56+
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+
};
82+
83+
proto.update = function(data) {
84+
this.data = data;
85+
this.mesh.update(convert(this.scene, data));
86+
};
87+
88+
proto.dispose = function() {
89+
this.scene.glplot.remove(this.mesh);
90+
this.mesh.dispose();
91+
};
92+
93+
function createMesh3DTrace(scene, data) {
94+
var gl = scene.glplot.gl;
95+
var meshData = convert(scene, data);
96+
var mesh = createMesh(gl, meshData);
97+
var result = new Mesh3DTrace(scene, mesh, data.uid);
98+
99+
result.data = data;
100+
mesh._trace = result;
101+
scene.glplot.add(mesh);
102+
103+
return result;
104+
}
105+
106+
module.exports = createMesh3DTrace;

src/traces/cone/defaults.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
10+
'use strict';
11+
12+
var Lib = require('../../lib');
13+
14+
var colorscaleDefaults = require('../../components/colorscale/defaults');
15+
var attributes = require('./attributes');
16+
17+
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
18+
function coerce(attr, dflt) {
19+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
20+
}
21+
22+
// TODO do 2D versions of these work?
23+
24+
var x = coerce('x');
25+
var y = coerce('y');
26+
var z = coerce('z');
27+
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');
35+
36+
coerce('cx');
37+
coerce('cy');
38+
coerce('cz');
39+
40+
coerce('text');
41+
42+
coerce('lighting.ambient');
43+
coerce('lighting.diffuse');
44+
coerce('lighting.specular');
45+
coerce('lighting.roughness');
46+
coerce('lighting.fresnel');
47+
coerce('lighting.vertexnormalsepsilon');
48+
coerce('lighting.facenormalsepsilon');
49+
coerce('lightposition.x');
50+
coerce('lightposition.y');
51+
coerce('lightposition.z');
52+
53+
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'});
54+
};

src/traces/cone/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
module.exports = {
12+
moduleType: 'trace',
13+
name: 'cone',
14+
basePlotModule: require('../../plots/gl3d'),
15+
categories: ['gl3d', '2dMap', 'noOpacity'],
16+
17+
attributes: require('./attributes'),
18+
supplyDefaults: require('./defaults'),
19+
colorbar: require('./colorbar'),
20+
calc: require('./calc'),
21+
plot: require('./convert'),
22+
23+
meta: {
24+
description: [
25+
'...'
26+
].join(' ')
27+
}
28+
};

0 commit comments

Comments
 (0)