| 1 |
Rickshaw.namespace('Rickshaw.Graph.HoverDetail'); |
| 2 |
|
| 3 |
Rickshaw.Graph.HoverDetail = Rickshaw.Class.create({ |
| 4 |
|
| 5 |
initialize: function(args) { |
| 6 |
|
| 7 |
var graph = this.graph = args.graph; |
| 8 |
|
| 9 |
this.xFormatter = args.xFormatter || function(x) { |
| 10 |
return new Date( x * 1000 ).toUTCString(); |
| 11 |
}; |
| 12 |
|
| 13 |
this.yFormatter = args.yFormatter || function(y) { |
| 14 |
return y === null ? y : y.toFixed(2); |
| 15 |
}; |
| 16 |
|
| 17 |
var element = this.element = document.createElement('div'); |
| 18 |
element.className = 'detail'; |
| 19 |
|
| 20 |
this.visible = true; |
| 21 |
graph.element.appendChild(element); |
| 22 |
|
| 23 |
this.lastEvent = null; |
| 24 |
this._addListeners(); |
| 25 |
|
| 26 |
this.onShow = args.onShow; |
| 27 |
this.onHide = args.onHide; |
| 28 |
this.onRender = args.onRender; |
| 29 |
|
| 30 |
this.formatter = args.formatter || this.formatter; |
| 31 |
|
| 32 |
}, |
| 33 |
|
| 34 |
formatter: function(series, x, y, formattedX, formattedY, d) { |
| 35 |
return series.name + ': ' + formattedY; |
| 36 |
}, |
| 37 |
|
| 38 |
update: function(e) { |
| 39 |
|
| 40 |
e = e || this.lastEvent; |
| 41 |
if (!e) return; |
| 42 |
this.lastEvent = e; |
| 43 |
|
| 44 |
if (!e.target.nodeName.match(/^(path|svg|rect|circle)$/)) return; |
| 45 |
|
| 46 |
var graph = this.graph; |
| 47 |
|
| 48 |
var eventX = e.offsetX || e.layerX; |
| 49 |
var eventY = e.offsetY || e.layerY; |
| 50 |
|
| 51 |
var j = 0; |
| 52 |
var points = []; |
| 53 |
var nearestPoint; |
| 54 |
|
| 55 |
this.graph.series.active().forEach( function(series) { |
| 56 |
|
| 57 |
var data = this.graph.stackedData[j++]; |
| 58 |
|
| 59 |
if (!data.length) |
| 60 |
return; |
| 61 |
|
| 62 |
var domainX = graph.x.invert(eventX); |
| 63 |
|
| 64 |
var domainIndexScale = d3.scale.linear() |
| 65 |
.domain([data[0].x, data.slice(-1)[0].x]) |
| 66 |
.range([0, data.length - 1]); |
| 67 |
|
| 68 |
var approximateIndex = Math.round(domainIndexScale(domainX)); |
| 69 |
if (approximateIndex == data.length - 1) approximateIndex--; |
| 70 |
|
| 71 |
var dataIndex = Math.min(approximateIndex || 0, data.length - 1); |
| 72 |
|
| 73 |
for (var i = approximateIndex; i < data.length - 1;) { |
| 74 |
|
| 75 |
if (!data[i] || !data[i + 1]) break; |
| 76 |
|
| 77 |
if (data[i].x <= domainX && data[i + 1].x > domainX) { |
| 78 |
dataIndex = Math.abs(domainX - data[i].x) < Math.abs(domainX - data[i + 1].x) ? i : i + 1; |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
if (data[i + 1].x <= domainX) { i++ } else { i-- } |
| 83 |
} |
| 84 |
|
| 85 |
if (dataIndex < 0) dataIndex = 0; |
| 86 |
var value = data[dataIndex]; |
| 87 |
|
| 88 |
var distance = Math.sqrt( |
| 89 |
Math.pow(Math.abs(graph.x(value.x) - eventX), 2) + |
| 90 |
Math.pow(Math.abs(graph.y(value.y + value.y0) - eventY), 2) |
| 91 |
); |
| 92 |
|
| 93 |
var xFormatter = series.xFormatter || this.xFormatter; |
| 94 |
var yFormatter = series.yFormatter || this.yFormatter; |
| 95 |
|
| 96 |
var point = { |
| 97 |
formattedXValue: xFormatter(value.x), |
| 98 |
formattedYValue: yFormatter(series.scale ? series.scale.invert(value.y) : value.y), |
| 99 |
series: series, |
| 100 |
value: value, |
| 101 |
distance: distance, |
| 102 |
order: j, |
| 103 |
name: series.name |
| 104 |
}; |
| 105 |
|
| 106 |
if (!nearestPoint || distance < nearestPoint.distance) { |
| 107 |
nearestPoint = point; |
| 108 |
} |
| 109 |
|
| 110 |
points.push(point); |
| 111 |
|
| 112 |
}, this ); |
| 113 |
|
| 114 |
if (!nearestPoint) |
| 115 |
return; |
| 116 |
|
| 117 |
nearestPoint.active = true; |
| 118 |
|
| 119 |
var domainX = nearestPoint.value.x; |
| 120 |
var formattedXValue = nearestPoint.formattedXValue; |
| 121 |
|
| 122 |
this.element.innerHTML = ''; |
| 123 |
this.element.style.left = graph.x(domainX) + 'px'; |
| 124 |
|
| 125 |
this.visible && this.render( { |
| 126 |
points: points, |
| 127 |
detail: points, // for backwards compatibility |
| 128 |
mouseX: eventX, |
| 129 |
mouseY: eventY, |
| 130 |
formattedXValue: formattedXValue, |
| 131 |
domainX: domainX |
| 132 |
} ); |
| 133 |
}, |
| 134 |
|
| 135 |
hide: function() { |
| 136 |
this.visible = false; |
| 137 |
this.element.classList.add('inactive'); |
| 138 |
|
| 139 |
if (typeof this.onHide == 'function') { |
| 140 |
this.onHide(); |
| 141 |
} |
| 142 |
}, |
| 143 |
|
| 144 |
show: function() { |
| 145 |
this.visible = true; |
| 146 |
this.element.classList.remove('inactive'); |
| 147 |
|
| 148 |
if (typeof this.onShow == 'function') { |
| 149 |
this.onShow(); |
| 150 |
} |
| 151 |
}, |
| 152 |
|
| 153 |
render: function(args) { |
| 154 |
|
| 155 |
var graph = this.graph; |
| 156 |
var points = args.points; |
| 157 |
var point = points.filter( function(p) { return p.active } ).shift(); |
| 158 |
|
| 159 |
if (point.value.y === null) return; |
| 160 |
|
| 161 |
var formattedXValue = point.formattedXValue; |
| 162 |
var formattedYValue = point.formattedYValue; |
| 163 |
|
| 164 |
this.element.innerHTML = ''; |
| 165 |
this.element.style.left = graph.x(point.value.x) + 'px'; |
| 166 |
|
| 167 |
var xLabel = document.createElement('div'); |
| 168 |
|
| 169 |
xLabel.className = 'x_label'; |
| 170 |
xLabel.innerHTML = formattedXValue; |
| 171 |
this.element.appendChild(xLabel); |
| 172 |
|
| 173 |
var item = document.createElement('div'); |
| 174 |
|
| 175 |
item.className = 'item'; |
| 176 |
|
| 177 |
// invert the scale if this series displays using a scale |
| 178 |
var series = point.series; |
| 179 |
var actualY = series.scale ? series.scale.invert(point.value.y) : point.value.y; |
| 180 |
|
| 181 |
item.innerHTML = this.formatter(series, point.value.x, actualY, formattedXValue, formattedYValue, point); |
| 182 |
item.style.top = this.graph.y(point.value.y0 + point.value.y) + 'px'; |
| 183 |
|
| 184 |
this.element.appendChild(item); |
| 185 |
|
| 186 |
var dot = document.createElement('div'); |
| 187 |
|
| 188 |
dot.className = 'dot'; |
| 189 |
dot.style.top = item.style.top; |
| 190 |
dot.style.borderColor = series.color; |
| 191 |
|
| 192 |
this.element.appendChild(dot); |
| 193 |
|
| 194 |
if (point.active) { |
| 195 |
item.classList.add('active'); |
| 196 |
dot.classList.add('active'); |
| 197 |
} |
| 198 |
|
| 199 |
// Assume left alignment until the element has been displayed and |
| 200 |
// bounding box calculations are possible. |
| 201 |
var alignables = [xLabel, item]; |
| 202 |
alignables.forEach(function(el) { |
| 203 |
el.classList.add('left'); |
| 204 |
}); |
| 205 |
|
| 206 |
this.show(); |
| 207 |
|
| 208 |
// If left-alignment results in any error, try right-alignment. |
| 209 |
var leftAlignError = this._calcLayoutError(alignables); |
| 210 |
if (leftAlignError > 0) { |
| 211 |
alignables.forEach(function(el) { |
| 212 |
el.classList.remove('left'); |
| 213 |
el.classList.add('right'); |
| 214 |
}); |
| 215 |
|
| 216 |
// If right-alignment is worse than left alignment, switch back. |
| 217 |
var rightAlignError = this._calcLayoutError(alignables); |
| 218 |
if (rightAlignError > leftAlignError) { |
| 219 |
alignables.forEach(function(el) { |
| 220 |
el.classList.remove('right'); |
| 221 |
el.classList.add('left'); |
| 222 |
}); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (typeof this.onRender == 'function') { |
| 227 |
this.onRender(args); |
| 228 |
} |
| 229 |
}, |
| 230 |
|
| 231 |
_calcLayoutError: function(alignables) { |
| 232 |
// Layout error is calculated as the number of linear pixels by which |
| 233 |
// an alignable extends past the left or right edge of the parent. |
| 234 |
var parentRect = this.element.parentNode.getBoundingClientRect(); |
| 235 |
|
| 236 |
var error = 0; |
| 237 |
var alignRight = alignables.forEach(function(el) { |
| 238 |
var rect = el.getBoundingClientRect(); |
| 239 |
if (!rect.width) { |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
if (rect.right > parentRect.right) { |
| 244 |
error += rect.right - parentRect.right; |
| 245 |
} |
| 246 |
|
| 247 |
if (rect.left < parentRect.left) { |
| 248 |
error += parentRect.left - rect.left; |
| 249 |
} |
| 250 |
}); |
| 251 |
return error; |
| 252 |
}, |
| 253 |
|
| 254 |
_addListeners: function() { |
| 255 |
|
| 256 |
this.graph.element.addEventListener( |
| 257 |
'mousemove', |
| 258 |
function(e) { |
| 259 |
this.visible = true; |
| 260 |
this.update(e); |
| 261 |
}.bind(this), |
| 262 |
false |
| 263 |
); |
| 264 |
|
| 265 |
this.graph.onUpdate( function() { this.update() }.bind(this) ); |
| 266 |
|
| 267 |
this.graph.element.addEventListener( |
| 268 |
'mouseout', |
| 269 |
function(e) { |
| 270 |
if (e.relatedTarget && !(e.relatedTarget.compareDocumentPosition(this.graph.element) & Node.DOCUMENT_POSITION_CONTAINS)) { |
| 271 |
this.hide(); |
| 272 |
} |
| 273 |
}.bind(this), |
| 274 |
false |
| 275 |
); |
| 276 |
} |
| 277 |
}); |