Commit 0c54df7f authored by olau@iola.dk's avatar olau@iola.dk

Added support for 12 hour timestamps, patch by Forrest Aldridge


git-svn-id: https://flot.googlecode.com/svn/trunk@205 1e0a6537-2640-0410-bfb7-f154510ff394
parent d4c5f444
...@@ -309,7 +309,6 @@ an example of a custom formatter: ...@@ -309,7 +309,6 @@ an example of a custom formatter:
return val.toFixed(axis.tickDecimals) + " B"; return val.toFixed(axis.tickDecimals) + " B";
} }
Time series data Time series data
================ ================
...@@ -380,6 +379,7 @@ through the following axis options: ...@@ -380,6 +379,7 @@ through the following axis options:
minTickSize: array minTickSize: array
timeformat: null or format string timeformat: null or format string
monthNames: null or array of size 12 of strings monthNames: null or array of size 12 of strings
twelveHourClock: boolean
Here "timeformat" is a format string to use. You might use it like Here "timeformat" is a format string to use. You might use it like
this: this:
...@@ -392,20 +392,25 @@ this: ...@@ -392,20 +392,25 @@ this:
This will result in tick labels like "2000/12/24". The following This will result in tick labels like "2000/12/24". The following
specifiers are supported specifiers are supported
%h': hours %h: hours
%H': hours (left-padded with a zero) %H: hours (left-padded with a zero)
%M': minutes (left-padded with a zero) %M: minutes (left-padded with a zero)
%S': seconds (left-padded with a zero) %S: seconds (left-padded with a zero)
%d': day of month (1-31) %d: day of month (1-31)
%m': month (1-12) %m: month (1-12)
%y': year (four digits) %y: year (four digits)
%b': month name (customizable) %b: month name (customizable)
%p: am/pm, additionally switches %h/%H to 12 hour instead of 24
%P: AM/PM (uppercase version of %p)
You can customize the month names with the "monthNames" option. For You can customize the month names with the "monthNames" option. For
instance, for Danish you might specify: instance, for Danish you might specify:
monthNames: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"] monthNames: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"]
If you set "twelveHourClock" to true, the autogenerated timestamps
will use 12 hour AM/PM timestamps instead of 24 hour.
The format string and month names are used by a very simple built-in The format string and month names are used by a very simple built-in
format function that takes a date object, a format string (and format function that takes a date object, a format string (and
optionally an array of month names) and returns the formatted string. optionally an array of month names) and returns the formatted string.
......
...@@ -114,6 +114,8 @@ Changes: ...@@ -114,6 +114,8 @@ Changes:
- Axis transformation support, useful for non-linear plots, e.g. log - Axis transformation support, useful for non-linear plots, e.g. log
axes and compressed time axes (like omitting weekends). axes and compressed time axes (like omitting weekends).
- Support for twelve-hour date formatting (patch by Forrest Aldridge).
Bug fixes: Bug fixes:
- Fixed two corner-case bugs when drawing filled curves (report and - Fixed two corner-case bugs when drawing filled curves (report and
......
...@@ -43,7 +43,8 @@ ...@@ -43,7 +43,8 @@
tickSize: null, // number or [number, "unit"] tickSize: null, // number or [number, "unit"]
minTickSize: null, // number or [number, "unit"] minTickSize: null, // number or [number, "unit"]
monthNames: null, // list of names of months monthNames: null, // list of names of months
timeformat: null // format string to use timeformat: null, // format string to use
twelveHourClock: false // 12 or 24 time in time mode
}, },
yaxis: { yaxis: {
autoscaleMargin: 0.02 autoscaleMargin: 0.02
...@@ -902,14 +903,15 @@ ...@@ -902,14 +903,15 @@
var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]]; var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
var span = axis.max - axis.min; var span = axis.max - axis.min;
var suffix = (axisOptions.twelveHourClock) ? " %p" : "";
if (t < timeUnitSize.minute) if (t < timeUnitSize.minute)
fmt = "%h:%M:%S"; fmt = "%h:%M:%S" + suffix;
else if (t < timeUnitSize.day) { else if (t < timeUnitSize.day) {
if (span < 2 * timeUnitSize.day) if (span < 2 * timeUnitSize.day)
fmt = "%h:%M"; fmt = "%h:%M" + suffix;
else else
fmt = "%b %d %h:%M"; fmt = "%b %d %h:%M" + suffix;
} }
else if (t < timeUnitSize.month) else if (t < timeUnitSize.month)
fmt = "%b %d"; fmt = "%b %d";
...@@ -2240,21 +2242,33 @@ ...@@ -2240,21 +2242,33 @@
var r = []; var r = [];
var escape = false; var escape = false;
var hours = d.getUTCHours();
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 (fmt.search(/%p|%P/) != -1) {
if (hours > 12) {
hours = hours - 12;
} else if (hours == 0) {
hours = 12;
}
}
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) {
switch (c) { switch (c) {
case 'h': c = "" + d.getUTCHours(); break; case 'h': c = "" + hours; break;
case 'H': c = leftPad(d.getUTCHours()); break; case 'H': c = leftPad(hours); break;
case 'M': c = leftPad(d.getUTCMinutes()); break; case 'M': c = leftPad(d.getUTCMinutes()); break;
case 'S': c = leftPad(d.getUTCSeconds()); break; case 'S': c = leftPad(d.getUTCSeconds()); break;
case 'd': c = "" + d.getUTCDate(); break; case 'd': c = "" + d.getUTCDate(); break;
case 'm': c = "" + (d.getUTCMonth() + 1); break; case 'm': c = "" + (d.getUTCMonth() + 1); break;
case 'y': c = "" + d.getUTCFullYear(); break; case 'y': c = "" + d.getUTCFullYear(); break;
case 'b': c = "" + monthNames[d.getUTCMonth()]; break; case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break;
case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break;
} }
r.push(c); r.push(c);
escape = false; escape = false;
......
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