| 1 |
Rickshaw.namespace('Rickshaw.Graph.Annotate'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Annotate = function(args) { |
| 4 |
|
| 5 |
var graph = this.graph = args.graph; |
| 6 |
this.elements = { timeline: args.element }; |
| 7 |
|
| 8 |
var self = this; |
| 9 |
|
| 10 |
this.data = {}; |
| 11 |
|
| 12 |
this.elements.timeline.classList.add('rickshaw_annotation_timeline'); |
| 13 |
|
| 14 |
this.add = function(time, content, end_time) { |
| 15 |
self.data[time] = self.data[time] || {'boxes': []}; |
| 16 |
self.data[time].boxes.push({content: content, end: end_time}); |
| 17 |
}; |
| 18 |
|
| 19 |
this.update = function() { |
| 20 |
|
| 21 |
Rickshaw.keys(self.data).forEach( function(time) { |
| 22 |
|
| 23 |
var annotation = self.data[time]; |
| 24 |
var left = self.graph.x(time); |
| 25 |
|
| 26 |
if (left < 0 || left > self.graph.x.range()[1]) { |
| 27 |
if (annotation.element) { |
| 28 |
annotation.line.classList.add('offscreen'); |
| 29 |
annotation.element.style.display = 'none'; |
| 30 |
} |
| 31 |
|
| 32 |
annotation.boxes.forEach( function(box) { |
| 33 |
if ( box.rangeElement ) box.rangeElement.classList.add('offscreen'); |
| 34 |
}); |
| 35 |
|
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if (!annotation.element) { |
| 40 |
var element = annotation.element = document.createElement('div'); |
| 41 |
element.classList.add('annotation'); |
| 42 |
this.elements.timeline.appendChild(element); |
| 43 |
element.addEventListener('click', function(e) { |
| 44 |
element.classList.toggle('active'); |
| 45 |
annotation.line.classList.toggle('active'); |
| 46 |
annotation.boxes.forEach( function(box) { |
| 47 |
if ( box.rangeElement ) box.rangeElement.classList.toggle('active'); |
| 48 |
}); |
| 49 |
}, false); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
annotation.element.style.left = left + 'px'; |
| 54 |
annotation.element.style.display = 'block'; |
| 55 |
|
| 56 |
annotation.boxes.forEach( function(box) { |
| 57 |
|
| 58 |
|
| 59 |
var element = box.element; |
| 60 |
|
| 61 |
if (!element) { |
| 62 |
element = box.element = document.createElement('div'); |
| 63 |
element.classList.add('content'); |
| 64 |
element.innerHTML = box.content; |
| 65 |
annotation.element.appendChild(element); |
| 66 |
|
| 67 |
annotation.line = document.createElement('div'); |
| 68 |
annotation.line.classList.add('annotation_line'); |
| 69 |
self.graph.element.appendChild(annotation.line); |
| 70 |
|
| 71 |
if ( box.end ) { |
| 72 |
box.rangeElement = document.createElement('div'); |
| 73 |
box.rangeElement.classList.add('annotation_range'); |
| 74 |
self.graph.element.appendChild(box.rangeElement); |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
if ( box.end ) { |
| 80 |
|
| 81 |
var annotationRangeStart = left; |
| 82 |
var annotationRangeEnd = Math.min( self.graph.x(box.end), self.graph.x.range()[1] ); |
| 83 |
|
| 84 |
// annotation makes more sense at end |
| 85 |
if ( annotationRangeStart > annotationRangeEnd ) { |
| 86 |
annotationRangeEnd = left; |
| 87 |
annotationRangeStart = Math.max( self.graph.x(box.end), self.graph.x.range()[0] ); |
| 88 |
} |
| 89 |
|
| 90 |
var annotationRangeWidth = annotationRangeEnd - annotationRangeStart; |
| 91 |
|
| 92 |
box.rangeElement.style.left = annotationRangeStart + 'px'; |
| 93 |
box.rangeElement.style.width = annotationRangeWidth + 'px'; |
| 94 |
|
| 95 |
box.rangeElement.classList.remove('offscreen'); |
| 96 |
} |
| 97 |
|
| 98 |
annotation.line.classList.remove('offscreen'); |
| 99 |
annotation.line.style.left = left + 'px'; |
| 100 |
} ); |
| 101 |
}, this ); |
| 102 |
}; |
| 103 |
|
| 104 |
this.graph.onUpdate( function() { self.update() } ); |
| 105 |
}; |