diff --git a/API.txt b/API.txt index e43d92a..aa5bd12 100644 --- a/API.txt +++ b/API.txt @@ -309,7 +309,6 @@ an example of a custom formatter: return val.toFixed(axis.tickDecimals) + " B"; } - Time series data ================ @@ -380,6 +379,7 @@ through the following axis options: minTickSize: array timeformat: null or format string monthNames: null or array of size 12 of strings + twelveHourClock: boolean Here "timeformat" is a format string to use. You might use it like this: @@ -392,20 +392,25 @@ this: This will result in tick labels like "2000/12/24". The following specifiers are supported - %h': hours - %H': hours (left-padded with a zero) - %M': minutes (left-padded with a zero) - %S': seconds (left-padded with a zero) - %d': day of month (1-31) - %m': month (1-12) - %y': year (four digits) - %b': month name (customizable) + %h: hours + %H: hours (left-padded with a zero) + %M: minutes (left-padded with a zero) + %S: seconds (left-padded with a zero) + %d: day of month (1-31) + %m: month (1-12) + %y: year (four digits) + %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 instance, for Danish you might specify: 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 format function that takes a date object, a format string (and optionally an array of month names) and returns the formatted string. diff --git a/NEWS.txt b/NEWS.txt index 965062d..343af64 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -113,6 +113,8 @@ Changes: - Axis transformation support, useful for non-linear plots, e.g. log axes and compressed time axes (like omitting weekends). + +- Support for twelve-hour date formatting (patch by Forrest Aldridge). Bug fixes: diff --git a/jquery.flot.js b/jquery.flot.js index c44c4cb..eef4760 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -43,7 +43,8 @@ tickSize: null, // number or [number, "unit"] minTickSize: null, // number or [number, "unit"] 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: { autoscaleMargin: 0.02 @@ -902,14 +903,15 @@ var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]]; var span = axis.max - axis.min; + var suffix = (axisOptions.twelveHourClock) ? " %p" : ""; if (t < timeUnitSize.minute) - fmt = "%h:%M:%S"; + fmt = "%h:%M:%S" + suffix; else if (t < timeUnitSize.day) { if (span < 2 * timeUnitSize.day) - fmt = "%h:%M"; + fmt = "%h:%M" + suffix; else - fmt = "%b %d %h:%M"; + fmt = "%b %d %h:%M" + suffix; } else if (t < timeUnitSize.month) fmt = "%b %d"; @@ -2240,21 +2242,33 @@ var r = []; var escape = false; + var hours = d.getUTCHours(); + var isAM = hours < 12; if (monthNames == null) 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) { var c = fmt.charAt(i); if (escape) { switch (c) { - case 'h': c = "" + d.getUTCHours(); break; - case 'H': c = leftPad(d.getUTCHours()); break; + case 'h': c = "" + hours; break; + case 'H': c = leftPad(hours); break; case 'M': c = leftPad(d.getUTCMinutes()); break; case 'S': c = leftPad(d.getUTCSeconds()); break; case 'd': c = "" + d.getUTCDate(); break; case 'm': c = "" + (d.getUTCMonth() + 1); break; case 'y': c = "" + d.getUTCFullYear(); 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); escape = false;