Skip to content

Commit fb3db10

Browse files
committed
added x y contours on or between all data points of surface trace
1 parent a956a22 commit fb3db10

File tree

5 files changed

+125
-11
lines changed

5 files changed

+125
-11
lines changed

src/traces/surface/attributes.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ function makeContourAttr(axLetter) {
4343
'dimension are drawn.'
4444
].join(' ')
4545
},
46+
onpoints: {
47+
valType: 'number',
48+
role: 'info',
49+
max: 1,
50+
min: 0,
51+
dflt: 0,
52+
description: [
53+
'Determines whether or contour lines about the', axLetter,
54+
'dimension are drawn on all data points. Ratios between 0 and 1',
55+
'e.g. 0.5 could also be applied to locate contours between data points'
56+
].join(' ')
57+
},
4658
project: {
4759
x: makeContourProjAttr('x'),
4860
y: makeContourProjAttr('y'),
@@ -257,3 +269,4 @@ colorscaleAttrs('', {
257269

258270
attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';
259271
attrs.transforms = undefined;
272+
delete attrs.contours.z.onpoints;

src/traces/surface/convert.js

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ function SurfaceTrace(scene, surface, uid) {
2525
this.surface = surface;
2626
this.data = null;
2727
this.showContour = [false, false, false];
28+
this.onPointsContour = [false, false]; // note: only available on x & y not z.
2829
this.minValues = [Infinity, Infinity, Infinity];
2930
this.maxValues = [-Infinity, -Infinity, -Infinity];
3031
this.dataScaleX = 1.0;
3132
this.dataScaleY = 1.0;
3233
this.refineData = true;
34+
this.objectOffset = [0, 0, 0];
3335
}
3436

3537
var proto = SurfaceTrace.prototype;
@@ -347,10 +349,53 @@ proto.setContourLevels = function() {
347349
var nlevels = [[], [], []];
348350
var needsUpdate = false;
349351

350-
for(var i = 0; i < 3; ++i) {
352+
var i, j;
353+
354+
for(i = 0; i < 3; ++i) {
351355
if(this.showContour[i]) {
352356
needsUpdate = true;
353-
nlevels[i] = this.scene.contourLevels[i];
357+
358+
nlevels[i] = [];
359+
for(j = 0; j < this.scene.contourLevels[i].length; j++) {
360+
nlevels[i][j] = this.scene.contourLevels[i][j];
361+
}
362+
363+
if(i < 2 && this.onPointsContour[i] !== 0) {
364+
365+
var ratio = this.onPointsContour[i];
366+
367+
var len = (i === 0) ?
368+
this.data.z[0].length :
369+
this.data._ylength;
370+
371+
for(var q = (ratio < 1) ? 1 : 0; q < len; q++) {
372+
373+
var here = (i === 0) ?
374+
this.getXat(q, 0) * this.scene.dataScale[i] :
375+
this.getYat(0, q) * this.scene.dataScale[i];
376+
377+
var value;
378+
if(ratio < 1) {
379+
var prev = (i === 0) ?
380+
this.getXat(q - 1, 0) * this.scene.dataScale[i] :
381+
this.getYat(0, q - 1) * this.scene.dataScale[i];
382+
383+
value = here * ratio + prev * (1 - ratio);
384+
} else {
385+
value = here;
386+
}
387+
388+
var found = false;
389+
for(j = 0; j < nlevels[i].length; j++) {
390+
if(value === nlevels[i][j]) {
391+
found = true;
392+
break;
393+
}
394+
}
395+
if(found === false) nlevels[i].push(value);
396+
}
397+
}
398+
354399
}
355400
}
356401

@@ -437,15 +482,15 @@ proto.update = function(data) {
437482
}
438483

439484
for(i = 0; i < 3; i++) {
440-
data._objectOffset[i] = 0.5 * (this.minValues[i] + this.maxValues[i]);
485+
this.objectOffset[i] = 0.5 * (this.minValues[i] + this.maxValues[i]);
441486
}
442487

443488
for(i = 0; i < 3; i++) {
444489
for(j = 0; j < xlen; j++) {
445490
for(k = 0; k < ylen; k++) {
446491
v = rawCoords[i][j][k];
447492
if(v !== null && v !== undefined) {
448-
rawCoords[i][j][k] -= data._objectOffset[i];
493+
rawCoords[i][j][k] -= this.objectOffset[i];
449494
}
450495
}
451496
}
@@ -545,8 +590,11 @@ proto.update = function(data) {
545590
surface.highlightTint[i] = params.contourTint[i] = 1;
546591
}
547592
params.contourWidth[i] = contourParams.width;
593+
594+
if(i < 2) this.onPointsContour[i] = contourParams.onpoints;
548595
} else {
549596
this.showContour[i] = false;
597+
if(i < 2) this.onPointsContour[i] = 0;
550598
}
551599

552600
if(contourParams.highlight) {
@@ -560,11 +608,7 @@ proto.update = function(data) {
560608
params.vertexColor = true;
561609
}
562610

563-
params.objectOffset = [
564-
data._objectOffset[0],
565-
data._objectOffset[1],
566-
data._objectOffset[2]
567-
];
611+
params.objectOffset = this.objectOffset;
568612

569613
params.coords = coords;
570614
surface.update(params);

src/traces/surface/defaults.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3636
traceOut._xlength = (Array.isArray(x) && Lib.isArrayOrTypedArray(x[0])) ? z.length : z[0].length;
3737
traceOut._ylength = z.length;
3838

39-
traceOut._objectOffset = [0, 0, 0];
40-
4139
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
4240
handleCalendarDefaults(traceIn, traceOut, ['x', 'y', 'z'], layout);
4341

@@ -76,6 +74,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7674
coerce(contourDim + '.color');
7775
coerce(contourDim + '.width');
7876
coerce(contourDim + '.usecolormap');
77+
if(i < 2) coerce(contourDim + '.onpoints');
7978
}
8079

8180
if(highlight) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"data": [
3+
{
4+
"type": "surface",
5+
"contours": {
6+
"x": { "show": true, "onpoints": 1 },
7+
"y": { "show": true, "onpoints": 1 },
8+
"z": { "show": false}
9+
},
10+
"x": [0, 0.25, 0.5, 0.75, 1],
11+
"y": [0, 0.25, 0.5, 0.75, 1],
12+
"z": [
13+
[0, 1, 0, 1, 0],
14+
[1, 0, 1, 0, 1],
15+
[0, 1, 0, 1, 0],
16+
[1, 0, 1, 0, 1],
17+
[0, 1, 0, 1, 0]
18+
]
19+
},
20+
{
21+
"type": "surface",
22+
"contours": {
23+
"x": { "show": true, "onpoints": 0.5 },
24+
"y": { "show": true, "onpoints": 0.5 },
25+
"z": { "show": false}
26+
},
27+
"x": [1.5, 1.75, 2, 2.25, 2.5],
28+
"y": [0, 0.25, 0.5, 0.75, 1],
29+
"z": [
30+
[0, 1, 0, 1, 0],
31+
[1, 0, 1, 0, 1],
32+
[0, 1, 0, 1, 0],
33+
[1, 0, 1, 0, 1],
34+
[0, 1, 0, 1, 0]
35+
]
36+
}
37+
],
38+
"layout": {
39+
"title": "Surface contours on or between all x & y data points",
40+
"width": 900,
41+
"height": 600,
42+
"scene": {
43+
"xaxis": { "nticks": 1 },
44+
"yaxis": { "nticks": 1 },
45+
"zaxis": { "nticks": 1 },
46+
"camera": {
47+
"eye": { "x": 0, "y": -0.9, "z": 1.8 },
48+
"center": { "x": 0, "y": 0, "z": 0 }
49+
},
50+
"aspectratio": {
51+
"x": 2,
52+
"y": 1,
53+
"z": 1
54+
}
55+
}
56+
}
57+
}

test/jasmine/tests/surface_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('Test surface', function() {
6262
expect(traceOut.contours.x).toEqual(fullOpts);
6363
expect(traceOut.contours.y).toEqual(Lib.extendDeep({}, fullOpts, {
6464
show: true,
65+
onpoints: 0,
6566
color: '#444',
6667
width: 2,
6768
usecolormap: false

0 commit comments

Comments
 (0)