Commit 6a02cab9 authored by David Schnur's avatar David Schnur

Cleaned up tabs and whitespace.

parent 0945a1cb
...@@ -4,20 +4,26 @@ Pretty handling of time axes. ...@@ -4,20 +4,26 @@ Pretty handling of time axes.
Set axis.mode to "time" to enable. See the section "Time series data" in API.txt Set axis.mode to "time" to enable. See the section "Time series data" in API.txt
for details. for details.
*/ */
(function ($) { (function ($) {
var options = {}; var options = {};
// round to nearby lower multiple of base // round to nearby lower multiple of base
function floorInBase(n, base) { function floorInBase(n, base) {
return base * Math.floor(n / base); return base * Math.floor(n / base);
} }
// Returns a string with the date d formatted according to fmt. // Returns a string with the date d formatted according to fmt.
// A subset of the Open Group's strftime format is supported. // A subset of the Open Group's strftime format is supported.
function formatDate(d, fmt, monthNames, dayNames) { function formatDate(d, fmt, monthNames, dayNames) {
if (typeof d.strftime == "function") { if (typeof d.strftime == "function") {
return d.strftime(fmt); return d.strftime(fmt);
} }
var leftPad = function(n, pad) { var leftPad = function(n, pad) {
n = "" + n; n = "" + n;
pad = "" + (pad == null ? "0" : pad); pad = "" + (pad == null ? "0" : pad);
...@@ -28,12 +34,14 @@ for details. ...@@ -28,12 +34,14 @@ for details.
var escape = false; var escape = false;
var hours = d.getHours(); var hours = d.getHours();
var isAM = hours < 12; var isAM = hours < 12;
if (monthNames == null) if (monthNames == null)
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
if (dayNames == null) if (dayNames == null)
dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var hours12; var hours12;
if (hours > 12) { if (hours > 12) {
hours12 = hours - 12; hours12 = hours - 12;
} else if (hours == 0) { } else if (hours == 0) {
...@@ -43,6 +51,7 @@ for details. ...@@ -43,6 +51,7 @@ for details.
} }
for (var i = 0; i < fmt.length; ++i) { for (var i = 0; i < fmt.length; ++i) {
var c = fmt.charAt(i); var c = fmt.charAt(i);
if (escape) { if (escape) {
...@@ -65,14 +74,14 @@ for details. ...@@ -65,14 +74,14 @@ for details.
} }
r.push(c); r.push(c);
escape = false; escape = false;
} } else {
else {
if (c == "%") if (c == "%")
escape = true; escape = true;
else else
r.push(c); r.push(c);
} }
} }
return r.join(""); return r.join("");
} }
...@@ -80,31 +89,41 @@ for details. ...@@ -80,31 +89,41 @@ for details.
// zone the client happens to be in we need a date-like object independent // zone the client happens to be in we need a date-like object independent
// of time zones. This is done through a wrapper that only calls the UTC // of time zones. This is done through a wrapper that only calls the UTC
// versions of the accessor methods. // versions of the accessor methods.
function makeUtcWrapper(d) { function makeUtcWrapper(d) {
function addProxyMethod(sourceObj, sourceMethod, targetObj, function addProxyMethod(sourceObj, sourceMethod, targetObj,
targetMethod) { targetMethod) {
sourceObj[sourceMethod] = function() { sourceObj[sourceMethod] = function() {
return targetObj[targetMethod].apply(targetObj, arguments); return targetObj[targetMethod].apply(targetObj, arguments);
}; };
}; };
var utc = { var utc = {
date: d date: d
}; };
// support strftime, if found // support strftime, if found
if (d.strftime != undefined) if (d.strftime != undefined)
addProxyMethod(utc, "strftime", d, "strftime"); addProxyMethod(utc, "strftime", d, "strftime");
addProxyMethod(utc, "getTime", d, "getTime"); addProxyMethod(utc, "getTime", d, "getTime");
addProxyMethod(utc, "setTime", d, "setTime"); addProxyMethod(utc, "setTime", d, "setTime");
var props = [ "Date", "Day", "FullYear", "Hours", "Milliseconds", "Minutes", "Month", "Seconds" ]; var props = [ "Date", "Day", "FullYear", "Hours", "Milliseconds", "Minutes", "Month", "Seconds" ];
for (var p = 0; p < props.length; p++) { for (var p = 0; p < props.length; p++) {
addProxyMethod(utc, "get" + props[p], d, "getUTC" + props[p]); addProxyMethod(utc, "get" + props[p], d, "getUTC" + props[p]);
addProxyMethod(utc, "set" + props[p], d, "setUTC" + props[p]); addProxyMethod(utc, "set" + props[p], d, "setUTC" + props[p]);
} }
return utc; return utc;
}; };
// select time zone strategy. This returns a date-like object tied to the // select time zone strategy. This returns a date-like object tied to the
// desired timezone // desired timezone
function dateGenerator(ts, opts) { function dateGenerator(ts, opts) {
if (opts.timezone == "browser") { if (opts.timezone == "browser") {
return new Date(ts); return new Date(ts);
...@@ -123,6 +142,7 @@ for details. ...@@ -123,6 +142,7 @@ for details.
} }
// map of app. size of time units in milliseconds // map of app. size of time units in milliseconds
var timeUnitSize = { var timeUnitSize = {
"second": 1000, "second": 1000,
"minute": 60 * 1000, "minute": 60 * 1000,
...@@ -134,6 +154,7 @@ for details. ...@@ -134,6 +154,7 @@ for details.
// the allowed tick sizes, after 1 year we use // the allowed tick sizes, after 1 year we use
// an integer algorithm // an integer algorithm
var spec = [ var spec = [
[1, "second"], [2, "second"], [5, "second"], [10, "second"], [1, "second"], [2, "second"], [5, "second"], [10, "second"],
[30, "second"], [30, "second"],
...@@ -150,7 +171,9 @@ for details. ...@@ -150,7 +171,9 @@ for details.
function init(plot) { function init(plot) {
plot.hooks.processDatapoints.push(function (plot, series, datapoints) { plot.hooks.processDatapoints.push(function (plot, series, datapoints) {
$.each(plot.getAxes(), function(axisName, axis) { $.each(plot.getAxes(), function(axisName, axis) {
var opts = axis.options; var opts = axis.options;
if (opts.mode == "time") { if (opts.mode == "time") {
axis.tickGenerator = function(axis) { axis.tickGenerator = function(axis) {
var ticks = [], var ticks = [],
...@@ -169,13 +192,17 @@ for details. ...@@ -169,13 +192,17 @@ for details.
+ spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2 + spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2
&& spec[i][0] * timeUnitSize[spec[i][1]] >= minSize) && spec[i][0] * timeUnitSize[spec[i][1]] >= minSize)
break; break;
var size = spec[i][0]; var size = spec[i][0];
var unit = spec[i][1]; var unit = spec[i][1];
// special-case the possibility of several years // special-case the possibility of several years
if (unit == "year") { if (unit == "year") {
// if given a minTickSize in years, just use it, // if given a minTickSize in years, just use it,
// ensuring that it's an integer // ensuring that it's an integer
if (opts.minTickSize != null && opts.minTickSize[1] == "year") { if (opts.minTickSize != null && opts.minTickSize[1] == "year") {
size = Math.floor(opts.minTickSize[0]); size = Math.floor(opts.minTickSize[0]);
} else { } else {
...@@ -216,7 +243,9 @@ for details. ...@@ -216,7 +243,9 @@ for details.
d.setFullYear(floorInBase(d.getFullYear(), tickSize)); d.setFullYear(floorInBase(d.getFullYear(), tickSize));
// reset smaller components // reset smaller components
d.setMilliseconds(0); d.setMilliseconds(0);
if (step >= timeUnitSize.minute) if (step >= timeUnitSize.minute)
d.setSeconds(0); d.setSeconds(0);
if (step >= timeUnitSize.hour) if (step >= timeUnitSize.hour)
...@@ -228,8 +257,8 @@ for details. ...@@ -228,8 +257,8 @@ for details.
if (step >= timeUnitSize.year) if (step >= timeUnitSize.year)
d.setMonth(0); d.setMonth(0);
var carry = 0, v = Number.NaN, prev; var carry = 0, v = Number.NaN, prev;
do { do {
prev = v; prev = v;
v = d.getTime(); v = d.getTime();
...@@ -273,6 +302,7 @@ for details. ...@@ -273,6 +302,7 @@ for details.
var hourCode = (opts.twelveHourClock) ? "%I" : "%H"; var hourCode = (opts.twelveHourClock) ? "%I" : "%H";
var fmt; var fmt;
if (t < timeUnitSize.minute) if (t < timeUnitSize.minute)
fmt = hourCode + ":%M:%S" + suffix; fmt = hourCode + ":%M:%S" + suffix;
else if (t < timeUnitSize.day) { else if (t < timeUnitSize.day) {
...@@ -293,6 +323,7 @@ for details. ...@@ -293,6 +323,7 @@ for details.
fmt = "%Y"; fmt = "%Y";
var rt = formatDate(d, fmt, opts.monthNames, opts.dayNames); var rt = formatDate(d, fmt, opts.monthNames, opts.dayNames);
return rt; return rt;
}; };
} }
...@@ -306,4 +337,5 @@ for details. ...@@ -306,4 +337,5 @@ for details.
name: 'time', name: 'time',
version: '1.0' version: '1.0'
}); });
})(jQuery); })(jQuery);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment