Timestamp support.

This commit is contained in:
Olly Smith 2012-03-15 21:44:55 +00:00
parent 8261dd1e9a
commit ca5675954f
4 changed files with 56 additions and 2 deletions

37
examples/timestamps.html Normal file
View File

@ -0,0 +1,37 @@
<!doctype html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://raw.github.com/DmitryBaranovskiy/raphael/300aa589f5a0ba7fce667cd62c7cdda0bd5ad904/raphael-min.js"></script>
<script src="../morris.js"></script>
<script src="lib/prettify.js"></script>
<script src="lib/example.js"></script>
<link rel="stylesheet" href="lib/example.css">
<link rel="stylesheet" href="lib/prettify.css">
</head>
<body>
<h1>Timestamps</h1>
<div id="graph"></div>
<pre id="code" class="prettyprint linenums">
// data stolen from http://howmanyleft.co.uk/vehicle/jaguar_'e'_type
var timestamp_data = [
{"period": 1349046000000, "licensed": 3407, "sorned": 660},
{"period": 1313103600000, "licensed": 3351, "sorned": 629},
{"period": 1299110400000, "licensed": 3269, "sorned": 618},
{"period": 1281222000000, "licensed": 3246, "sorned": 661},
{"period": 1273446000000, "licensed": 3257, "sorned": 667},
{"period": 1268524800000, "licensed": 3248, "sorned": 627},
{"period": 1263081600000, "licensed": 3171, "sorned": 660},
{"period": 1260403200000, "licensed": 3171, "sorned": 676},
{"period": 1254870000000, "licensed": 3201, "sorned": 656},
{"period": 1253833200000, "licensed": 3215, "sorned": 622}
];
Morris.Line({
element: 'graph',
data: timestamp_data,
xkey: 'period',
ykeys: ['licensed', 'sorned'],
labels: ['Licensed', 'SORN'],
dateFormat: function (x) { return new Date(x).toDateString(); }
});
</pre>
</body>

View File

@ -60,6 +60,7 @@ class Morris.Line
hideHover: false
parseTime: true
units: ''
dateFormat: (x) -> new Date(x).toString()
# Do any necessary pre-processing for a new dataset
#
@ -81,6 +82,12 @@ class Morris.Line
@xvals = $.map @columnLabels, (x) => @parseYear x
else
@xvals = [(@columnLabels.length-1)..0]
# translate column labels, if they're timestamps
@columnLabels = $.map @columnLabels, (d) =>
if typeof d is 'number'
@options.dateFormat(d)
else
d
@xmin = Math.min.apply null, @xvals
@xmax = Math.max.apply null, @xvals
if @xmin is @xmax

View File

@ -48,7 +48,10 @@
smooth: true,
hideHover: false,
parseTime: true,
units: ''
units: '',
dateFormat: function(x) {
return new Date(x).toString();
}
};
Line.prototype.precalc = function() {
@ -80,6 +83,13 @@
return _results;
}).apply(this);
}
this.columnLabels = $.map(this.columnLabels, function(d) {
if (typeof d === 'number') {
return _this.options.dateFormat(d);
} else {
return d;
}
});
this.xmin = Math.min.apply(null, this.xvals);
this.xmax = Math.max.apply(null, this.xvals);
if (this.xmin === this.xmax) {

2
morris.min.js vendored

File diff suppressed because one or more lines are too long