From: Jeremy Brewer Date: Fri, 7 Jan 2011 17:14:30 +0000 (-0500) Subject: Merge branch 'master' of https://github.com/danvk/dygraphs X-Git-Tag: v1.0.0~592 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=15b00ba8914e0da9ad5dca2917d7743004485e73;p=dygraphs.git Merge branch 'master' of https://github.com/danvk/dygraphs Conflicts: dygraph.js tests/significant-figures.html --- 15b00ba8914e0da9ad5dca2917d7743004485e73 diff --cc dygraph.js index 032b1cd,0d21e42..3b9a98d --- a/dygraph.js +++ b/dygraph.js @@@ -73,6 -73,6 +73,46 @@@ Dygraph.toString = function() return this.__repr__(); }; ++/** ++ * Number formatting function which mimicks 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 and ++ * significant figures are not dropped. ++ * ++ * NOTE: JavaScript's native toPrecision() is NOT a drop-in replacement for %g. ++ * It creates strings which are too long for absolute values between 10^-4 and ++ * 10^-6. See tests/number-format.html for examples. ++ * ++ * @param {Number} x The number to format ++ * @param {Number} opt_precision The precision to use, default 2. ++ * @return {String} A string formatted like %g in printf. The max generated ++ * string length should be precision + ++ */ ++Dygraph.defaultFormat = function(x, opt_precision) { ++ // Avoid invalid precision values; [1, 21] is the valid range. ++ var p = Math.min(Math.max(1, opt_precision || 2), 21); ++ ++ // This is deceptively simple. The actual algorithm comes from: ++ // ++ // Max allowed length = p + 4 ++ // where 4 comes from 'e+n' and '.'. ++ // ++ // Length of fixed format = 2 + y + p ++ // where 2 comes from '0.' and y = # of leading zeroes. ++ // ++ // Equating the two and solving for y yields y = 2, or 0.00xxxx which is ++ // 1.0e-3. ++ // ++ // Since the behavior of toPrecision() is identical for larger numbers, we ++ // don't have to worry about the other bound. ++ // ++ // Finally, the argument for toExponential() is the number of trailing digits, ++ // so we take off 1 for the value before the '.'. ++ return (Math.abs(x) < 1.0e-3 && x != 0.0) ? ++ x.toExponential(p - 1) : x.toPrecision(p); ++}; ++ // Various default values Dygraph.DEFAULT_ROLL_PERIOD = 1; Dygraph.DEFAULT_WIDTH = 480; @@@ -96,9 -96,7 +136,7 @@@ Dygraph.DEFAULT_ATTRS = labelsKMG2: false, showLabelsOnHighlight: true, - yValueFormatter: function(x, opt_numDigits) { - return x.toPrecision(Math.min(21, Math.max(1, opt_numDigits || 2))); - }, - yValueFormatter: function(x) { return Dygraph.round_(x, 2); }, ++ yValueFormatter: Dygraph.defaultFormat, strokeWidth: 1.0, @@@ -2352,10 -2305,10 +2405,12 @@@ Dygraph.prototype.computeYAxisRanges_ Dygraph.numericTicks(axis.computedValueRange[0], axis.computedValueRange[1], this, axis, tick_values); + axis.ticks = ret.ticks; + this.numYDigits_ = ret.numDigits; } } + + return [this.axes_, this.seriesToAxisMap_]; }; /** diff --cc tests/dygraph-many-points-benchmark.html index 0000000,0000000..3abdeb1 new file mode 100644 --- /dev/null +++ b/tests/dygraph-many-points-benchmark.html @@@ -1,0 -1,0 +1,55 @@@ ++ ++ ++ Benchmarking for Plots with Many Points ++ ++ ++ ++ ++ ++ ++ ++

Plot which can be easily generated with different numbers of points for ++ benchmarking/profiling and improving performance of dygraphs.

++

Number of points: ++

++

Roll period (in points): ++

++
++
++
++ ++ ++ ++ diff --cc tests/number-format.html index 0000000,0000000..e17a15c new file mode 100644 --- /dev/null +++ b/tests/number-format.html @@@ -1,0 -1,0 +1,87 @@@ ++ ++ ++ Test of number formatting ++ ++ ++ ++ ++ ++ ++ ++

The default formatting mimicks printf with %.pg where p is ++ the precision to use. It turns out that JavaScript's toPrecision() ++ method is almost but not exactly equal to %g; they differ for values ++ with small absolute values (10^-1 to 10^-5 or so), with toPrecision() ++ yielding strings that are longer than they should be (i.e. using fixed ++ point where %g would use exponential).

++ ++

This test is intended to check that our formatting works properly for a ++ variety of precisions.

++ ++

Precision to use (1 to 21): ++

++
++
++
++ ++ ++ ++