Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4c676df

Browse filesBrowse files
committed
Merge pull request #367 from plotly/contour-cleanup
Contour colormap cleanup
2 parents 6460fb8 + 6611d1e commit 4c676df
Copy full SHA for 4c676df

File tree

Expand file treeCollapse file tree

10 files changed

+246
-88
lines changed
Filter options
Expand file treeCollapse file tree

10 files changed

+246
-88
lines changed

‎.eslintrc

Copy file name to clipboardExpand all lines: .eslintrc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"no-multi-spaces": [2],
3636
"no-whitespace-before-property": [2],
3737
"no-unexpected-multiline": [2],
38+
"no-floating-decimal": [2],
3839
"space-infix-ops": [0, {"int32Hint": false}],
3940
"quotes": [2, "single"],
4041
"dot-notation": [2, {"allowKeywords": false}],

‎src/traces/contour/calc.js

Copy file name to clipboardExpand all lines: src/traces/contour/calc.js
+11-7Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ var Axes = require('../../plots/cartesian/axes');
1313
var heatmapCalc = require('../heatmap/calc');
1414

1515

16+
// most is the same as heatmap calc, then adjust it
17+
// though a few things inside heatmap calc still look for
18+
// contour maps, because the makeBoundArray calls are too entangled
1619
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
2020
var cd = heatmapCalc(gd, trace),
2121
contours = trace.contours;
2222

2323
// check if we need to auto-choose contour levels
24-
if(trace.autocontour!==false) {
24+
if(trace.autocontour !== false) {
2525
var dummyAx = {
2626
type: 'linear',
2727
range: [trace.zmin, trace.zmax]
2828
};
29-
Axes.autoTicks(dummyAx,
30-
(trace.zmax - trace.zmin) / (trace.ncontours||15));
29+
30+
Axes.autoTicks(
31+
dummyAx,
32+
(trace.zmax - trace.zmin) / (trace.ncontours || 15)
33+
);
34+
3135
contours.start = Axes.tickFirst(dummyAx);
3236
contours.size = dummyAx.dtick;
3337
dummyAx.range.reverse();
@@ -37,7 +41,7 @@ module.exports = function calc(gd, trace) {
3741
if(contours.end === trace.zmax) contours.end -= contours.size;
3842

3943
// so rounding errors don't cause us to miss the last contour
40-
contours.end += contours.size/100;
44+
contours.end += contours.size / 100;
4145

4246
// copy auto-contour info back to the source data.
4347
trace._input.contours = contours;

‎src/traces/contour/colorbar.js

Copy file name to clipboardExpand all lines: src/traces/contour/colorbar.js
+11-44Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
'use strict';
1111

12-
var d3 = require('d3');
13-
1412
var Plots = require('../../plots/plots');
15-
var getColorscale = require('../../components/colorscale/get_scale');
1613
var drawColorbar = require('../../components/colorbar/draw');
1714

15+
var makeColorMap = require('./make_color_map');
16+
1817

1918
module.exports = function colorbar(gd, cd) {
2019
var trace = cd[0].trace,
@@ -32,55 +31,23 @@ module.exports = function colorbar(gd, cd) {
3231

3332
var contours = trace.contours,
3433
line = trace.line,
35-
cs = contours.size||1,
36-
nc = Math.floor((contours.end + cs/10 - contours.start)/cs)+1,
37-
scl = getColorscale(trace.colorscale),
38-
extraLevel = contours.coloring==='lines' ? 0 : 1,
39-
colormap = d3.scale.linear().interpolate(d3.interpolateRgb),
40-
colorDomain = scl.map(function(si) {
41-
return (si[0]*(nc+extraLevel-1)-(extraLevel/2)) * cs +
42-
contours.start;
43-
}),
44-
colorRange = scl.map(function(si) { return si[1]; });
34+
cs = contours.size || 1,
35+
coloring = contours.coloring;
36+
37+
var colorMap = makeColorMap(trace, {isColorbar: true});
4538

46-
// colorbar fill and lines
47-
if(contours.coloring==='heatmap') {
48-
if(trace.zauto && trace.autocontour===false) {
49-
trace.zmin = contours.start-cs/2;
50-
trace.zmax = trace.zmin+nc*cs;
51-
}
39+
if(coloring === 'heatmap') {
5240
cb.filllevels({
5341
start: trace.zmin,
5442
end: trace.zmax,
55-
size: (trace.zmax-trace.zmin)/254
43+
size: (trace.zmax - trace.zmin) / 254
5644
});
57-
colorDomain = scl.map(function(si) {
58-
return si[0]*(trace.zmax-trace.zmin) + trace.zmin;
59-
});
60-
61-
// do the contours extend beyond the colorscale?
62-
// if so, extend the colorscale with constants
63-
var zRange = d3.extent([trace.zmin, trace.zmax, contours.start,
64-
contours.start + cs*(nc-1)]),
65-
zmin = zRange[trace.zmin<trace.zmax ? 0 : 1],
66-
zmax = zRange[trace.zmin<trace.zmax ? 1 : 0];
67-
if(zmin!==trace.zmin) {
68-
colorDomain.splice(0, 0, zmin);
69-
colorRange.splice(0, 0, colorRange[0]);
70-
}
71-
if(zmax!==trace.zmax) {
72-
colorDomain.push(zmax);
73-
colorRange.push(colorRange[colorRange.length-1]);
74-
}
7545
}
7646

77-
colormap.domain(colorDomain).range(colorRange);
78-
79-
cb.fillcolor(contours.coloring==='fill' || contours.coloring==='heatmap' ?
80-
colormap : '')
47+
cb.fillcolor((coloring === 'fill' || coloring === 'heatmap') ? colorMap : '')
8148
.line({
82-
color: contours.coloring==='lines' ? colormap : line.color,
83-
width: contours.showlines!==false ? line.width : 0,
49+
color: coloring === 'lines' ? colorMap : line.color,
50+
width: contours.showlines !== false ? line.width : 0,
8451
dash: line.dash
8552
})
8653
.levels({

‎src/traces/contour/make_color_map.js

Copy file name to clipboard
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2012-2016, 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 getColorscale = require('../../components/colorscale/get_scale');
15+
16+
17+
module.exports = function makeColorMap(trace) {
18+
var contours = trace.contours,
19+
start = contours.start,
20+
end = contours.end,
21+
cs = contours.size || 1,
22+
nc = Math.floor((end + cs / 10 - start) / cs) + 1,
23+
extra = contours.coloring === 'lines' ? 0 : 1;
24+
25+
var scl = getColorscale(trace.colorscale),
26+
len = scl.length;
27+
28+
var domain = new Array(len),
29+
range = new Array(len);
30+
31+
var si, i;
32+
33+
if(contours.coloring === 'heatmap') {
34+
if(trace.zauto && trace.autocontour === false) {
35+
trace.zmin = start - cs / 2;
36+
trace.zmax = trace.zmin + nc * cs;
37+
}
38+
39+
for(i = 0; i < len; i++) {
40+
si = scl[i];
41+
42+
domain[i] = si[0] * (trace.zmax - trace.zmin) + trace.zmin;
43+
range[i] = si[1];
44+
}
45+
46+
// do the contours extend beyond the colorscale?
47+
// if so, extend the colorscale with constants
48+
var zRange = d3.extent([trace.zmin, trace.zmax, contours.start,
49+
contours.start + cs * (nc - 1)]),
50+
zmin = zRange[trace.zmin < trace.zmax ? 0 : 1],
51+
zmax = zRange[trace.zmin < trace.zmax ? 1 : 0];
52+
53+
if(zmin !== trace.zmin) {
54+
domain.splice(0, 0, zmin);
55+
range.splice(0, 0, Range[0]);
56+
}
57+
58+
if(zmax !== trace.zmax) {
59+
domain.push(zmax);
60+
range.push(range[range.length - 1]);
61+
}
62+
}
63+
else {
64+
for(i = 0; i < len; i++) {
65+
si = scl[i];
66+
67+
domain[i] = (si[0] * (nc + extra - 1) - (extra / 2)) * cs + start;
68+
range[i] = si[1];
69+
}
70+
}
71+
72+
var colorMap = d3.scale.linear()
73+
.interpolate(d3.interpolateRgb)
74+
.domain(domain)
75+
.range(range);
76+
77+
return colorMap;
78+
};

‎src/traces/contour/style.js

Copy file name to clipboardExpand all lines: src/traces/contour/style.js
+33-34Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,44 @@
1212
var d3 = require('d3');
1313

1414
var Drawing = require('../../components/drawing');
15-
var getColorscale = require('../../components/colorscale/get_scale');
1615
var heatmapStyle = require('../heatmap/style');
1716

17+
var makeColorMap = require('./make_color_map');
18+
1819

1920
module.exports = function style(gd) {
20-
d3.select(gd).selectAll('g.contour')
21-
.style('opacity', function(d) { return d.trace.opacity; })
22-
.each(function(d) {
23-
var c = d3.select(this),
24-
trace = d.trace,
25-
contours = trace.contours,
26-
line = trace.line,
27-
colorLines = contours.coloring==='lines',
28-
cs = contours.size||1,
29-
nc = Math.floor((contours.end + cs/10 - contours.start)/cs) + 1,
30-
scl = getColorscale(trace.colorscale),
31-
extraLevel = colorLines ? 0 : 1,
32-
colormap = d3.scale.linear()
33-
.domain(scl.map(function(si) {
34-
return (si[0]*(nc+extraLevel-1)-(extraLevel/2)) * cs +
35-
contours.start;
36-
}))
37-
.interpolate(d3.interpolateRgb)
38-
.range(scl.map(function(si) { return si[1]; }));
39-
40-
c.selectAll('g.contourlevel').each(function(d, i) {
41-
d3.select(this).selectAll('path')
42-
.call(Drawing.lineGroupStyle,
43-
line.width,
44-
colorLines ? colormap(contours.start+i*cs) : line.color,
45-
line.dash);
46-
});
47-
c.selectAll('g.contourbg path')
48-
.style('fill', colormap(contours.start - cs/2));
49-
c.selectAll('g.contourfill path')
50-
.style('fill',function(d, i) {
51-
return colormap(contours.start + (i+0.5)*cs);
52-
});
21+
var contours = d3.select(gd).selectAll('g.contour');
22+
23+
contours.style('opacity', function(d) {
24+
return d.trace.opacity;
25+
});
26+
27+
contours.each(function(d) {
28+
var c = d3.select(this),
29+
trace = d.trace,
30+
contours = trace.contours,
31+
line = trace.line,
32+
cs = contours.size || 1,
33+
start = contours.start;
34+
35+
var colorMap = makeColorMap(trace);
36+
37+
c.selectAll('g.contourlevel').each(function(d, i) {
38+
d3.select(this).selectAll('path')
39+
.call(Drawing.lineGroupStyle,
40+
line.width,
41+
contours.coloring === 'lines' ? colorMap(start + i * cs) : line.color,
42+
line.dash);
5343
});
5444

45+
c.selectAll('g.contourbg path')
46+
.style('fill', colorMap(start - cs / 2));
47+
48+
c.selectAll('g.contourfill path')
49+
.style('fill', function(d, i) {
50+
return colorMap(start + (i + 0.5) * cs);
51+
});
52+
});
53+
5554
heatmapStyle(gd);
5655
};
Loading
29.1 KB
Loading
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"data":[{
3+
"contours":{"coloring":"heatmap","showlines":false},
4+
"z":[["1",""],["2",""],["3",""],["3",""],["4",""],["5",""],["6",""],["5",""],["2",""],["3",""],["3",""],["5",""],["6",""],["5",""],["4","1"],["4","2"],["2","3"],["1","4"],["3","5"],["2","4"],["1","3"],["3","2"],["5","3"],["4","4"],["3","3"],["2","2"],["1","1"],["2","2"],["3","3"],["4","4"],["5","6"],["4","5"],["3","4"],["2","3"],["3","2"],["2","3"],["3","4"],["3","3"],["3","2"]],
5+
"type":"contour"
6+
}],
7+
"layout":{
8+
"autosize":false,
9+
"height":400,
10+
"width":400
11+
}
12+
}
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"data":[{
3+
"contours":{"coloring":"lines"},
4+
"z":[["1",""],["2",""],["3",""],["3",""],["4",""],["5",""],["6",""],["5",""],["2",""],["3",""],["3",""],["5",""],["6",""],["5",""],["4","1"],["4","2"],["2","3"],["1","4"],["3","5"],["2","4"],["1","3"],["3","2"],["5","3"],["4","4"],["3","3"],["2","2"],["1","1"],["2","2"],["3","3"],["4","4"],["5","6"],["4","5"],["3","4"],["2","3"],["3","2"],["2","3"],["3","4"],["3","3"],["3","2"]],
5+
"type":"contour"
6+
}],
7+
"layout":{
8+
"autosize":false,
9+
"height":400,
10+
"width":400
11+
}
12+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.