Merge xLabelFormat contributions by @jonthornton.

Rename `xLabelsFormat` -> `xLabelFormat` (grammatically better :))

Add note to example to recommend use of `xLabels` option when using
`xLabelFormat`.

Use $.extends rather than setting spec.fmt directly (avoids modifying
the globals).

Add a test.
This commit is contained in:
Olly Smith 2012-04-04 20:32:24 +01:00
parent 57211d6db2
commit 564126fbfa
5 changed files with 31 additions and 11 deletions

View File

@ -31,7 +31,10 @@ Morris.Line({
xkey: 'period',
ykeys: ['licensed', 'sorned'],
labels: ['Licensed', 'SORN'],
xLabelsFormat: function(d) { return (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear(); }
// custom label formatting with `xLabelFormat`
xLabelFormat: function(d) { return (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear(); },
// setting `xLabels` is recommended when using xLabelFormat
xLabels: 'day'
});
</pre>
</body>

View File

@ -62,7 +62,7 @@ class Morris.Line
units: ''
dateFormat: (x) -> new Date(x).toString()
xLabels: 'auto'
xLabelsFormat: null
xLabelFormat: null
# Do any necessary pre-processing for a new dataset
#
@ -229,7 +229,7 @@ class Morris.Line
# column label
drawLabel(@columnLabels[0], @xvals[0])
else
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelsFormat)
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat)
drawLabel(l[0], l[1])
else
for i in [0..@columnLabels.length]
@ -451,7 +451,7 @@ Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number
# generate a series of label, timestamp pairs for x-axis labels
#
Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelsFormat) ->
Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) ->
ddensity = 200 * (dmax - dmin) / pxwidth # seconds per `margin` pixels
d0 = new Date(dmin)
spec = Morris.LABEL_SPECS[specName]
@ -466,8 +466,8 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelsFormat) ->
if spec is undefined
spec = Morris.LABEL_SPECS["second"]
# check if there's a user-defined formatting function
if xLabelsFormat
spec.fmt = xLabelsFormat
if xLabelFormat
spec = $.extend({}, spec, {fmt: xLabelFormat})
# calculate labels
d = spec.start(d0)
ret = []

View File

@ -58,7 +58,7 @@
return new Date(x).toString();
},
xLabels: 'auto',
xLabelsFormat: null
xLabelFormat: null
};
Line.prototype.precalc = function() {
@ -249,7 +249,7 @@
if (this.columnLabels.length === 1 && this.options.xLabels === 'auto') {
return drawLabel(this.columnLabels[0], this.xvals[0]);
} else {
_ref = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelsFormat);
_ref = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelFormat);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
l = _ref[_i];
@ -499,7 +499,7 @@
return (number < 10 ? '0' : '') + number;
};
Morris.labelSeries = function(dmin, dmax, pxwidth, specName, xLabelsFormat) {
Morris.labelSeries = function(dmin, dmax, pxwidth, specName, xLabelFormat) {
var d, d0, ddensity, name, ret, s, spec, t, _i, _len, _ref;
ddensity = 200 * (dmax - dmin) / pxwidth;
d0 = new Date(dmin);
@ -516,7 +516,11 @@
}
}
if (spec === void 0) spec = Morris.LABEL_SPECS["second"];
if (xLabelsFormat) spec.fmt = xLabelsFormat;
if (xLabelFormat) {
spec = $.extend({}, spec, {
fmt: xLabelFormat
});
}
d = spec.start(d0);
ret = [];
while ((t = d.getTime()) <= dmax) {

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -163,6 +163,19 @@
new Date(2012, 0, 12).getTime(),
1000, "year"),
[["2012", new Date(2012, 0, 1).getTime()]]);
// test custom formatter
deepEqual(
Morris.labelSeries(
new Date(2012, 0, 1).getTime(),
new Date(2012, 0, 6).getTime(),
1000, "day",
function (d) { return (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear(); }),
[["1/1/2012", new Date(2012, 0, 1).getTime()],
["1/2/2012", new Date(2012, 0, 2).getTime()],
["1/3/2012", new Date(2012, 0, 3).getTime()],
["1/4/2012", new Date(2012, 0, 4).getTime()],
["1/5/2012", new Date(2012, 0, 5).getTime()],
["1/6/2012", new Date(2012, 0, 6).getTime()]]);
});
test("Morris.parseDate", function () {