X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=src%2Fdygraph-utils.js;h=c0b8f34b60b5f3f80494e944c265825da9c37f3e;hb=6611837c5490f2f4b6d61967aa1ad658ed5f11d4;hp=d16da53aba2ee552c4941dc188ee7d1db7482373;hpb=6ecc073934b76e5076f917112a24ff7094857730;p=dygraphs.git diff --git a/src/dygraph-utils.js b/src/dygraph-utils.js index d16da53..c0b8f34 100644 --- a/src/dygraph-utils.js +++ b/src/dygraph-utils.js @@ -28,6 +28,39 @@ export var log10 = function(x) { return Math.log(x) / LN_TEN; }; +/** + * @private + * @param {number} r0 + * @param {number} r1 + * @param {number} pct + * @return {number} + */ +export var logRangeFraction = function(r0, r1, pct) { + // Computing the inverse of toPercentXCoord. The function was arrived at with + // the following steps: + // + // Original calcuation: + // pct = (log(x) - log(xRange[0])) / (log(xRange[1]) - log(xRange[0]))); + // + // Multiply both sides by the right-side denominator. + // pct * (log(xRange[1] - log(xRange[0]))) = log(x) - log(xRange[0]) + // + // add log(xRange[0]) to both sides + // log(xRange[0]) + (pct * (log(xRange[1]) - log(xRange[0])) = log(x); + // + // Swap both sides of the equation, + // log(x) = log(xRange[0]) + (pct * (log(xRange[1]) - log(xRange[0])) + // + // Use both sides as the exponent in 10^exp and we're done. + // x = 10 ^ (log(xRange[0]) + (pct * (log(xRange[1]) - log(xRange[0]))) + + var logr0 = log10(r0); + var logr1 = log10(r1); + var exponent = logr0 + (pct * (logr1 - logr0)); + var value = Math.pow(LOG_SCALE, exponent); + return value; +}; + /** A dotted line stroke pattern. */ export var DOTTED_LINE = [2, 2]; /** A dashed line stroke pattern. */ @@ -232,7 +265,7 @@ export function isValidPoint(p, opt_allowNaNY) { }; /** - * Number formatting function which mimicks the behavior of %g in printf, i.e. + * Number formatting function which mimics the behavior of %g in printf, i.e. * either exponential or fixed format (without trailing 0s) is used depending on * the length of the generated string. The advantage of this format is that * there is a predictable upper bound on the resulting string length, @@ -329,11 +362,14 @@ export var DateAccessorsUTC = { * @return {string} A time of the form "HH:MM" or "HH:MM:SS" * @private */ -export function hmsString_(hh, mm, ss) { - var zeropad = Dygraph.zeropad; +export function hmsString_(hh, mm, ss, ms) { var ret = zeropad(hh) + ":" + zeropad(mm); if (ss) { ret += ":" + zeropad(ss); + if (ms) { + var str = "" + ms; + ret += "." + ('000'+str).substring(str.length); + } } return ret; }; @@ -341,7 +377,7 @@ export function hmsString_(hh, mm, ss) { /** * Convert a JS date (millis since epoch) to a formatted string. * @param {number} time The JavaScript time value (ms since epoch) - * @param {boolean} utc Wether output UTC or local time + * @param {boolean} utc Whether output UTC or local time * @return {string} A date of one of these forms: * "YYYY/MM/DD", "YYYY/MM/DD HH:MM" or "YYYY/MM/DD HH:MM:SS" * @private @@ -355,16 +391,17 @@ export function dateString_(time, utc) { var hh = accessors.getHours(date); var mm = accessors.getMinutes(date); var ss = accessors.getSeconds(date); + var ms = accessors.getMilliseconds(date); // Get a year string: var year = "" + y; // Get a 0 padded month string var month = zeropad(m + 1); //months are 0-offset, sigh // Get a 0 padded day string var day = zeropad(d); - var frac = hh * 3600 + mm * 60 + ss; + var frac = hh * 3600 + mm * 60 + ss + 1e-3 * ms; var ret = year + "/" + month + "/" + day; if (frac) { - ret += " " + hmsString_(hh, mm, ss); + ret += " " + hmsString_(hh, mm, ss, ms); } return ret; }; @@ -599,7 +636,7 @@ export function clone(o) { var r = []; for (var i = 0; i < o.length; i++) { if (isArrayLike(o[i])) { - r.push(Dygraph.clone(o[i])); + r.push(clone(o[i])); } else { r.push(o[i]); } @@ -649,17 +686,6 @@ export function getContextPixelRatio(context) { }; /** - * Checks whether the user is on an Android browser. - * Android does not fully support the tag, e.g. w/r/t/ clipping. - * @return {boolean} - * @private - */ -export function isAndroid() { - return (/Android/).test(navigator.userAgent); -}; - - -/** * TODO(danvk): use @template here when it's better supported for classes. * @param {!Array} array * @param {number} start @@ -768,7 +794,7 @@ export function repeatAndCleanup(repeatFn, maxFrames, framePeriodInMillis, (function loop() { if (frameNumber >= maxFrames) return; - Dygraph.requestAnimFrame.call(window, function() { + requestAnimFrame.call(window, function() { // Determine which frame to draw based on the delay so far. Will skip // frames if necessary. var currentTime = new Date().getTime(); @@ -800,7 +826,6 @@ var pixelSafeOptions = { 'annotationDblClickHandler': true, 'annotationMouseOutHandler': true, 'annotationMouseOverHandler': true, - 'axisLabelColor': true, 'axisLineColor': true, 'axisLineWidth': true, 'clickCallback': true, @@ -816,10 +841,7 @@ var pixelSafeOptions = { 'highlightCallback': true, 'highlightCircleSize': true, 'interactionModel': true, - 'isZoomedIgnoreProgrammaticZoom': true, 'labelsDiv': true, - 'labelsDivStyles': true, - 'labelsDivWidth': true, 'labelsKMB': true, 'labelsKMG2': true, 'labelsSeparateLines': true, @@ -919,72 +941,6 @@ export var Circles = { }; /** - * To create a "drag" interaction, you typically register a mousedown event - * handler on the element where the drag begins. In that handler, you register a - * mouseup handler on the window to determine when the mouse is released, - * wherever that release happens. This works well, except when the user releases - * the mouse over an off-domain iframe. In that case, the mouseup event is - * handled by the iframe and never bubbles up to the window handler. - * - * To deal with this issue, we cover iframes with high z-index divs to make sure - * they don't capture mouseup. - * - * Usage: - * element.addEventListener('mousedown', function() { - * var tarper = new utils.IFrameTarp(); - * tarper.cover(); - * var mouseUpHandler = function() { - * ... - * window.removeEventListener(mouseUpHandler); - * tarper.uncover(); - * }; - * window.addEventListener('mouseup', mouseUpHandler); - * }; - * - * @constructor - */ -export function IFrameTarp() { - /** @type {Array.} */ - this.tarps = []; -}; - -/** - * Find all the iframes in the document and cover them with high z-index - * transparent divs. - */ -IFrameTarp.prototype.cover = function() { - var iframes = document.getElementsByTagName("iframe"); - for (var i = 0; i < iframes.length; i++) { - var iframe = iframes[i]; - var pos = Dygraph.findPos(iframe), - x = pos.x, - y = pos.y, - width = iframe.offsetWidth, - height = iframe.offsetHeight; - - var div = document.createElement("div"); - div.style.position = "absolute"; - div.style.left = x + 'px'; - div.style.top = y + 'px'; - div.style.width = width + 'px'; - div.style.height = height + 'px'; - div.style.zIndex = 999; - document.body.appendChild(div); - this.tarps.push(div); - } -}; - -/** - * Remove all the iframe covers. You should call this in a mouseup handler. - */ -IFrameTarp.prototype.uncover = function() { - for (var i = 0; i < this.tarps.length; i++) { - this.tarps[i].parentNode.removeChild(this.tarps[i]); - } - this.tarps = []; -}; - -/** * Determine whether |data| is delimited by CR, CRLF, LF, LFCR. * @param {string} data * @return {?string} the delimiter that was detected (or null on failure). @@ -1029,7 +985,6 @@ export function isNodeContainedBy(containee, container) { return (containeeNode === container); }; - // This masks some numeric issues in older versions of Firefox, // where 1.0/Math.pow(10,2) != Math.pow(10,-2). /** @type {function(number,number):number} */ @@ -1043,7 +998,7 @@ export function pow(base, exp) { var RGBA_RE = /^rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})(?:,\s*([01](?:\.\d+)?))?\)$/; /** - * Helper for Dygraph.toRGB_ which parses strings of the form: + * Helper for toRGB_ which parses strings of the form: * rgb(123, 45, 67) * rgba(123, 45, 67, 0.5) * @return parsed {r,g,b,a?} tuple or null. @@ -1248,7 +1203,7 @@ export function dateAxisLabelFormatter(date, granularity, opts) { hours = accessors.getHours(date), mins = accessors.getMinutes(date), secs = accessors.getSeconds(date), - millis = accessors.getSeconds(date); + millis = accessors.getMilliseconds(date); if (granularity >= DygraphTickers.Granularity.DECADAL) { return '' + year; @@ -1259,8 +1214,14 @@ export function dateAxisLabelFormatter(date, granularity, opts) { if (frac === 0 || granularity >= DygraphTickers.Granularity.DAILY) { // e.g. '21 Jan' (%d%b) return zeropad(day) + ' ' + SHORT_MONTH_NAMES_[month]; + } else if (granularity < DygraphTickers.Granularity.SECONDLY) { + // e.g. 40.310 (meaning 40 seconds and 310 milliseconds) + var str = "" + millis; + return zeropad(secs) + "." + ('000'+str).substring(str.length); + } else if (granularity > DygraphTickers.Granularity.MINUTELY) { + return hmsString_(hours, mins, secs, 0); } else { - return hmsString_(hours, mins, secs); + return hmsString_(hours, mins, secs, millis); } } };