Skip to content

Commit 7f1801e

Browse files
committed
split up contour index
1 parent 503fd93 commit 7f1801e

File tree

7 files changed

+967
-875
lines changed

7 files changed

+967
-875
lines changed

src/traces/contour/calc.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Plotly = require('../../plotly');
13+
var heatmapCalc = require('../heatmap/calc');
14+
15+
16+
module.exports = function calc(gd, trace) {
17+
// most is the same as heatmap calc, then adjust it
18+
// though a few things inside heatmap calc still look for
19+
// contour maps, because the makeBoundArray calls are too entangled
20+
var cd = heatmapCalc(gd, trace),
21+
contours = trace.contours;
22+
23+
// check if we need to auto-choose contour levels
24+
if(trace.autocontour!==false) {
25+
var dummyAx = {
26+
type: 'linear',
27+
range: [trace.zmin, trace.zmax]
28+
};
29+
Plotly.Axes.autoTicks(dummyAx,
30+
(trace.zmax - trace.zmin) / (trace.ncontours||15));
31+
contours.start = Plotly.Axes.tickFirst(dummyAx);
32+
contours.size = dummyAx.dtick;
33+
dummyAx.range.reverse();
34+
contours.end = Plotly.Axes.tickFirst(dummyAx);
35+
36+
if(contours.start===trace.zmin) contours.start += contours.size;
37+
if(contours.end===trace.zmax) contours.end -= contours.size;
38+
39+
// so rounding errors don't cause us to miss the last contour
40+
contours.end += contours.size/100;
41+
42+
// copy auto-contour info back to the source data.
43+
trace._input.contours = contours;
44+
}
45+
46+
return cd;
47+
};

src/traces/contour/colorbar.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 d3 = require('d3');
13+
14+
var Plotly = require('../../plotly');
15+
16+
17+
module.exports = function colorbar(gd, cd) {
18+
var trace = cd[0].trace,
19+
cbId = 'cb'+trace.uid;
20+
21+
gd._fullLayout._infolayer.selectAll('.'+cbId).remove();
22+
23+
if(trace.showscale===false){
24+
Plotly.Plots.autoMargin(gd, cbId);
25+
return;
26+
}
27+
28+
// instantiate the colorbar (will be drawn and styled in contour.style)
29+
var cb = Plotly.Colorbar(gd, cbId);
30+
cd[0].t.cb = cb;
31+
32+
var contours = trace.contours,
33+
line = trace.line,
34+
cs = contours.size||1,
35+
nc = Math.floor((contours.end + cs/10 - contours.start)/cs)+1,
36+
scl = Plotly.Colorscale.getScale(trace.colorscale),
37+
extraLevel = contours.coloring==='lines' ? 0 : 1,
38+
colormap = d3.scale.linear().interpolate(d3.interpolateRgb),
39+
colorDomain = scl.map(function(si){
40+
return (si[0]*(nc+extraLevel-1)-(extraLevel/2)) * cs +
41+
contours.start;
42+
}),
43+
colorRange = scl.map(function(si){ return si[1]; });
44+
45+
// colorbar fill and lines
46+
if(contours.coloring==='heatmap') {
47+
if(trace.zauto && trace.autocontour===false) {
48+
trace.zmin = contours.start-cs/2;
49+
trace.zmax = trace.zmin+nc*cs;
50+
}
51+
cb.filllevels({
52+
start: trace.zmin,
53+
end: trace.zmax,
54+
size: (trace.zmax-trace.zmin)/254
55+
});
56+
colorDomain = scl.map(function(si){
57+
return si[0]*(trace.zmax-trace.zmin) + trace.zmin;
58+
});
59+
60+
// do the contours extend beyond the colorscale?
61+
// if so, extend the colorscale with constants
62+
var zRange = d3.extent([trace.zmin, trace.zmax, contours.start,
63+
contours.start + cs*(nc-1)]),
64+
zmin = zRange[trace.zmin<trace.zmax ? 0 : 1],
65+
zmax = zRange[trace.zmin<trace.zmax ? 1 : 0];
66+
if(zmin!==trace.zmin) {
67+
colorDomain.splice(0, 0, zmin);
68+
colorRange.splice(0, 0, colorRange[0]);
69+
}
70+
if(zmax!==trace.zmax) {
71+
colorDomain.push(zmax);
72+
colorRange.push(colorRange[colorRange.length-1]);
73+
}
74+
}
75+
76+
colormap.domain(colorDomain).range(colorRange);
77+
78+
cb.fillcolor(contours.coloring==='fill' || contours.coloring==='heatmap' ?
79+
colormap : '')
80+
.line({
81+
color: contours.coloring==='lines' ? colormap : line.color,
82+
width: contours.showlines!==false ? line.width : 0,
83+
dash: line.dash
84+
})
85+
.levels({
86+
start: contours.start,
87+
end: contours.end,
88+
size: cs
89+
})
90+
.options(trace.colorbar)();
91+
};

src/traces/contour/defaults.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Lib = require('../../lib');
13+
var heatmapSupplyDefaults = require('../heatmap/defaults');
14+
15+
var attributes = require('./attributes');
16+
17+
18+
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
19+
function coerce(attr, dflt) {
20+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
21+
}
22+
23+
var contourStart = Lib.coerce2(traceIn, traceOut, attributes, 'contours.start'),
24+
contourEnd = Lib.coerce2(traceIn, traceOut, attributes, 'contours.end'),
25+
autocontour = coerce('autocontour', !(contourStart && contourEnd));
26+
27+
if(autocontour) coerce('ncontours');
28+
else coerce('contours.size');
29+
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);
43+
};

src/traces/contour/hover.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 heatmapHoverPoints = require('../heatmap/hover');
13+
14+
15+
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
16+
return heatmapHoverPoints(pointData, xval, yval, hovermode, true);
17+
};

0 commit comments

Comments
 (0)