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

Skip to content

Commit 76e9641

Browse files
committed
centralize contour color map function creation:
- contour colorbar and style now call same color map function creator - contourgl will used the same makeColorMap
1 parent e800e2a commit 76e9641

File tree

3 files changed

+122
-78
lines changed

3 files changed

+122
-78
lines changed

src/traces/contour/colorbar.js

Lines 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

Lines 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

Lines 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
};

0 commit comments

Comments
 (0)