Skip to content

Commit e6c393b

Browse files
committed
Merge pull request #401 from plotly/colorscale-bug
Colorscale bugfix
2 parents 1991972 + 1f5e075 commit e6c393b

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/components/colorscale/make_scale_function.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var d3 = require('d3');
1313
var tinycolor = require('tinycolor2');
1414
var isNumeric = require('fast-isnumeric');
1515

16+
var Lib = require('../../lib');
1617
var Color = require('../color');
1718

1819

@@ -34,7 +35,10 @@ module.exports = function makeScaleFunction(scl, cmin, cmax) {
3435
.range(range);
3536

3637
return function(v) {
37-
if(isNumeric(v)) return sclFunc(v);
38+
if(isNumeric(v)) {
39+
var sclVal = Lib.constrain(v, cmin, cmax);
40+
return sclFunc(sclVal);
41+
}
3842
else if(tinycolor(v).isValid()) return v;
3943
else return Color.defaultLine;
4044
};
26.3 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"data": [{
3+
"x": [1,2,3,4,5],
4+
"y": [1,2,3,4],
5+
"marker": {
6+
"color": [1,2,3,4],
7+
"cmin": 2,
8+
"cmax": 3,
9+
"showscale": true
10+
}
11+
}, {
12+
"x": [1,2,3,4,5],
13+
"y": [4,3,2,1],
14+
"marker": {
15+
"color": [1,2,3,4],
16+
"cmin": 1,
17+
"cmax": 4,
18+
"showscale": false
19+
}
20+
}],
21+
"layout": {
22+
"showlegend": false
23+
}
24+
}

test/jasmine/tests/colorscale_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,28 @@ describe('Test colorscale:', function() {
370370
});
371371

372372
});
373+
374+
describe('makeScaleFunction', function() {
375+
var scale = [
376+
[0,'rgb(5,10,172)'],
377+
[0.35,'rgb(106,137,247)'],
378+
[0.5,'rgb(190,190,190)'],
379+
[0.6,'rgb(220,170,132)'],
380+
[0.7,'rgb(230,145,90)'],
381+
[1,'rgb(178,10,28)']
382+
],
383+
scaleFunction = Colorscale.makeScaleFunction(scale, 2, 3);
384+
385+
it('should constrain color array values between cmin and cmax', function() {
386+
var color1 = scaleFunction(1),
387+
color2 = scaleFunction(2),
388+
color3 = scaleFunction(3),
389+
color4 = scaleFunction(4);
390+
391+
expect(color1).toEqual(color2);
392+
expect(color1).toEqual('#050aac');
393+
expect(color3).toEqual(color4);
394+
expect(color4).toEqual('#b20a1c');
395+
});
396+
});
373397
});

0 commit comments

Comments
 (0)