-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor colorbars for simpler connection to trace attributes
Also fixes and tests a few traces that should have supported colorbars but didn't and some cases of editing colorbars that didn't previously work
- Loading branch information
1 parent
6dd68bd
commit fa86147
Showing
36 changed files
with
428 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright 2012-2018, 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 isNumeric = require('fast-isnumeric'); | ||
|
||
var aggNums = require('../../lib').aggNums; | ||
var Colorscale = require('../colorscale'); | ||
var drawColorbar = require('./draw'); | ||
|
||
/** | ||
* connectColorbar: create a colorbar from a trace, using its module to | ||
* describe the connection. | ||
* | ||
* @param {DOM element} gd | ||
* | ||
* @param {Array} cd | ||
* calcdata entry for this trace. cd[0].trace is the trace itself, and the | ||
* colorbar object will be stashed in cd[0].t.cb | ||
* | ||
* @param {object|function} moduleOpts | ||
* may be a function(gd, cd) to override the standard handling below. If | ||
* an object, should have these keys: | ||
* @param {Optional(string)} moduleOpts.container | ||
* name of the container inside the trace where the colorbar and colorscale | ||
* attributes live (ie 'marker', 'line') - omit if they're at the trace root. | ||
* @param {string} moduleOpts.min | ||
* name of the attribute holding the value of the minimum color | ||
* @param {string} moduleOpts.max | ||
* name of the attribute holding the value of the maximum color | ||
* @param {Optional(string)} moduleOpts.vals | ||
* name of the attribute holding the (numeric) color data | ||
* used only if min/max fail. May be omitted if these are always | ||
* pre-calculated. | ||
*/ | ||
module.exports = function connectColorbar(gd, cd, moduleOpts) { | ||
if(typeof moduleOpts === 'function') return moduleOpts(gd, cd); | ||
|
||
var trace = cd[0].trace; | ||
var cbId = 'cb' + trace.uid; | ||
var containerName = moduleOpts.container; | ||
var container = containerName ? trace[containerName] : trace; | ||
|
||
gd._fullLayout._infolayer.selectAll('.' + cbId).remove(); | ||
if(!container || !container.showscale) return; | ||
|
||
var zmin = container[moduleOpts.min]; | ||
var zmax = container[moduleOpts.max]; | ||
|
||
var valAttr = moduleOpts.vals; | ||
var vals; | ||
if(Array.isArray(valAttr)) { | ||
for(var i = 0; i < valAttr.length; i++) { | ||
vals = container[valAttr[i]]; | ||
if(vals) break; | ||
} | ||
} | ||
else vals = container[valAttr]; | ||
|
||
if(vals) { | ||
if(!isNumeric(zmin)) zmin = aggNums(Math.min, null, vals); | ||
if(!isNumeric(zmax)) zmax = aggNums(Math.max, null, vals); | ||
} | ||
|
||
var cb = cd[0].t.cb = drawColorbar(gd, cbId); | ||
var sclFunc = Colorscale.makeColorScaleFunc( | ||
Colorscale.extractScale( | ||
container.colorscale, | ||
zmin, | ||
zmax | ||
), | ||
{ noNumericCheck: true } | ||
); | ||
|
||
cb.fillcolor(sclFunc) | ||
.filllevels({start: zmin, end: zmax, size: (zmax - zmin) / 254}) | ||
.options(container.colorbar)(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.