Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03b1557

Browse files
committed
make contour and heatmap have distinct defaults step:
- split up xyz heatmap defaults logic to be reused by contour defaults - split up contour style defaults logic to be resued by histogram2d and histogram2dcontour
1 parent 7bc3e75 commit 03b1557

File tree

3 files changed

+134
-14
lines changed

3 files changed

+134
-14
lines changed

src/traces/contour/defaults.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
'use strict';
1111

1212
var Lib = require('../../lib');
13-
var heatmapSupplyDefaults = require('../heatmap/defaults');
1413

14+
var hasColumns = require('../heatmap/has_columns');
15+
var handleXYZDefaults = require('../heatmap/xyz_defaults');
16+
var handleStyleDefaults = require('../contour/style_defaults');
1517
var attributes = require('./attributes');
1618

1719

@@ -20,24 +22,21 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2022
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2123
}
2224

25+
var len = handleXYZDefaults(traceIn, traceOut, coerce);
26+
if(!len) {
27+
traceOut.visible = false;
28+
return;
29+
}
30+
31+
coerce('text');
32+
coerce('connectgaps', hasColumns(traceOut));
33+
2334
var contourStart = Lib.coerce2(traceIn, traceOut, attributes, 'contours.start'),
2435
contourEnd = Lib.coerce2(traceIn, traceOut, attributes, 'contours.end'),
2536
autocontour = coerce('autocontour', !(contourStart && contourEnd));
2637

2738
if(autocontour) coerce('ncontours');
2839
else coerce('contours.size');
2940

30-
var coloring = coerce('contours.coloring');
31-
32-
if(coloring === 'fill') coerce('contours.showlines');
33-
34-
if(traceOut.contours.showlines!==false) {
35-
if(coloring !== 'lines') coerce('line.color', '#000');
36-
coerce('line.width', 0.5);
37-
coerce('line.dash');
38-
}
39-
40-
coerce('line.smoothing');
41-
42-
heatmapSupplyDefaults(traceIn, traceOut, defaultColor, layout);
41+
handleStyleDefaults(traceIn, traceOut, coerce, layout);
4342
};

src/traces/contour/style_defaults.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2012-2015, 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 Colorscale = require('../../components/colorscale');
13+
14+
15+
module.exports = function handleStyleDefaults(traceIn, traceOut, coerce, layout) {
16+
var coloring = coerce('contours.coloring');
17+
18+
var showLines;
19+
if(coloring === 'fill') showLines = coerce('contours.showlines');
20+
21+
if(showLines !== false) {
22+
if(coloring !== 'lines') coerce('line.color', '#000');
23+
coerce('line.width', 0.5);
24+
coerce('line.dash');
25+
}
26+
27+
coerce('line.smoothing');
28+
29+
if((traceOut.contours || {}).coloring !== 'none') {
30+
Colorscale.handleDefaults(
31+
traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'}
32+
);
33+
}
34+
};

src/traces/heatmap/xyz_defaults.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright 2012-2015, 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 isNumeric = require('fast-isnumeric');
13+
14+
var hasColumns = require('./has_columns');
15+
16+
17+
module.exports = function handleXYZDefaults(traceIn, traceOut, coerce) {
18+
var z = coerce('z');
19+
var x, y;
20+
21+
if(z===undefined || !z.length) return 0;
22+
23+
if(hasColumns(traceIn)) {
24+
x = coerce('x');
25+
y = coerce('y');
26+
27+
// column z must be accompanied by 'x' and 'y' arrays
28+
if(!x || !y) return 0;
29+
}
30+
else {
31+
x = coordDefaults('x', coerce);
32+
y = coordDefaults('y', coerce);
33+
34+
// TODO put z validation elsewhere
35+
if(!isValidZ(z)) return 0;
36+
37+
coerce('transpose');
38+
}
39+
40+
return traceOut.z.length;
41+
};
42+
43+
function coordDefaults(coordStr, coerce) {
44+
var coord = coerce(coordStr),
45+
coordType = coord ?
46+
coerce(coordStr + 'type', 'array') :
47+
'scaled';
48+
49+
if(coordType === 'scaled') {
50+
coerce(coordStr + '0');
51+
coerce('d' + coordStr);
52+
}
53+
54+
return coord;
55+
}
56+
57+
function isValidZ(z) {
58+
var allRowsAreArrays = true,
59+
oneRowIsFilled = false,
60+
hasOneNumber = false,
61+
zi;
62+
63+
/*
64+
* Without this step:
65+
*
66+
* hasOneNumber = false breaks contour but not heatmap
67+
* allRowsAreArrays = false breaks contour but not heatmap
68+
* oneRowIsFilled = false breaks both
69+
*/
70+
71+
for(var i = 0; i < z.length; i++) {
72+
zi = z[i];
73+
if(!Array.isArray(zi)) {
74+
allRowsAreArrays = false;
75+
break;
76+
}
77+
if(zi.length > 0) oneRowIsFilled = true;
78+
for(var j = 0; j < zi.length; j++) {
79+
if(isNumeric(zi[j])) {
80+
hasOneNumber = true;
81+
break;
82+
}
83+
}
84+
}
85+
86+
return (allRowsAreArrays && oneRowIsFilled && hasOneNumber);
87+
}

0 commit comments

Comments
 (0)