|
| 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 | +var isNumeric = require('fast-isnumeric'); |
| 14 | + |
| 15 | +var Lib = require('../../lib'); |
| 16 | +var Plots = require('../../plots/plots'); |
| 17 | +var getColorscale = require('../../components/colorscale/get_scale'); |
| 18 | +var drawColorbar = require('../../components/colorbar/draw'); |
| 19 | + |
| 20 | + |
| 21 | +module.exports = function colorbar(gd, cd) { |
| 22 | + var trace = cd[0].trace, |
| 23 | + cbId = 'cb' + trace.uid, |
| 24 | + scl = getColorscale(trace.colorscale), |
| 25 | + zmin = trace.cmin, |
| 26 | + zmax = trace.cmax, |
| 27 | + vals = trace.surfacecolor || trace.z; |
| 28 | + |
| 29 | + if(!isNumeric(zmin)) zmin = Lib.aggNums(Math.min, null, vals); |
| 30 | + if(!isNumeric(zmax)) zmax = Lib.aggNums(Math.max, null, vals); |
| 31 | + |
| 32 | + gd._fullLayout._infolayer.selectAll('.' + cbId).remove(); |
| 33 | + |
| 34 | + if(!trace.showscale) { |
| 35 | + Plots.autoMargin(gd, cbId); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + var cb = cd[0].t.cb = drawColorbar(gd, cbId); |
| 40 | + cb.fillcolor(d3.scale.linear() |
| 41 | + .domain(scl.map(function(v) { return zmin + v[0]*(zmax-zmin); })) |
| 42 | + .range(scl.map(function(v) { return v[1]; }))) |
| 43 | + .filllevels({start: zmin, end: zmax, size: (zmax-zmin)/254}) |
| 44 | + .options(trace.colorbar)(); |
| 45 | + |
| 46 | + Lib.markTime('done colorbar'); |
| 47 | +}; |
0 commit comments