| 1 |
Rickshaw.namespace('Rickshaw.Graph.Axis.Time'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Axis.Time = function(args) { |
| 4 |
|
| 5 |
var self = this; |
| 6 |
|
| 7 |
this.graph = args.graph; |
| 8 |
this.elements = []; |
| 9 |
this.ticksTreatment = args.ticksTreatment || 'plain'; |
| 10 |
this.fixedTimeUnit = args.timeUnit; |
| 11 |
|
| 12 |
var time = args.timeFixture || new Rickshaw.Fixtures.Time(); |
| 13 |
|
| 14 |
this.appropriateTimeUnit = function() { |
| 15 |
|
| 16 |
var unit; |
| 17 |
var units = time.units; |
| 18 |
|
| 19 |
var domain = this.graph.x.domain(); |
| 20 |
var rangeSeconds = domain[1] - domain[0]; |
| 21 |
|
| 22 |
units.forEach( function(u) { |
| 23 |
if (Math.floor(rangeSeconds / u.seconds) >= 2) { |
| 24 |
unit = unit || u; |
| 25 |
} |
| 26 |
} ); |
| 27 |
|
| 28 |
return (unit || time.units[time.units.length - 1]); |
| 29 |
}; |
| 30 |
|
| 31 |
this.tickOffsets = function() { |
| 32 |
|
| 33 |
var domain = this.graph.x.domain(); |
| 34 |
|
| 35 |
var unit = this.fixedTimeUnit || this.appropriateTimeUnit(); |
| 36 |
var count = Math.ceil((domain[1] - domain[0]) / unit.seconds); |
| 37 |
|
| 38 |
var runningTick = domain[0]; |
| 39 |
|
| 40 |
var offsets = []; |
| 41 |
|
| 42 |
for (var i = 0; i < count; i++) { |
| 43 |
|
| 44 |
var tickValue = time.ceil(runningTick, unit); |
| 45 |
runningTick = tickValue + unit.seconds / 2; |
| 46 |
|
| 47 |
offsets.push( { value: tickValue, unit: unit } ); |
| 48 |
} |
| 49 |
|
| 50 |
return offsets; |
| 51 |
}; |
| 52 |
|
| 53 |
this.render = function() { |
| 54 |
|
| 55 |
this.elements.forEach( function(e) { |
| 56 |
e.parentNode.removeChild(e); |
| 57 |
} ); |
| 58 |
|
| 59 |
this.elements = []; |
| 60 |
|
| 61 |
var offsets = this.tickOffsets(); |
| 62 |
|
| 63 |
offsets.forEach( function(o) { |
| 64 |
|
| 65 |
if (self.graph.x(o.value) > self.graph.x.range()[1]) return; |
| 66 |
|
| 67 |
var element = document.createElement('div'); |
| 68 |
element.style.left = self.graph.x(o.value) + 'px'; |
| 69 |
element.classList.add('x_tick'); |
| 70 |
element.classList.add(self.ticksTreatment); |
| 71 |
|
| 72 |
var title = document.createElement('div'); |
| 73 |
title.classList.add('title'); |
| 74 |
title.innerHTML = o.unit.formatter(new Date(o.value * 1000)); |
| 75 |
element.appendChild(title); |
| 76 |
|
| 77 |
self.graph.element.appendChild(element); |
| 78 |
self.elements.push(element); |
| 79 |
|
| 80 |
} ); |
| 81 |
}; |
| 82 |
|
| 83 |
this.graph.onUpdate( function() { self.render() } ); |
| 84 |
}; |
| 85 |
|