Commit d0c96fc0 authored by olau@iola.dk's avatar olau@iola.dk

Added access to the built-in date formatter through $.plot.formatDate

git-svn-id: https://flot.googlecode.com/svn/trunk@117 1e0a6537-2640-0410-bfb7-f154510ff394
parent 9b08e820
......@@ -347,7 +347,7 @@ Tick generation and formatting can also be controlled separately
through the following axis options:
xaxis, yaxis: {
minTickSize
minTickSize: array
timeformat: null or format string
monthNames: null or array of size 12 of strings
}
......@@ -356,7 +356,7 @@ Here "timeformat" is a format string to use. You might use it like
this:
xaxis: {
mode: "time",
mode: "time"
timeformat: "%y/%m/%d"
}
......@@ -377,6 +377,13 @@ instance, for Danish you might specify:
monthName: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"]
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.
If needed, you can access it as $.plot.formatDate(date, formatstring,
monthNames) or even replace it with another more advanced function
from a date library if you're feeling adventurous.
If everything else fails, you can control the formatting by specifying
a custom tick formatter function as usual. Here's a simple example
which will format December 24 as 24/12:
......
......@@ -24,6 +24,10 @@ Changes:
10 times per second, you'll have to put in a setTimeout yourself if
you're doing something really expensive on this event.
- The built-in date formatter can now be accessed as
$.plot.formatDate(...) (suggestion by Matt Manela) and even
replaced.
Bug fixes:
- Fixed two corner-case bugs when drawing filled curves (report and
......
......@@ -461,44 +461,6 @@
if (axisOptions.mode == "time") {
// pretty handling of time
function formatDate(d, fmt, monthNames) {
var leftPad = function(n) {
n = "" + n;
return n.length == 1 ? "0" + n : n;
};
var r = [];
var escape = false;
if (monthNames == null)
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
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 '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;
}
r.push(c);
escape = false;
}
else {
if (c == "%")
escape = true;
else
r.push(c);
}
}
return r.join("");
}
// map of app. size of time units in milliseconds
var timeUnitSize = {
"second": 1000,
......@@ -630,7 +592,7 @@
// first check global format
if (axisOptions.timeformat != null)
return formatDate(d, axisOptions.timeformat, axisOptions.monthNames);
return $.plot.formatDate(d, axisOptions.timeformat, axisOptions.monthNames);
var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
var span = axis.max - axis.min;
......@@ -654,7 +616,7 @@
else
fmt = "%y";
return formatDate(d, fmt, axisOptions.monthNames);
return $.plot.formatDate(d, fmt, axisOptions.monthNames);
};
}
else {
......@@ -2070,6 +2032,44 @@
return plot;
};
// returns a string with the date d formatted according to fmt
$.plot.formatDate = function(d, fmt, monthNames) {
var leftPad = function(n) {
n = "" + n;
return n.length == 1 ? "0" + n : n;
};
var r = [];
var escape = false;
if (monthNames == null)
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
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 '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;
}
r.push(c);
escape = false;
}
else {
if (c == "%")
escape = true;
else
r.push(c);
}
}
return r.join("");
};
// round to nearby lower multiple of base
function floorInBase(n, base) {
return base * Math.floor(n / base);
......
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