mirror of
https://github.com/morrisjs/morris.js.git
synced 2024-11-13 07:11:12 +01:00
Added option to customize x-labels.
This commit is contained in:
parent
4b6be4104c
commit
57211d6db2
4 changed files with 14 additions and 7 deletions
|
@ -30,7 +30,8 @@ Morris.Line({
|
||||||
data: day_data,
|
data: day_data,
|
||||||
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(); }
|
||||||
});
|
});
|
||||||
</pre>
|
</pre>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -62,6 +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
|
||||||
|
|
||||||
# Do any necessary pre-processing for a new dataset
|
# Do any necessary pre-processing for a new dataset
|
||||||
#
|
#
|
||||||
|
@ -228,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)
|
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelsFormat)
|
||||||
drawLabel(l[0], l[1])
|
drawLabel(l[0], l[1])
|
||||||
else
|
else
|
||||||
for i in [0..@columnLabels.length]
|
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
|
# 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
|
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]
|
||||||
|
@ -464,6 +465,9 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName) ->
|
||||||
# if we run out of options, use second-intervals
|
# if we run out of options, use second-intervals
|
||||||
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
|
||||||
|
if xLabelsFormat
|
||||||
|
spec.fmt = xLabelsFormat
|
||||||
# calculate labels
|
# calculate labels
|
||||||
d = spec.start(d0)
|
d = spec.start(d0)
|
||||||
ret = []
|
ret = []
|
||||||
|
|
|
@ -57,7 +57,8 @@
|
||||||
dateFormat: function(x) {
|
dateFormat: function(x) {
|
||||||
return new Date(x).toString();
|
return new Date(x).toString();
|
||||||
},
|
},
|
||||||
xLabels: 'auto'
|
xLabels: 'auto',
|
||||||
|
xLabelsFormat: null
|
||||||
};
|
};
|
||||||
|
|
||||||
Line.prototype.precalc = function() {
|
Line.prototype.precalc = function() {
|
||||||
|
@ -248,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);
|
_ref = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelsFormat);
|
||||||
_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];
|
||||||
|
@ -498,7 +499,7 @@
|
||||||
return (number < 10 ? '0' : '') + number;
|
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;
|
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);
|
||||||
|
@ -515,6 +516,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (spec === void 0) spec = Morris.LABEL_SPECS["second"];
|
if (spec === void 0) spec = Morris.LABEL_SPECS["second"];
|
||||||
|
if (xLabelsFormat) spec.fmt = xLabelsFormat;
|
||||||
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
Loading…
Reference in a new issue