|
| 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 isNumeric = require('fast-isnumeric'); |
| 13 | + |
| 14 | +var Axes = require('../../plots/cartesian/axes'); |
| 15 | +var Lib = require('../../lib'); |
| 16 | + |
| 17 | +var subTypes = require('./subtypes'); |
| 18 | +var calcMarkerColorscale = require('./marker_colorscale_calc'); |
| 19 | + |
| 20 | + |
| 21 | +module.exports = function calc(gd, trace) { |
| 22 | + var xa = Axes.getFromId(gd, trace.xaxis || 'x'), |
| 23 | + ya = Axes.getFromId(gd, trace.yaxis || 'y'); |
| 24 | + Lib.markTime('in Scatter.calc'); |
| 25 | + |
| 26 | + var x = xa.makeCalcdata(trace, 'x'); |
| 27 | + Lib.markTime('finished convert x'); |
| 28 | + |
| 29 | + var y = ya.makeCalcdata(trace, 'y'); |
| 30 | + Lib.markTime('finished convert y'); |
| 31 | + |
| 32 | + var serieslen = Math.min(x.length, y.length), |
| 33 | + marker, |
| 34 | + s, |
| 35 | + i; |
| 36 | + |
| 37 | + // cancel minimum tick spacings (only applies to bars and boxes) |
| 38 | + xa._minDtick = 0; |
| 39 | + ya._minDtick = 0; |
| 40 | + |
| 41 | + if(x.length > serieslen) x.splice(serieslen, x.length - serieslen); |
| 42 | + if(y.length > serieslen) y.splice(serieslen, y.length - serieslen); |
| 43 | + |
| 44 | + // check whether bounds should be tight, padded, extended to zero... |
| 45 | + // most cases both should be padded on both ends, so start with that. |
| 46 | + var xOptions = {padded: true}, |
| 47 | + yOptions = {padded: true}; |
| 48 | + |
| 49 | + if(subTypes.hasMarkers(trace)) { |
| 50 | + |
| 51 | + // Treat size like x or y arrays --- Run d2c |
| 52 | + // this needs to go before ppad computation |
| 53 | + marker = trace.marker; |
| 54 | + s = marker.size; |
| 55 | + |
| 56 | + if (Array.isArray(s)) { |
| 57 | + // I tried auto-type but category and dates dont make much sense. |
| 58 | + var ax = {type: 'linear'}; |
| 59 | + Axes.setConvert(ax); |
| 60 | + s = ax.makeCalcdata(trace.marker, 'size'); |
| 61 | + if(s.length > serieslen) s.splice(serieslen, s.length - serieslen); |
| 62 | + } |
| 63 | + |
| 64 | + var sizeref = 1.6 * (trace.marker.sizeref || 1), |
| 65 | + markerTrans; |
| 66 | + if(trace.marker.sizemode === 'area') { |
| 67 | + markerTrans = function(v) { |
| 68 | + return Math.max(Math.sqrt((v || 0) / sizeref), 3); |
| 69 | + }; |
| 70 | + } |
| 71 | + else { |
| 72 | + markerTrans = function(v) { |
| 73 | + return Math.max((v || 0) / sizeref, 3); |
| 74 | + }; |
| 75 | + } |
| 76 | + xOptions.ppad = yOptions.ppad = Array.isArray(s) ? |
| 77 | + s.map(markerTrans) : markerTrans(s); |
| 78 | + } |
| 79 | + |
| 80 | + calcMarkerColorscale(trace); |
| 81 | + |
| 82 | + // TODO: text size |
| 83 | + |
| 84 | + // include zero (tight) and extremes (padded) if fill to zero |
| 85 | + // (unless the shape is closed, then it's just filling the shape regardless) |
| 86 | + if(((trace.fill === 'tozerox') || |
| 87 | + ((trace.fill === 'tonextx') && gd.firstscatter)) && |
| 88 | + ((x[0] !== x[serieslen - 1]) || (y[0] !== y[serieslen - 1]))) { |
| 89 | + xOptions.tozero = true; |
| 90 | + } |
| 91 | + |
| 92 | + // if no error bars, markers or text, or fill to y=0 remove x padding |
| 93 | + else if(!trace.error_y.visible && ( |
| 94 | + ['tonexty', 'tozeroy'].indexOf(trace.fill) !== -1 || |
| 95 | + (!subTypes.hasMarkers(trace) && !subTypes.hasText(trace)) |
| 96 | + )) { |
| 97 | + xOptions.padded = false; |
| 98 | + xOptions.ppad = 0; |
| 99 | + } |
| 100 | + |
| 101 | + // now check for y - rather different logic, though still mostly padded both ends |
| 102 | + // include zero (tight) and extremes (padded) if fill to zero |
| 103 | + // (unless the shape is closed, then it's just filling the shape regardless) |
| 104 | + if(((trace.fill === 'tozeroy') || ((trace.fill === 'tonexty') && gd.firstscatter)) && |
| 105 | + ((x[0] !== x[serieslen - 1]) || (y[0] !== y[serieslen - 1]))) { |
| 106 | + yOptions.tozero = true; |
| 107 | + } |
| 108 | + |
| 109 | + // tight y: any x fill |
| 110 | + else if(['tonextx', 'tozerox'].indexOf(trace.fill) !== -1) { |
| 111 | + yOptions.padded = false; |
| 112 | + } |
| 113 | + |
| 114 | + Lib.markTime('ready for Axes.expand'); |
| 115 | + Axes.expand(xa, x, xOptions); |
| 116 | + Lib.markTime('done expand x'); |
| 117 | + Axes.expand(ya, y, yOptions); |
| 118 | + Lib.markTime('done expand y'); |
| 119 | + |
| 120 | + // create the "calculated data" to plot |
| 121 | + var cd = new Array(serieslen); |
| 122 | + for(i = 0; i < serieslen; i++) { |
| 123 | + cd[i] = (isNumeric(x[i]) && isNumeric(y[i])) ? |
| 124 | + {x: x[i], y: y[i]} : {x: false, y: false}; |
| 125 | + } |
| 126 | + |
| 127 | + // this has migrated up from arraysToCalcdata as we have a reference to 's' here |
| 128 | + if (typeof s !== undefined) Lib.mergeArray(s, cd, 'ms'); |
| 129 | + |
| 130 | + gd.firstscatter = false; |
| 131 | + return cd; |
| 132 | +}; |
0 commit comments