labelsKMG2: false,
showLabelsOnHighlight: true,
- yValueFormatter: function(x, opt_numDigits) {
- return x.toPrecision(opt_numDigits || 2);
- },
+ yValueFormatter: function(x) { return Dygraph.round_(x, 2); },
strokeWidth: 1.0,
this.wilsonInterval_ = attrs.wilsonInterval || true;
this.is_initial_draw_ = true;
this.annotations_ = [];
- this.numDigits_ = 2;
// Clear the div. This ensure that, if multiple dygraphs are passed the same
// div, then only one will be drawn.
/**
* Returns the current rolling period, as set by the user or an option.
- * @return {Number} The number of points in the rolling window
+ * @return {Number} The number of days in the rolling window
*/
Dygraph.prototype.rollPeriod = function() {
return this.rollPeriod_;
}
var point = this.selPoints_[i];
var c = new RGBColor(this.plotter_.colors[point.name]);
- var yval = fmtFunc(point.yval, this.numDigits_ + 1); // In tenths.
+ var yval = fmtFunc(point.yval);
replace += " <b><font color='" + c.toHex() + "'>"
+ point.name + "</font></b>:"
+ yval;
};
/**
+ * Round a number to the specified number of digits past the decimal point.
+ * @param {Number} num The number to round
+ * @param {Number} places The number of decimals to which to round
+ * @return {Number} The rounded number
+ * @private
+ */
+Dygraph.round_ = function(num, places) {
+ var shift = Math.pow(10, places);
+ return Math.round(num * shift)/shift;
+};
+
+/**
* Fires when there's data available to be graphed.
* @param {String} data Raw CSV data to be plotted
* @private
endDate = this.rawData_[this.rawData_.length - 1][0];
}
- var ret = this.attr_('xTicker')(startDate, endDate, this);
- if (ret.ticks !== undefined) { // Used numericTicks()?
- this.layout_.updateOptions({xTicks: ret.ticks});
- } else { // Used dateTicker() instead.
- this.layout_.updateOptions({xTicks: ret});
- }
+ var xTicks = this.attr_('xTicker')(startDate, endDate, this);
+ this.layout_.updateOptions({xTicks: xTicks});
};
// Time granularity enumeration
};
/**
- * Determine the number of significant figures in a Number up to the specified
- * precision. Note that there is no way to determine if a trailing '0' is
- * significant or not, so by convention we return 1 for all of the following
- * inputs: 1, 1.0, 1.00, 1.000 etc.
- * @param {Number} x The input value.
- * @param {Number} opt_maxPrecision Optional maximum precision to consider.
- * Default and maximum allowed value is 13.
- * @return {Number} The number of significant figures which is >= 1.
- */
-Dygraph.significantFigures = function(x, opt_maxPrecision) {
- var precision = Math.max(opt_maxPrecision || 13, 13);
-
- // Convert the number to it's exponential notation form and work backwards,
- // ignoring the 'e+xx' bit. This may seem like a hack, but doing a loop and
- // dividing by 10 leads to roundoff errors. By using toExponential(), we let
- // the JavaScript interpreter handle the low level bits of the Number for us.
- var s = x.toExponential(precision);
- var ePos = s.lastIndexOf('e'); // -1 case handled by return below.
-
- for (var i = ePos - 1; i >= 0; i--) {
- if (s[i] == '.') {
- // Got to the decimal place. We'll call this 1 digit of precision because
- // we can't know for sure how many trailing 0s are significant.
- return 1;
- } else if (s[i] != '0') {
- // Found the first non-zero digit. Return the number of characters
- // except for the '.'.
- return i; // This is i - 1 + 1 (-1 is for '.', +1 is for 0 based index).
- }
- }
-
- // Occurs if toExponential() doesn't return a string containing 'e', which
- // should never happen.
- return 1;
-};
-
-/**
* Add ticks when the x axis has numbers on it (instead of dates)
* @param {Number} startDate Start of the date window (millis since epoch)
* @param {Number} endDate End of the date window (millis since epoch)
var ticks = [];
if (vals) {
for (var i = 0; i < vals.length; i++) {
- ticks[i] = {v: vals[i]};
+ ticks.push({v: vals[i]});
}
} else {
// Basic idea:
if (low_val > high_val) scale *= -1;
for (var i = 0; i < nTicks; i++) {
var tickV = low_val + i * scale;
- ticks[i] = {v: tickV};
+ ticks.push( {v: tickV} );
}
}
k = 1024;
k_labels = [ "k", "M", "G", "T" ];
}
- var formatter = attr('yAxisLabelFormatter') ?
- attr('yAxisLabelFormatter') : attr('yValueFormatter');
-
- // Determine the number of decimal places needed for the labels below by
- // taking the maximum number of significant figures for any label. We must
- // take the max because we can't tell if trailing 0s are significant.
- var numDigits = 0;
- for (var i = 0; i < ticks.length; i++) {
- var tickV = ticks[i].v;
- numDigits = Math.max(Dygraph.significantFigures(tickV), numDigits);
- }
+ var formatter = attr('yAxisLabelFormatter') ? attr('yAxisLabelFormatter') : attr('yValueFormatter');
for (var i = 0; i < ticks.length; i++) {
var tickV = ticks[i].v;
var absTickV = Math.abs(tickV);
- var label = (formatter !== undefined) ?
- formatter(tickV, numDigits) : tickV.toPrecision(numDigits);
- if (k_labels.length > 0) {
+ var label;
+ if (formatter != undefined) {
+ label = formatter(tickV);
+ } else {
+ label = Dygraph.round_(tickV, 2);
+ }
+ if (k_labels.length) {
// Round up to an appropriate unit.
var n = k*k*k*k;
for (var j = 3; j >= 0; j--, n /= k) {
if (absTickV >= n) {
- label = (tickV / n).toPrecision(numDigits) + k_labels[j];
+ label = Dygraph.round_(tickV / n, 1) + k_labels[j];
break;
}
}
}
ticks[i].label = label;
}
- return {ticks: ticks, numDigits: numDigits};
+ return ticks;
};
// Computes the range of the data series (including confidence intervals).
this.layout_.addDataset(this.attr_("labels")[i], datasets[i]);
}
- this.computeYAxisRanges_(extremes);
- this.layout_.updateOptions( { yAxes: this.axes_,
- seriesToAxisMap: this.seriesToAxisMap_
+ // TODO(danvk): this method doesn't need to return anything.
+ var out = this.computeYAxisRanges_(extremes);
+ var axes = out[0];
+ var seriesToAxisMap = out[1];
+ this.layout_.updateOptions( { yAxes: axes,
+ seriesToAxisMap: seriesToAxisMap
} );
this.addXTicks_();
// primary axis. However, if an axis is specifically marked as having
// independent ticks, then that is permissible as well.
if (i == 0 || axis.independentTicks) {
- var ret =
+ axis.ticks =
Dygraph.numericTicks(axis.computedValueRange[0],
axis.computedValueRange[1],
this,
axis);
- axis.ticks = ret.ticks;
- this.numDigits_ = ret.numDigits;
} else {
var p_axis = this.axes_[0];
var p_ticks = p_axis.ticks;
tick_values.push(y_val);
}
- var ret =
+ axis.ticks =
Dygraph.numericTicks(axis.computedValueRange[0],
axis.computedValueRange[1],
this, axis, tick_values);
- axis.ticks = ret.ticks;
- this.numDigits_ = ret.numDigits;
}
}
+
+ return [this.axes_, this.seriesToAxisMap_];
};
/**
* Note that this is where fractional input (i.e. '5/10') is converted into
* decimal values.
* @param {Array} originalData The data in the appropriate format (see above)
- * @param {Number} rollPeriod The number of points over which to average the
- * data
+ * @param {Number} rollPeriod The number of days over which to average the data
*/
Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
if (originalData.length < 2)
}
} else {
// Calculate the rolling average for the first rollPeriod - 1 points where
- // there is not enough data to roll over the full number of points
+ // there is not enough data to roll over the full number of days
var num_init_points = Math.min(rollPeriod - 1, originalData.length - 2);
if (!this.attr_("errorBars")){
if (rollPeriod == 1) {
};
/**
- * Adjusts the number of points in the rolling average. Updates the graph to
+ * Adjusts the number of days in the rolling average. Updates the graph to
* reflect the new averaging period.
- * @param {Number} length Number of points over which to average the data.
+ * @param {Number} length Number of days over which to average the data.
*/
Dygraph.prototype.adjustRoll = function(length) {
this.rollPeriod_ = length;
+++ /dev/null
-<html>
- <head>
- <title>significant figures</title>
- <!--[if IE]>
- <script type="text/javascript" src="../excanvas.js"></script>
- <![endif]-->
- <script type="text/javascript" src="../strftime/strftime-min.js"></script>
- <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
- <script type="text/javascript" src="../dygraph-canvas.js"></script>
- <script type="text/javascript" src="../dygraph.js"></script>
- </head>
- <body>
- <p>Tests for various inputs to Dygraph.significantFigures(). All tests
- should have result PASS.</p>
- <div id="tests"></div>
-
- <script type="text/javascript">
- // Helper functions for generating an HTML table for holding the test
- // results.
- createRow = function(columnType, columns) {
- var row = document.createElement('tr');
- for (var i = 0; i < columns.length; i ++) {
- var th = document.createElement(columnType);
- var text = document.createTextNode(columns[i]);
- th.appendChild(text);
- row.appendChild(th);
- };
- return row;
- };
-
- createHeaderRow = function(columns) {
- return createRow('th', columns);
- };
-
- createDataRow = function(columns) {
- return createRow('td', columns);
- };
-
- createTable = function(headerColumns, dataColumnsList) {
- var table = document.createElement('table');
- table.appendChild(createHeaderRow(headerColumns));
- for (var i = 0; i < dataColumnsList.length; i++) {
- table.appendChild(createDataRow(dataColumnsList[i]));
- }
- table.setAttribute('border', '1');
- return table;
- };
-
- // input gives input floating point in string form
- // expected gives number of significant figures
- var testData = [
- {input: '1.0', expected: 1},
- {input: '1.0000', expected: 1},
- {input: '3.14159', expected: 6},
- {input: '3.05', expected: 3},
- {input: '3.0000001', expected: 8},
- {input: '1.999999999999', expected: 13} // = 13 digits.
- ];
-
- var headers = ['Input', 'Output', 'Expected', 'Test Result'];
- var data = [];
-
- for (var i = 0; i < testData.length; i++) {
- var test = testData[i];
- var output = Dygraph.significantFigures(parseFloat(test.input));
- data[i] = [test.input, output, test.expected,
- (output == test.expected ? 'PASS' : 'FAIL')];
- }
-
- var root = document.getElementById('tests');
- root.appendChild(createTable(headers, data));
- </script>
-
- <br>
- <br>
-
- <p>Check for correct number of significant figures with very large and small
- y values. Both plots have the same input x,y values.</p>
-
- <div id="smallvals1" style="width:600px; height:300px;"></div>
- <br>
- <br>
- <div id="smallvals2" style="width:600px; height:300px;"></div>
-
- <script type="text/javascript">
- var data = [
- [new Date("2009/12/01"), 1.02e-7],
- [new Date("2009/12/02"), 1.1e-7],
- [new Date("2009/12/03"), 1.2e-7],
- [new Date("2009/12/04"), 1.522e-7]
- ];
-
- new Dygraph(document.getElementById("smallvals1"), data,
- {
- labels: ["Date","CustomFormatting"],
- yValueFormatter: function(x) { return x.toPrecision(3); }
- });
-
- new Dygraph(document.getElementById("smallvals2"), data,
- {
- labels: ["Date","DefaultFormat"]
- });
- </script>
- </body>
-</html>