-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Centralise color scale function creation #1090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
982176a
88f3d9a
c3577f9
8b1f52d
90df2af
5bee22f
654f5cd
cb86fac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright 2012-2016, Plotly, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
|
||
'use strict'; | ||
|
||
/** | ||
* Extract colorscale into numeric domain and color range. | ||
* | ||
* @param {array} scl colorscale array of arrays | ||
* @param {number} cmin minimum color value (used to clamp scale) | ||
* @param {number} cmax maximum color value (used to clamp scale) | ||
*/ | ||
module.exports = function extractScale(scl, cmin, cmax) { | ||
var N = scl.length, | ||
domain = new Array(N), | ||
range = new Array(N); | ||
|
||
for(var i = 0; i < N; i++) { | ||
var si = scl[i]; | ||
|
||
domain[i] = cmin + si[0] * (cmax - cmin); | ||
range[i] = si[1]; | ||
} | ||
|
||
return { | ||
domain: domain, | ||
range: range | ||
}; | ||
}; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2012-2016, Plotly, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
|
||
'use strict'; | ||
|
||
var d3 = require('d3'); | ||
var tinycolor = require('tinycolor2'); | ||
var isNumeric = require('fast-isnumeric'); | ||
|
||
var Color = require('../color'); | ||
|
||
/** | ||
* General colorscale function generator. | ||
* | ||
* @param {object} specs output of Colorscale.extractScale or precomputed domain, range. | ||
* - domain {array} | ||
* - range {array} | ||
* | ||
* @param {object} opts | ||
* - noNumericCheck {boolean} if true, scale func bypasses numeric checks | ||
* - returnArray {boolean} if true, scale func return 4-item array instead of color strings | ||
* | ||
* @return {function} | ||
*/ | ||
module.exports = function makeColorScaleFunc(specs, opts) { | ||
opts = opts || {}; | ||
|
||
var domain = specs.domain, | ||
range = specs.range, | ||
N = range.length, | ||
_range = new Array(N); | ||
|
||
for(var i = 0; i < N; i++) { | ||
var rgba = tinycolor(range[i]).toRgb(); | ||
_range[i] = [rgba.r, rgba.g, rgba.b, rgba.a]; | ||
} | ||
|
||
var _sclFunc = d3.scale.linear() | ||
.domain(domain) | ||
.range(_range) | ||
.clamp(true); | ||
|
||
var noNumericCheck = opts.noNumericCheck, | ||
returnArray = opts.returnArray, | ||
sclFunc; | ||
|
||
if(noNumericCheck && returnArray) { | ||
sclFunc = _sclFunc; | ||
} | ||
else if(noNumericCheck) { | ||
sclFunc = function(v) { | ||
return colorArray2rbga(_sclFunc(v)); | ||
}; | ||
} | ||
else if(returnArray) { | ||
sclFunc = function(v) { | ||
if(isNumeric(v)) return _sclFunc(v); | ||
else if(tinycolor(v).isValid()) return v; | ||
else return Color.defaultLine; | ||
}; | ||
} | ||
else { | ||
sclFunc = function(v) { | ||
if(isNumeric(v)) return colorArray2rbga(_sclFunc(v)); | ||
else if(tinycolor(v).isValid()) return v; | ||
else return Color.defaultLine; | ||
}; | ||
} | ||
|
||
// colorbar draw looks into the d3 scale closure for domain and range | ||
|
||
sclFunc.domain = _sclFunc.domain; | ||
|
||
sclFunc.range = function() { return range; }; | ||
|
||
return sclFunc; | ||
}; | ||
|
||
function colorArray2rbga(colorArray) { | ||
var colorObj = { | ||
r: colorArray[0], | ||
g: colorArray[1], | ||
b: colorArray[2], | ||
a: colorArray[3] | ||
}; | ||
|
||
return tinycolor(colorObj).toRgbString(); | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,8 @@ var Axes = require('../../plots/cartesian/axes'); | |
var Fx = require('../../plots/cartesian/graph_interact'); | ||
var Color = require('../../components/color'); | ||
var Drawing = require('../../components/drawing'); | ||
var Colorscale = require('../../components/colorscale'); | ||
|
||
var getColorscale = require('../../components/colorscale/get_scale'); | ||
var makeScaleFunction = require('../../components/colorscale/make_scale_function'); | ||
var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures; | ||
var locationToFeature = require('../../lib/geo_location_utils').locationToFeature; | ||
var arrayToCalcItem = require('../../lib/array_to_calc_item'); | ||
|
@@ -151,11 +150,15 @@ plotChoropleth.style = function(geo) { | |
var trace = calcTrace[0].trace, | ||
s = d3.select(this), | ||
marker = trace.marker || {}, | ||
markerLine = marker.line || {}, | ||
zmin = trace.zmin, | ||
zmax = trace.zmax, | ||
scl = getColorscale(trace.colorscale), | ||
sclFunc = makeScaleFunction(scl, zmin, zmax); | ||
markerLine = marker.line || {}; | ||
|
||
var sclFunc = Colorscale.makeColorScaleFunc( | ||
Colorscale.extractScale( | ||
trace.colorscale, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good catch, we've already done this at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, so many |
||
trace.zmin, | ||
trace.zmax | ||
) | ||
); | ||
|
||
s.selectAll('path.choroplethlocation') | ||
.each(function(pt) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK sure - we could imagine having this one also include
opts
and call the other one, since I can't see us ever using this without wrapping it inmakeColorScaleFunc
... but then naming gets confusing. meh, lets leave it as you have it here.