Added option to customize x-labels.

This commit is contained in:
Jon Thornton 2012-04-03 11:31:59 -04:00
parent 4b6be4104c
commit 57211d6db2
4 changed files with 14 additions and 7 deletions

View File

@ -30,7 +30,8 @@ Morris.Line({
data: day_data,
xkey: 'period',
ykeys: ['licensed', 'sorned'],
labels: ['Licensed', 'SORN']
labels: ['Licensed', 'SORN'],
xLabelsFormat: function(d) { return (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear(); }
});
</pre>
</body>

View File

@ -62,6 +62,7 @@ class Morris.Line
units: ''
dateFormat: (x) -> new Date(x).toString()
xLabels: 'auto'
xLabelsFormat: null
# Do any necessary pre-processing for a new dataset
#
@ -228,7 +229,7 @@ class Morris.Line
# column label
drawLabel(@columnLabels[0], @xvals[0])
else
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels)
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelsFormat)
drawLabel(l[0], l[1])
else
for i in [0..@columnLabels.length]
@ -450,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) ->
Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelsFormat) ->
ddensity = 200 * (dmax - dmin) / pxwidth # seconds per `margin` pixels
d0 = new Date(dmin)
spec = Morris.LABEL_SPECS[specName]
@ -464,6 +465,9 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName) ->
# if we run out of options, use second-intervals
if spec is undefined
spec = Morris.LABEL_SPECS["second"]
# check if there's a user-defined formatting function
if xLabelsFormat
spec.fmt = xLabelsFormat
# calculate labels
d = spec.start(d0)
ret = []

View File

@ -57,7 +57,8 @@
dateFormat: function(x) {
return new Date(x).toString();
},
xLabels: 'auto'
xLabels: 'auto',
xLabelsFormat: null
};
Line.prototype.precalc = function() {
@ -248,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);
_ref = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelsFormat);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
l = _ref[_i];
@ -498,7 +499,7 @@
return (number < 10 ? '0' : '') + number;
};
Morris.labelSeries = function(dmin, dmax, pxwidth, specName) {
Morris.labelSeries = function(dmin, dmax, pxwidth, specName, xLabelsFormat) {
var d, d0, ddensity, name, ret, s, spec, t, _i, _len, _ref;
ddensity = 200 * (dmax - dmin) / pxwidth;
d0 = new Date(dmin);
@ -515,6 +516,7 @@
}
}
if (spec === void 0) spec = Morris.LABEL_SPECS["second"];
if (xLabelsFormat) spec.fmt = xLabelsFormat;
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