| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
(function($) { |
| 32 |
$.jqplot.eventListenerHooks.push(['jqplotMouseMove', handleMove]); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
$.jqplot.Highlighter = function(options) { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
this.show = $.jqplot.config.enablePlugins; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
this.markerRenderer = new $.jqplot.MarkerRenderer({shadow:false}); |
| 87 |
|
| 88 |
|
| 89 |
this.showMarker = true; |
| 90 |
|
| 91 |
|
| 92 |
this.lineWidthAdjust = 2.5; |
| 93 |
|
| 94 |
|
| 95 |
this.sizeAdjust = 5; |
| 96 |
|
| 97 |
|
| 98 |
this.showTooltip = true; |
| 99 |
|
| 100 |
|
| 101 |
this.tooltipLocation = 'nw'; |
| 102 |
|
| 103 |
|
| 104 |
this.fadeTooltip = true; |
| 105 |
|
| 106 |
|
| 107 |
this.tooltipFadeSpeed = "fast"; |
| 108 |
|
| 109 |
|
| 110 |
this.tooltipOffset = 2; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
this.tooltipAxes = 'both'; |
| 115 |
|
| 116 |
|
| 117 |
this.tooltipSeparator = ', '; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
this.tooltipContentEditor = null; |
| 124 |
|
| 125 |
|
| 126 |
this.useAxesFormatters = true; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
this.tooltipFormatString = '%.5P'; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
this.formatString = null; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
this.yvalues = 1; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
this.bringSeriesToFront = false; |
| 153 |
this._tooltipElem; |
| 154 |
this.isHighlighting = false; |
| 155 |
this.currentNeighbor = null; |
| 156 |
|
| 157 |
$.extend(true, this, options); |
| 158 |
}; |
| 159 |
|
| 160 |
var locations = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w']; |
| 161 |
var locationIndicies = {'nw':0, 'n':1, 'ne':2, 'e':3, 'se':4, 's':5, 'sw':6, 'w':7}; |
| 162 |
var oppositeLocations = ['se', 's', 'sw', 'w', 'nw', 'n', 'ne', 'e']; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
$.jqplot.Highlighter.init = function (target, data, opts){ |
| 168 |
var options = opts || {}; |
| 169 |
|
| 170 |
this.plugins.highlighter = new $.jqplot.Highlighter(options.highlighter); |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
$.jqplot.Highlighter.parseOptions = function (defaults, options) { |
| 175 |
|
| 176 |
|
| 177 |
this.showHighlight = true; |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
$.jqplot.Highlighter.postPlotDraw = function() { |
| 184 |
|
| 185 |
if (this.plugins.highlighter && this.plugins.highlighter.highlightCanvas) { |
| 186 |
this.plugins.highlighter.highlightCanvas.resetCanvas(); |
| 187 |
this.plugins.highlighter.highlightCanvas = null; |
| 188 |
} |
| 189 |
|
| 190 |
if (this.plugins.highlighter && this.plugins.highlighter._tooltipElem) { |
| 191 |
this.plugins.highlighter._tooltipElem.emptyForce(); |
| 192 |
this.plugins.highlighter._tooltipElem = null; |
| 193 |
} |
| 194 |
|
| 195 |
this.plugins.highlighter.highlightCanvas = new $.jqplot.GenericCanvas(); |
| 196 |
|
| 197 |
this.eventCanvas._elem.before(this.plugins.highlighter.highlightCanvas.createElement(this._gridPadding, 'jqplot-highlight-canvas', this._plotDimensions, this)); |
| 198 |
this.plugins.highlighter.highlightCanvas.setContext(); |
| 199 |
|
| 200 |
var elem = document.createElement('div'); |
| 201 |
this.plugins.highlighter._tooltipElem = $(elem); |
| 202 |
elem = null; |
| 203 |
this.plugins.highlighter._tooltipElem.addClass('jqplot-highlighter-tooltip'); |
| 204 |
this.plugins.highlighter._tooltipElem.css({position:'absolute', display:'none'}); |
| 205 |
|
| 206 |
this.eventCanvas._elem.before(this.plugins.highlighter._tooltipElem); |
| 207 |
}; |
| 208 |
|
| 209 |
$.jqplot.preInitHooks.push($.jqplot.Highlighter.init); |
| 210 |
$.jqplot.preParseSeriesOptionsHooks.push($.jqplot.Highlighter.parseOptions); |
| 211 |
$.jqplot.postDrawHooks.push($.jqplot.Highlighter.postPlotDraw); |
| 212 |
|
| 213 |
function draw(plot, neighbor) { |
| 214 |
var hl = plot.plugins.highlighter; |
| 215 |
var s = plot.series[neighbor.seriesIndex]; |
| 216 |
var smr = s.markerRenderer; |
| 217 |
var mr = hl.markerRenderer; |
| 218 |
mr.style = smr.style; |
| 219 |
mr.lineWidth = smr.lineWidth + hl.lineWidthAdjust; |
| 220 |
mr.size = smr.size + hl.sizeAdjust; |
| 221 |
var rgba = $.jqplot.getColorComponents(smr.color); |
| 222 |
var newrgb = [rgba[0], rgba[1], rgba[2]]; |
| 223 |
var alpha = (rgba[3] >= 0.6) ? rgba[3]*0.6 : rgba[3]*(2-rgba[3]); |
| 224 |
mr.color = 'rgba('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+','+alpha+')'; |
| 225 |
mr.init(); |
| 226 |
mr.draw(s.gridData[neighbor.pointIndex][0], s.gridData[neighbor.pointIndex][1], hl.highlightCanvas._ctx); |
| 227 |
} |
| 228 |
|
| 229 |
function showTooltip(plot, series, neighbor) { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
var hl = plot.plugins.highlighter; |
| 234 |
var elem = hl._tooltipElem; |
| 235 |
var serieshl = series.highlighter || {}; |
| 236 |
|
| 237 |
var opts = $.extend(true, {}, hl, serieshl); |
| 238 |
|
| 239 |
if (opts.useAxesFormatters) { |
| 240 |
var xf = series._xaxis._ticks[0].formatter; |
| 241 |
var yf = series._yaxis._ticks[0].formatter; |
| 242 |
var xfstr = series._xaxis._ticks[0].formatString; |
| 243 |
var yfstr = series._yaxis._ticks[0].formatString; |
| 244 |
var str; |
| 245 |
var xstr = xf(xfstr, neighbor.data[0]); |
| 246 |
var ystrs = []; |
| 247 |
for (var i=1; i<opts.yvalues+1; i++) { |
| 248 |
ystrs.push(yf(yfstr, neighbor.data[i])); |
| 249 |
} |
| 250 |
if (typeof opts.formatString === 'string') { |
| 251 |
switch (opts.tooltipAxes) { |
| 252 |
case 'both': |
| 253 |
case 'xy': |
| 254 |
ystrs.unshift(xstr); |
| 255 |
ystrs.unshift(opts.formatString); |
| 256 |
str = $.jqplot.sprintf.apply($.jqplot.sprintf, ystrs); |
| 257 |
break; |
| 258 |
case 'yx': |
| 259 |
ystrs.push(xstr); |
| 260 |
ystrs.unshift(opts.formatString); |
| 261 |
str = $.jqplot.sprintf.apply($.jqplot.sprintf, ystrs); |
| 262 |
break; |
| 263 |
case 'x': |
| 264 |
str = $.jqplot.sprintf.apply($.jqplot.sprintf, [opts.formatString, xstr]); |
| 265 |
break; |
| 266 |
case 'y': |
| 267 |
ystrs.unshift(opts.formatString); |
| 268 |
str = $.jqplot.sprintf.apply($.jqplot.sprintf, ystrs); |
| 269 |
break; |
| 270 |
default: |
| 271 |
ystrs.unshift(xstr); |
| 272 |
ystrs.unshift(opts.formatString); |
| 273 |
str = $.jqplot.sprintf.apply($.jqplot.sprintf, ystrs); |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
else { |
| 278 |
switch (opts.tooltipAxes) { |
| 279 |
case 'both': |
| 280 |
case 'xy': |
| 281 |
str = xstr; |
| 282 |
for (var i=0; i<ystrs.length; i++) { |
| 283 |
str += opts.tooltipSeparator + ystrs[i]; |
| 284 |
} |
| 285 |
break; |
| 286 |
case 'yx': |
| 287 |
str = ''; |
| 288 |
for (var i=0; i<ystrs.length; i++) { |
| 289 |
str += ystrs[i] + opts.tooltipSeparator; |
| 290 |
} |
| 291 |
str += xstr; |
| 292 |
break; |
| 293 |
case 'x': |
| 294 |
str = xstr; |
| 295 |
break; |
| 296 |
case 'y': |
| 297 |
str = ystrs.join(opts.tooltipSeparator); |
| 298 |
break; |
| 299 |
default: |
| 300 |
str = xstr; |
| 301 |
for (var i=0; i<ystrs.length; i++) { |
| 302 |
str += opts.tooltipSeparator + ystrs[i]; |
| 303 |
} |
| 304 |
break; |
| 305 |
|
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
else { |
| 310 |
var str; |
| 311 |
if (typeof opts.formatString === 'string') { |
| 312 |
str = $.jqplot.sprintf.apply($.jqplot.sprintf, [opts.formatString].concat(neighbor.data)); |
| 313 |
} |
| 314 |
|
| 315 |
else { |
| 316 |
if (opts.tooltipAxes == 'both' || opts.tooltipAxes == 'xy') { |
| 317 |
str = $.jqplot.sprintf(opts.tooltipFormatString, neighbor.data[0]) + opts.tooltipSeparator + $.jqplot.sprintf(opts.tooltipFormatString, neighbor.data[1]); |
| 318 |
} |
| 319 |
else if (opts.tooltipAxes == 'yx') { |
| 320 |
str = $.jqplot.sprintf(opts.tooltipFormatString, neighbor.data[1]) + opts.tooltipSeparator + $.jqplot.sprintf(opts.tooltipFormatString, neighbor.data[0]); |
| 321 |
} |
| 322 |
else if (opts.tooltipAxes == 'x') { |
| 323 |
str = $.jqplot.sprintf(opts.tooltipFormatString, neighbor.data[0]); |
| 324 |
} |
| 325 |
else if (opts.tooltipAxes == 'y') { |
| 326 |
str = $.jqplot.sprintf(opts.tooltipFormatString, neighbor.data[1]); |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
if ($.isFunction(opts.tooltipContentEditor)) { |
| 331 |
|
| 332 |
|
| 333 |
str = opts.tooltipContentEditor(str, neighbor.seriesIndex, neighbor.pointIndex, plot); |
| 334 |
} |
| 335 |
elem.html(str); |
| 336 |
var gridpos = {x:neighbor.gridData[0], y:neighbor.gridData[1]}; |
| 337 |
var ms = 0; |
| 338 |
var fact = 0.707; |
| 339 |
if (series.markerRenderer.show == true) { |
| 340 |
ms = (series.markerRenderer.size + opts.sizeAdjust)/2; |
| 341 |
} |
| 342 |
|
| 343 |
var loc = locations; |
| 344 |
if (series.fillToZero && series.fill && neighbor.data[1] < 0) { |
| 345 |
loc = oppositeLocations; |
| 346 |
} |
| 347 |
|
| 348 |
switch (loc[locationIndicies[opts.tooltipLocation]]) { |
| 349 |
case 'nw': |
| 350 |
var x = gridpos.x + plot._gridPadding.left - elem.outerWidth(true) - opts.tooltipOffset - fact * ms; |
| 351 |
var y = gridpos.y + plot._gridPadding.top - opts.tooltipOffset - elem.outerHeight(true) - fact * ms; |
| 352 |
break; |
| 353 |
case 'n': |
| 354 |
var x = gridpos.x + plot._gridPadding.left - elem.outerWidth(true)/2; |
| 355 |
var y = gridpos.y + plot._gridPadding.top - opts.tooltipOffset - elem.outerHeight(true) - ms; |
| 356 |
break; |
| 357 |
case 'ne': |
| 358 |
var x = gridpos.x + plot._gridPadding.left + opts.tooltipOffset + fact * ms; |
| 359 |
var y = gridpos.y + plot._gridPadding.top - opts.tooltipOffset - elem.outerHeight(true) - fact * ms; |
| 360 |
break; |
| 361 |
case 'e': |
| 362 |
var x = gridpos.x + plot._gridPadding.left + opts.tooltipOffset + ms; |
| 363 |
var y = gridpos.y + plot._gridPadding.top - elem.outerHeight(true)/2; |
| 364 |
break; |
| 365 |
case 'se': |
| 366 |
var x = gridpos.x + plot._gridPadding.left + opts.tooltipOffset + fact * ms; |
| 367 |
var y = gridpos.y + plot._gridPadding.top + opts.tooltipOffset + fact * ms; |
| 368 |
break; |
| 369 |
case 's': |
| 370 |
var x = gridpos.x + plot._gridPadding.left - elem.outerWidth(true)/2; |
| 371 |
var y = gridpos.y + plot._gridPadding.top + opts.tooltipOffset + ms; |
| 372 |
break; |
| 373 |
case 'sw': |
| 374 |
var x = gridpos.x + plot._gridPadding.left - elem.outerWidth(true) - opts.tooltipOffset - fact * ms; |
| 375 |
var y = gridpos.y + plot._gridPadding.top + opts.tooltipOffset + fact * ms; |
| 376 |
break; |
| 377 |
case 'w': |
| 378 |
var x = gridpos.x + plot._gridPadding.left - elem.outerWidth(true) - opts.tooltipOffset - ms; |
| 379 |
var y = gridpos.y + plot._gridPadding.top - elem.outerHeight(true)/2; |
| 380 |
break; |
| 381 |
default: |
| 382 |
var x = gridpos.x + plot._gridPadding.left - elem.outerWidth(true) - opts.tooltipOffset - fact * ms; |
| 383 |
var y = gridpos.y + plot._gridPadding.top - opts.tooltipOffset - elem.outerHeight(true) - fact * ms; |
| 384 |
break; |
| 385 |
} |
| 386 |
elem.css('left', x); |
| 387 |
elem.css('top', y); |
| 388 |
if (opts.fadeTooltip) { |
| 389 |
|
| 390 |
elem.stop(true,true).fadeIn(opts.tooltipFadeSpeed); |
| 391 |
} |
| 392 |
else { |
| 393 |
elem.show(); |
| 394 |
} |
| 395 |
elem = null; |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
function handleMove(ev, gridpos, datapos, neighbor, plot) { |
| 400 |
var hl = plot.plugins.highlighter; |
| 401 |
var c = plot.plugins.cursor; |
| 402 |
if (hl.show) { |
| 403 |
if (neighbor == null && hl.isHighlighting) { |
| 404 |
var evt = jQuery.Event('jqplotHighlighterUnhighlight'); |
| 405 |
plot.target.trigger(evt); |
| 406 |
|
| 407 |
var ctx = hl.highlightCanvas._ctx; |
| 408 |
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 409 |
if (hl.fadeTooltip) { |
| 410 |
hl._tooltipElem.fadeOut(hl.tooltipFadeSpeed); |
| 411 |
} |
| 412 |
else { |
| 413 |
hl._tooltipElem.hide(); |
| 414 |
} |
| 415 |
if (hl.bringSeriesToFront) { |
| 416 |
plot.restorePreviousSeriesOrder(); |
| 417 |
} |
| 418 |
hl.isHighlighting = false; |
| 419 |
hl.currentNeighbor = null; |
| 420 |
ctx = null; |
| 421 |
} |
| 422 |
else if (neighbor != null && plot.series[neighbor.seriesIndex].showHighlight && !hl.isHighlighting) { |
| 423 |
var evt = jQuery.Event('jqplotHighlighterHighlight'); |
| 424 |
evt.which = ev.which; |
| 425 |
evt.pageX = ev.pageX; |
| 426 |
evt.pageY = ev.pageY; |
| 427 |
var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data, plot]; |
| 428 |
plot.target.trigger(evt, ins); |
| 429 |
|
| 430 |
hl.isHighlighting = true; |
| 431 |
hl.currentNeighbor = neighbor; |
| 432 |
if (hl.showMarker) { |
| 433 |
draw(plot, neighbor); |
| 434 |
} |
| 435 |
if (plot.series[neighbor.seriesIndex].show && hl.showTooltip && (!c || !c._zoom.started)) { |
| 436 |
showTooltip(plot, plot.series[neighbor.seriesIndex], neighbor); |
| 437 |
} |
| 438 |
if (hl.bringSeriesToFront) { |
| 439 |
plot.moveSeriesToFront(neighbor.seriesIndex); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
else if (neighbor != null && hl.isHighlighting && hl.currentNeighbor != neighbor) { |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
if (plot.series[neighbor.seriesIndex].showHighlight) { |
| 448 |
var ctx = hl.highlightCanvas._ctx; |
| 449 |
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 450 |
hl.isHighlighting = true; |
| 451 |
hl.currentNeighbor = neighbor; |
| 452 |
if (hl.showMarker) { |
| 453 |
draw(plot, neighbor); |
| 454 |
} |
| 455 |
if (plot.series[neighbor.seriesIndex].show && hl.showTooltip && (!c || !c._zoom.started)) { |
| 456 |
showTooltip(plot, plot.series[neighbor.seriesIndex], neighbor); |
| 457 |
} |
| 458 |
if (hl.bringSeriesToFront) { |
| 459 |
plot.moveSeriesToFront(neighbor.seriesIndex); |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
})(jQuery); |