Decade intervals for time-axis labels.

This commit is contained in:
Olly Smith 2012-11-08 17:18:23 +00:00
parent 2a2a849ebd
commit 25698d6664
4 changed files with 46 additions and 3 deletions

View File

@ -329,6 +329,11 @@ secondsSpecHelper = (interval) ->
incr: (d) -> d.setSeconds(d.getSeconds() + interval) incr: (d) -> d.setSeconds(d.getSeconds() + interval)
Morris.LABEL_SPECS = Morris.LABEL_SPECS =
"decade":
span: 172800000000 # 10 * 365 * 24 * 60 * 60 * 1000
start: (d) -> new Date(d.getFullYear() - d.getFullYear() % 10, 0, 1)
fmt: (d) -> "#{d.getFullYear()}"
incr: (d) -> d.setFullYear(d.getFullYear() + 10)
"year": "year":
span: 17280000000 # 365 * 24 * 60 * 60 * 1000 span: 17280000000 # 365 * 24 * 60 * 60 * 1000
start: (d) -> new Date(d.getFullYear(), 0, 1) start: (d) -> new Date(d.getFullYear(), 0, 1)
@ -357,7 +362,7 @@ Morris.LABEL_SPECS =
"second": secondsSpecHelper(1) "second": secondsSpecHelper(1)
Morris.AUTO_LABEL_ORDER = [ Morris.AUTO_LABEL_ORDER = [
"year", "month", "day", "hour", "decade", "year", "month", "day", "hour",
"30min", "15min", "10min", "5min", "minute", "30min", "15min", "10min", "5min", "minute",
"30sec", "15sec", "10sec", "5sec", "second" "30sec", "15sec", "10sec", "5sec", "second"
] ]

View File

@ -892,6 +892,18 @@
}; };
Morris.LABEL_SPECS = { Morris.LABEL_SPECS = {
"decade": {
span: 172800000000,
start: function(d) {
return new Date(d.getFullYear() - d.getFullYear() % 10, 0, 1);
},
fmt: function(d) {
return "" + (d.getFullYear());
},
incr: function(d) {
return d.setFullYear(d.getFullYear() + 10);
}
},
"year": { "year": {
span: 17280000000, span: 17280000000,
start: function(d) { start: function(d) {
@ -941,7 +953,7 @@
"second": secondsSpecHelper(1) "second": secondsSpecHelper(1)
}; };
Morris.AUTO_LABEL_ORDER = ["year", "month", "day", "hour", "30min", "15min", "10min", "5min", "minute", "30sec", "15sec", "10sec", "5sec", "second"]; Morris.AUTO_LABEL_ORDER = ["decade", "year", "month", "day", "hour", "30min", "15min", "10min", "5min", "minute", "30sec", "15sec", "10sec", "5sec", "second"];
Morris.Area = (function(_super) { Morris.Area = (function(_super) {

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,31 @@
describe '#labelSeries', -> describe '#labelSeries', ->
it 'should generate decade intervals', ->
Morris.labelSeries(
new Date(1952, 0, 1).getTime(),
new Date(2012, 0, 1).getTime(),
1000
).should.deep.equal([
["1960", new Date(1960, 0, 1).getTime()],
["1970", new Date(1970, 0, 1).getTime()],
["1980", new Date(1980, 0, 1).getTime()],
["1990", new Date(1990, 0, 1).getTime()],
["2000", new Date(2000, 0, 1).getTime()],
["2010", new Date(2010, 0, 1).getTime()]
])
Morris.labelSeries(
new Date(1952, 3, 1).getTime(),
new Date(2012, 3, 1).getTime(),
1000
).should.deep.equal([
["1960", new Date(1960, 0, 1).getTime()],
["1970", new Date(1970, 0, 1).getTime()],
["1980", new Date(1980, 0, 1).getTime()],
["1990", new Date(1990, 0, 1).getTime()],
["2000", new Date(2000, 0, 1).getTime()],
["2010", new Date(2010, 0, 1).getTime()]
])
it 'should generate year intervals', -> it 'should generate year intervals', ->
Morris.labelSeries( Morris.labelSeries(
new Date(2007, 0, 1).getTime(), new Date(2007, 0, 1).getTime(),