mirror of
https://github.com/morrisjs/morris.js.git
synced 2024-11-10 21:36:34 +01:00
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:
parent
57211d6db2
commit
564126fbfa
@ -31,7 +31,10 @@ Morris.Line({
|
|||||||
xkey: 'period',
|
xkey: 'period',
|
||||||
ykeys: ['licensed', 'sorned'],
|
ykeys: ['licensed', 'sorned'],
|
||||||
labels: ['Licensed', 'SORN'],
|
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>
|
</pre>
|
||||||
</body>
|
</body>
|
||||||
|
@ -62,7 +62,7 @@ class Morris.Line
|
|||||||
units: ''
|
units: ''
|
||||||
dateFormat: (x) -> new Date(x).toString()
|
dateFormat: (x) -> new Date(x).toString()
|
||||||
xLabels: 'auto'
|
xLabels: 'auto'
|
||||||
xLabelsFormat: null
|
xLabelFormat: null
|
||||||
|
|
||||||
# Do any necessary pre-processing for a new dataset
|
# Do any necessary pre-processing for a new dataset
|
||||||
#
|
#
|
||||||
@ -229,7 +229,7 @@ class Morris.Line
|
|||||||
# column label
|
# column label
|
||||||
drawLabel(@columnLabels[0], @xvals[0])
|
drawLabel(@columnLabels[0], @xvals[0])
|
||||||
else
|
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])
|
drawLabel(l[0], l[1])
|
||||||
else
|
else
|
||||||
for i in [0..@columnLabels.length]
|
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
|
# 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
|
ddensity = 200 * (dmax - dmin) / pxwidth # seconds per `margin` pixels
|
||||||
d0 = new Date(dmin)
|
d0 = new Date(dmin)
|
||||||
spec = Morris.LABEL_SPECS[specName]
|
spec = Morris.LABEL_SPECS[specName]
|
||||||
@ -466,8 +466,8 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelsFormat) ->
|
|||||||
if spec is undefined
|
if spec is undefined
|
||||||
spec = Morris.LABEL_SPECS["second"]
|
spec = Morris.LABEL_SPECS["second"]
|
||||||
# check if there's a user-defined formatting function
|
# check if there's a user-defined formatting function
|
||||||
if xLabelsFormat
|
if xLabelFormat
|
||||||
spec.fmt = xLabelsFormat
|
spec = $.extend({}, spec, {fmt: xLabelFormat})
|
||||||
# calculate labels
|
# calculate labels
|
||||||
d = spec.start(d0)
|
d = spec.start(d0)
|
||||||
ret = []
|
ret = []
|
||||||
|
12
morris.js
12
morris.js
@ -58,7 +58,7 @@
|
|||||||
return new Date(x).toString();
|
return new Date(x).toString();
|
||||||
},
|
},
|
||||||
xLabels: 'auto',
|
xLabels: 'auto',
|
||||||
xLabelsFormat: null
|
xLabelFormat: null
|
||||||
};
|
};
|
||||||
|
|
||||||
Line.prototype.precalc = function() {
|
Line.prototype.precalc = function() {
|
||||||
@ -249,7 +249,7 @@
|
|||||||
if (this.columnLabels.length === 1 && this.options.xLabels === 'auto') {
|
if (this.columnLabels.length === 1 && this.options.xLabels === 'auto') {
|
||||||
return drawLabel(this.columnLabels[0], this.xvals[0]);
|
return drawLabel(this.columnLabels[0], this.xvals[0]);
|
||||||
} else {
|
} 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 = [];
|
_results = [];
|
||||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||||
l = _ref[_i];
|
l = _ref[_i];
|
||||||
@ -499,7 +499,7 @@
|
|||||||
return (number < 10 ? '0' : '') + number;
|
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;
|
var d, d0, ddensity, name, ret, s, spec, t, _i, _len, _ref;
|
||||||
ddensity = 200 * (dmax - dmin) / pxwidth;
|
ddensity = 200 * (dmax - dmin) / pxwidth;
|
||||||
d0 = new Date(dmin);
|
d0 = new Date(dmin);
|
||||||
@ -516,7 +516,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (spec === void 0) spec = Morris.LABEL_SPECS["second"];
|
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);
|
d = spec.start(d0);
|
||||||
ret = [];
|
ret = [];
|
||||||
while ((t = d.getTime()) <= dmax) {
|
while ((t = d.getTime()) <= dmax) {
|
||||||
|
2
morris.min.js
vendored
2
morris.min.js
vendored
File diff suppressed because one or more lines are too long
@ -163,6 +163,19 @@
|
|||||||
new Date(2012, 0, 12).getTime(),
|
new Date(2012, 0, 12).getTime(),
|
||||||
1000, "year"),
|
1000, "year"),
|
||||||
[["2012", new Date(2012, 0, 1).getTime()]]);
|
[["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 () {
|
test("Morris.parseDate", function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user